Skip to content

Commit

Permalink
test: use fixture of full Lambda payload
Browse files Browse the repository at this point in the history
[#1]
  • Loading branch information
alxndr committed Nov 21, 2024
1 parent fc88f2e commit ca6fbaf
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 2 deletions.
33 changes: 31 additions & 2 deletions index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
isSongfishPayload,
type SongfishWebhookPayload,
} from './index'
import testFixture from './test-fixture.json'

vi.mock('./bluesky') // use e.g.: vi.mocked(login).mockResolvedValue({})

Expand Down Expand Up @@ -68,7 +69,7 @@ describe('handlePayload', () => {
})
})
describe('with valid login and prior post does not match latest song title', () => {
let mockedLoginReturnValue = {
const mockedLoginReturnValue = {
getAuthorFeed: vi.fn().mockReturnValueOnce({data: {feed: [{post: {record: {text: 'Prior Post'}}}] }}),
}
beforeEach(() => {
Expand Down Expand Up @@ -125,5 +126,33 @@ describe('handlePayload', () => {
})
})
})
describe.todo('with fixture payload')
describe('with fixture payload matching latest show_id', () => {
const testWithFixture = test.extend({
event: async ({}, use) => {
await use(testFixture.event)
}
})
const mockedLoginReturnValue = {
getAuthorFeed: vi.fn().mockReturnValueOnce({data: {feed: [{post: {record: {text: 'Prior Post'}}}] }}),
}
let mockedPost
beforeEach(() => {
mockedPost = vi.fn().mockReturnValueOnce({mocked: true})
vi.mocked(login).mockResolvedValue({
...mockedLoginReturnValue,
post: mockedPost,
})
mockJson(/\bkglw\.net\b.+\blatest\.json$/, {data: [
// the id 1699404057 is defined in the fixture file
{show_id: 1699404057, songname: 'Name of Song From Show #1699404057'},
]})
})
afterEach(() => {
vi.mocked(login).mockReset()
})
testWithFixture('does not throw', async ({event}) => {
await expect(handlePayload(event)).resolves.not.to.throw()
expect(mockedPost).toHaveBeenCalledWith({text: 'Name of Song From Show #1699404057'})
})
})
})
40 changes: 40 additions & 0 deletions test-fixture.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"event": {
"version": "2.0",
"routeKey": "$default",
"rawPath": "/",
"rawQueryString": "",
"headers": {
"x-amzn-tls-cipher-suite": "TLS_AES_128_GCM_SHA256",
"content-length": "22",
"x-amzn-tls-version": "TLSv1.3",
"x-amzn-trace-id": "Root=1-673ab04e-002e78a15109e2ae29abb650",
"x-forwarded-proto": "https",
"host": "t7es4wh4viztwzqupz3rhkqcvi0sxpoq.lambda-url.us-west-1.on.aws",
"x-forwarded-port": "443",
"content-type": "application/json",
"x-forwarded-for": "149.28.253.24",
"accept": "*/*"
},
"requestContext": {
"accountId": "anonymous",
"apiId": "t7es4wh4viztwzqupz3rhkqcvi0sxpoq",
"domainName": "t7es4wh4viztwzqupz3rhkqcvi0sxpoq.lambda-url.us-west-1.on.aws",
"domainPrefix": "t7es4wh4viztwzqupz3rhkqcvi0sxpoq",
"http": {
"method": "POST",
"path": "/",
"protocol": "HTTP/1.1",
"sourceIp": "149.28.253.24",
"userAgent": null
},
"requestId": "36366359-454e-4f2a-87cf-53e666d930fe",
"routeKey": "$default",
"stage": "$default",
"time": "18/Nov/2024:03:11:10 +0000",
"timeEpoch": 1731899470068
},
"body": "{\"show_id\":1699404057}",
"isBase64Encoded": false
}
}

0 comments on commit ca6fbaf

Please sign in to comment.