import { readFileSync } from 'node:fs'; import { describe, expect, it } from 'vitest'; import { parseJobMessage } from './job-message.js'; const contract = JSON.parse( readFileSync( new URL('../../../contracts/jobs.schema.json', import.meta.url), 'utf8', ), ) as { examples: Record[] }; const example = contract.examples[0]; describe('parseJobMessage', () => { it('accepts the contract example', () => { expect(parseJobMessage(Buffer.from(JSON.stringify(example)))).toEqual( example, ); }); it('rejects an extra property', () => { expect( parseJobMessage( Buffer.from(JSON.stringify({ ...example, extra: true })), ), ).toBeUndefined(); }); it('rejects attempt 0', () => { expect( parseJobMessage( Buffer.from(JSON.stringify({ ...example, attempt: 0 })), ), ).toBeUndefined(); }); it('rejects a wrong type', () => { expect( parseJobMessage( Buffer.from(JSON.stringify({ ...example, type: 'other' })), ), ).toBeUndefined(); }); it('rejects non-JSON', () => { expect(parseJobMessage(Buffer.from('not-json'))).toBeUndefined(); }); });