import { describe, expect, test } from 'bun:test' import type { SchemaProperty } from './test-case-review.ts' const structured = (properties: Record) => ({ isWorkflow: false, configuration: { structuredOutput: { enabled: true, schema: { properties } } }, }) function testCase(generation: Record) { return { id: 'tc', generations: [{ id: 'gen', status: 'completed', createdAt: '2026-01-01T00:00:00Z', ...generation }], } as never } describe('getTestCaseReview', () => { test('reports pending_review when nothing has been rated', async () => { const { getTestCaseReview } = await import('./test-case-review.ts') const review = getTestCaseReview( testCase({ content: '{"vendor":"Acme","total":10}' }), structured({ vendor: { type: 'string' }, total: { type: 'number' } }), ) expect(review.status).toBe('pending_review') expect(review.pending).toEqual(['vendor', 'total']) expect(review.unreviewable).toBe(false) }) test('reports partially_reviewed when some attributes are rated', async () => { const { getTestCaseReview } = await import('./test-case-review.ts') const review = getTestCaseReview( testCase({ content: '{"vendor":"Acme","total":10}', attributeFeedback: { vendor: 1 } }), structured({ vendor: { type: 'string' }, total: { type: 'number' } }), ) expect(review.status).toBe('partially_reviewed') expect(review.reviewed).toEqual(['vendor']) expect(review.pending).toEqual(['total']) }) test('reports completed once every attribute is rated', async () => { const { getTestCaseReview } = await import('./test-case-review.ts') const review = getTestCaseReview( testCase({ content: '{"vendor":"Acme","total":10}', attributeFeedback: { vendor: 1, total: 0 } }), structured({ vendor: { type: 'string' }, total: { type: 'number' } }), ) expect(review.status).toBe('completed') expect(review.pending).toEqual([]) expect(review.unreviewable).toBe(false) }) test('counts an inferred good/bad validation as reviewed', async () => { const { getTestCaseReview } = await import('./test-case-review.ts') const review = getTestCaseReview( testCase({ content: '{"vendor":"Acme"}', validations: { vendor: { type: 'good' } } }), structured({ vendor: { type: 'string' } }), ) expect(review.status).toBe('completed') }) test('does not count a warning validation as reviewed', async () => { const { getTestCaseReview } = await import('./test-case-review.ts') const review = getTestCaseReview( testCase({ content: '{"vendor":"Acme"}', validations: { vendor: { type: 'warning' } } }), structured({ vendor: { type: 'string' } }), ) expect(review.status).toBe('pending_review') }) test('flags a canvas with no structured output as unreviewable', async () => { const { getTestCaseReview } = await import('./test-case-review.ts') const review = getTestCaseReview( testCase({ content: 'plain text answer' }), { isWorkflow: false, configuration: {} }, ) expect(review.status).toBe('completed') expect(review.unreviewable).toBe(true) }) test('flags an enabled but empty schema as unreviewable', async () => { const { getTestCaseReview } = await import('./test-case-review.ts') const review = getTestCaseReview(testCase({ content: '{}' }), structured({})) expect(review.status).toBe('completed') expect(review.unreviewable).toBe(true) }) test('treats the string "false" as enabled, matching the product', async () => { const { getTestCaseReview } = await import('./test-case-review.ts') const review = getTestCaseReview( testCase({ content: '{"vendor":"Acme"}' }), { isWorkflow: false, configuration: { structuredOutput: { enabled: 'true', schema: { properties: { vendor: {} } } } } }, ) expect(review.status).toBe('pending_review') }) test('flattens nested objects to dot-paths and keeps arrays as one key', async () => { const { getTestCaseReview } = await import('./test-case-review.ts') const review = getTestCaseReview( testCase({ content: '{}' }), structured({ address: { type: 'object', properties: { city: { type: 'string' }, zip: { type: 'string' } } }, items: { type: 'array' }, }), ) expect(review.pending).toEqual(['address.city', 'address.zip', 'items']) }) test('skips attributes hidden from validation', async () => { const { getTestCaseReview } = await import('./test-case-review.ts') const review = getTestCaseReview( testCase({ content: '{}' }), structured({ vendor: { type: 'string' }, debug: { type: 'string', hideFromValidation: true } }), ) expect(review.pending).toEqual(['vendor']) }) test('derives workflow attributes from the run content when the schema is empty', async () => { const { getTestCaseReview } = await import('./test-case-review.ts') const review = getTestCaseReview( testCase({ content: '{"summary":"x","score":{"value":1}}' }), { isWorkflow: true, configuration: { structuredOutput: { enabled: true, schema: { properties: {} } } } }, ) expect(review.pending).toEqual(['summary', 'score.value']) expect(review.unreviewable).toBe(false) }) test('surfaces a failed run rather than a review state', async () => { const { getTestCaseReview } = await import('./test-case-review.ts') const review = getTestCaseReview( testCase({ status: 'failed', content: '' }), structured({ vendor: { type: 'string' } }), ) expect(review.status).toBe('failed') }) test('reports pending_run when the test case has never run', async () => { const { getTestCaseReview } = await import('./test-case-review.ts') expect(getTestCaseReview({ id: 'tc', generations: [] } as never, structured({})).status).toBe('pending_run') }) test('reviews the most recent generation', async () => { const { getTestCaseReview } = await import('./test-case-review.ts') const review = getTestCaseReview( { id: 'tc', generations: [ { id: 'old', status: 'completed', createdAt: '2026-01-01T00:00:00Z', content: '{"vendor":"A"}', attributeFeedback: { vendor: 1 } }, { id: 'new', status: 'completed', createdAt: '2026-02-01T00:00:00Z', content: '{"vendor":"B"}' }, ], } as never, structured({ vendor: { type: 'string' } }), ) expect(review.status).toBe('pending_review') }) })