Skip to content

Commit

Permalink
test: WIP adding tests
Browse files Browse the repository at this point in the history
[#1]
  • Loading branch information
alxndr committed Nov 20, 2024
1 parent 92ccb25 commit 8421b31
Show file tree
Hide file tree
Showing 4 changed files with 1,359 additions and 15 deletions.
122 changes: 122 additions & 0 deletions index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import {afterEach, beforeEach, describe, expect, test, vi} from 'vitest'
import createFetchMock from 'vitest-fetch-mock'
import {
handlePayload,
isSongfishPayload,
loginBluesky,
type SongfishWebhookPayload,
} from './index'

const fetchMocker = createFetchMock(vi)
fetchMocker.enableMocks()

describe('isSongfishPayload', () => {
test('is a function', () => {
expect(typeof isSongfishPayload).toBe('function')
})
describe('with non-string body', () => {
test('is false', () => {
expect(isSongfishPayload()).toBe(false)
expect(isSongfishPayload(9)).toBe(false)
expect(isSongfishPayload('foo')).toBe(false)
})
})
describe('with an event object containing "body" property with value of a stringified JSON object', () => {
const fakeSongfishPayload:SongfishWebhookPayload = {
body: JSON.stringify({show_id: 'foo'})
}
test('retuns true', () => {
expect(isSongfishPayload(fakeSongfishPayload)).toBe(true)
})
})
})

describe('handlePayload', () => {
test('is a function', () => {
expect(typeof handlePayload).toBe('function')
})
describe('with malformed payload', () => {
test('throws', async () => {
await expect(() => handlePayload({body: '{foo'})).rejects.toThrow(/JSON/)
})
})
describe('with valid payload', () => {
let payload
beforeEach(() => {
const data = {
show_id: 'quxxxx'
}
payload = {
body: JSON.stringify(data)
}
})
describe.only('with invalid login', () => {
beforeEach(() => {
vi.mock('./index', async (importOriginal) => {
const originalImplementation = await importOriginal()
console.log('tryna mock the index import...', originalImplementation)
return {
...originalImplementation,
loginBluesky: function mockedLoginBluesky() {
console.log('mocked loginBluesky implementation......')
throw new Error('mock: loginBluesky failure')
},
}
});
})
test.skip('(SKIPPED: old implementation) resolves with a message', async () => {
await expect(handlePayload(payload)).resolves.toMatch(/foooooo/)
})
test('throws', async () => {
await expect(() => handlePayload(payload)).rejects.toThrow()
})
})
describe('with valid login', () => {
beforeEach(() => {
vi.mock('loginBluesky', {
getAuthorFeed: 'foo' //vi.fn({authorFeedData:'this is mocked author feed data'})
})
})
describe(`with invalid payload`, () => {
beforeEach(() => {
fetchMocker.mockIf(/\bkglw\.net\b.+\blatest\.json$/, () => 'mocked Songfish payload is malformed')
})
test('returns a helpful message', async () => {
await expect(handlePayload({body:JSON.stringify({show_id:123})})).resolves.toBe(
'payload show_id does not match latest show'
)
})
})
describe(`when payload's show_id does _not_ match fetched JSON's data[0].show_id`, () => {
beforeEach(() => {
fetchMocker.mockIf(/\bkglw\.net\b.+\blatest\.json$/, () => ({data: [
{show_id: 666, songname: 'Most Recent Song Name'},
{foo: 'bar'},
{foo: 'baz'},
{foo: 'qux'},
]}))
})
test('returns a helpful message', async () => {
await expect(handlePayload({body:JSON.stringify({show_id:123})})).resolves.toBe(
'payload show_id does not match latest show'
)
})
})
describe(`when payload's show_id matches fetched JSON's data[0].show_id`, () => {
beforeEach(() => {
fetchMocker.mockIf(/kglw/, () => ({data: [
{show_id: 123, songname: 'Most Recent Song Name'},
{foo: 'bar'},
{foo: 'baz'},
{foo: 'qux'},
]}))
})
test('does something useful', async () => {
await expect(handlePayload({body:JSON.stringify({show_id:123})})).resolves.toBe(
'whattttt'
)
})
})
})
})
})
13 changes: 7 additions & 6 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type SongfishWebhookPayload = {
body:{show_id:number}
}

async function loginBluesky():Promise<BskyAgent> {
export async function loginBluesky():Promise<BskyAgent> {
const agent = new BskyAgent({
service: 'https://bsky.social',
})
Expand All @@ -19,23 +19,24 @@ async function loginBluesky():Promise<BskyAgent> {
return agent
}

function isSongfishPayload(event:any):event is SongfishWebhookPayload {
// note that testing this via commandline will mean that the event is a string payload, whereas on Lambda it is a true object
export function isSongfishPayload(event:any):event is SongfishWebhookPayload {
return typeof event?.body === 'string' && event.body.includes('"show_id"')
// TODO could further verify that the song_id appears to be an int...
}

async function handlePayload(event:SongfishWebhookPayload):Promise<string> {
export async function handlePayload(event:SongfishWebhookPayload):Promise<string> {
let payloadBody
try {
payloadBody = JSON.parse(event.body)
} catch (err) {
console.log('error parsing event body', err)
// console.log('error parsing event body', err)
throw err
}
let bsky
console.log('about to call loginBluesky...', loginBluesky)
try {
bsky = await loginBluesky()
console.log('logged in successfully!')
// console.log('logged in successfully!')
} catch (err) {
console.log('login error', err)
throw err
Expand Down
Loading

0 comments on commit 8421b31

Please sign in to comment.