import { Test } from '@nestjs/testing'; import { INestApplication } from '@nestjs/common'; import { PrismaClient } from '@prisma/client'; import { createHash, randomUUID } from 'crypto'; import { PrismaService } from '../prisma/prisma.service'; import { PrepareWorkflowService } from '../orchestration/prepare-workflow.service'; import { WorkerTransportModule, tracerReport } from './worker-transport.module'; (process.env.EDITORIAL_EXECUTION_DB === '1' ? describe : describe.skip)( 'editorial approved-run real TCP/PostgreSQL', () => { const db = new PrismaClient(); const token = 'disposable-editorial-pmo-'.repeat(3), other = 'disposable-other-editor-'.repeat(3), ci = 'disposable-editorial-ci-'.repeat(3); const sha = (s: string) => createHash('sha256').update(s).digest('hex'); let app: INestApplication, base: string, workflow: PrepareWorkflowService; const brief = () => ({ researchDate: '2026-10-24', inputAvailability: [ { input: 'search console', status: 'UNAVAILABLE', details: 'No authenticated property supplied', }, ], findings: [ { sourceUrl: 'https://www.who.int/news', sourceDate: '2026-10-20', finding: 'Fixture finding for contract testing, not real research', }, ], candidates: [0, 1, 2].map((i) => ({ title: 'Fixture candidate ' + i, score: 80 - i, rationale: 'Contract fixture only', sourceUrls: ['https://www.who.int/news'], })), }); beforeAll(async () => { const m = await Test.createTestingModule({ imports: [ WorkerTransportModule.register({ enabled: true, grants: [ { id: 'pmo', role: 'WORKFLOW_WORKER', credentialSha256: sha(token), }, { id: 'other', role: 'WORKFLOW_WORKER', credentialSha256: sha(other), }, { id: 'ci', role: 'STAGE_CI', credentialSha256: sha(ci) }, ], workerIds: ['pmo', 'other'], stageCiIds: ['ci'], artifactDirectory: '/work/tmp/editorial-execution', artifactBaseUrl: 'https://isolated.invalid/internal/prepare-worker/artifacts', }), ], }) .overrideProvider(PrismaService) .useValue(db) .compile(); app = m.createNestApplication(); await app.listen(0, '127.0.0.1'); base = await app.getUrl(); workflow = app.get(PrepareWorkflowService); }); afterAll(async () => { await app?.close(); await db.$disconnect(); }); async function request(route: string, body?: any, secret = token) { const r = await fetch(base + '/internal/prepare-worker/' + route, { method: body === undefined ? 'GET' : 'POST', headers: { authorization: 'Bearer ' + secret, 'content-type': 'application/json', }, ...(body === undefined ? {} : { body: JSON.stringify(body) }), }); return { status: r.status, body: await r.json() }; } async function proposal(ready = true, ttl = 60000) { const r = await request('editorial-proposals', { requestId: randomUUID(), pilotKind: 'editorial-research-to-brief-v1', operationMode: 'PREPARE', environment: 'staging', scope: 'EDITORIAL_RESEARCH_TO_BRIEF', researchTopic: 'Disposable research contract', acceptanceCriteria: ['Evidence with dates'], commitSha: 'a'.repeat(40), budgetLimitCents: 20, }); expect(r.status).toBe(201); let t = await db.orchestrationTask.findUniqueOrThrow({ where: { id: r.body.taskId }, }); if (ready) { t = await workflow.decide( t.id, 1, { decision: 'APPROVED', expiresAt: new Date(Date.now() + ttl).toISOString(), expectedVersion: t.version, idempotencyKey: randomUUID(), }, { id: 'independent-disposable-reviewer', role: 'SUPER_ADMIN' }, ); // Gate decision now atomically queues READY; no second queue transition. } return t; } async function lease(ttl = 60000) { const t = await proposal(true, ttl); const r = await request('editorial-claim', { taskId: t.id }); expect(r.status).toBe(201); expect(r.body.lease).toBeTruthy(); const l = r.body.lease; return { t, l, b: { taskId: t.id, runId: l.runId, leaseToken: l.leaseToken }, }; } it('stops a held run through authenticated failure API, fences late writes and preserves unknown reservation', async () => { const {t,l,b}=await lease(); const d={...b,expectedVersion: t.version+1,idempotencyKey:'stop:'+l.runId,reason:'Disposable subprocess failure'}; expect((await request('editorial-failure',{...d,leaseToken:randomUUID()})).status).toBe(403); expect((await request('editorial-failure',d,other)).status).toBe(403); expect((await request('editorial-failure',{...d,actualCents:0})).status).toBe(400); const stopped=await Promise.all([request('editorial-failure',d),request('editorial-failure',d)]); expect(stopped.map(x=>x.status)).toEqual([201,201]); expect(stopped[0].body).toMatchObject({state:'FAILED',retainedReservationCents:20}); expect((await request('heartbeat',b)).status).toBe(403); expect((await request('editorial-result',{...b,brief:brief()})).status).toBe(403); expect((await request('editorial-claim',{taskId:t.id})).status).toBe(403); expect(await db.orchestrationArtifact.count({where:{runId:l.runId}})).toBe(0); expect(await db.orchestrationAttestation.count({where:{runId:l.runId}})).toBe(0); expect((await db.orchestrationBudgetLedger.findMany({where:{taskId:t.id}})).map(x=>[x.kind,x.amountCents])).toEqual([['RESERVE',20]]); }); it('recovers an expired run only as independent reviewer and fences original token', async () => { const {t,l,b}=await lease(); const d={runId:l.runId,expectedVersion:t.version+1,idempotencyKey:'stale-stop:'+l.runId,reason:'Disposable ended process'}; await expect(workflow.editorialFailure(t.id,d,{id:'reviewer',role:'SUPER_ADMIN'})).rejects.toThrow(); await db.prepareWorkflowOutbox.update({where:{taskId:t.id},data:{leaseUntil:new Date(Date.now()-1000)}}); const results=await Promise.all([workflow.editorialFailure(t.id,d,{id:'reviewer',role:'SUPER_ADMIN'}),workflow.editorialFailure(t.id,d,{id:'reviewer',role:'SUPER_ADMIN'})]); expect(results.map(x=>x.state)).toEqual(['FAILED','FAILED']); expect((await request('heartbeat',b)).status).toBe(403); expect((await request('editorial-result',{...b,brief:brief()})).status).toBe(403); expect(await db.orchestrationEvent.count({where:{taskId:t.id,eventType:'EDITORIAL_RUN_STOPPED'}})).toBe(1); expect(await db.orchestrationBudgetLedger.count({where:{taskId:t.id}})).toBe(1); }); it('projects only owned exact proposals and refuses pending dispatch', async () => { const t = await proposal(false); const p = await request('editorial-proposals/' + t.id); expect(p.status).toBe(200); expect(p.body).toMatchObject({ taskId: t.id, state: 'GATE1_PENDING', qaStatus: 'PENDING_INDEPENDENT_QA', }); expect(JSON.stringify(p.body)).not.toMatch( /leaseToken|credential|createdBy|fingerprint/, ); expect((await request('editorial-claim', { taskId: t.id })).status).toBe( 403, ); expect( (await request('editorial-proposals/' + t.id, undefined, other)).status, ).toBe(403); expect( (await request('editorial-proposals/' + randomUUID())).status, ).toBe(404); }); it('races claims, ingests immutable bounded brief, never self-attests QA or reconciles unknown cost', async () => { const t = await proposal(); const claims = await Promise.all([ request('editorial-claim', { taskId: t.id }), request('editorial-claim', { taskId: t.id }), ]); const leases = claims.filter((x) => x.body.lease); expect(leases).toHaveLength(1); const l = leases[0].body.lease, b = { taskId: t.id, runId: l.runId, leaseToken: l.leaseToken }; expect((await request('heartbeat', b)).status).toBe(201); expect((await request('claim', { taskId: t.id })).status).toBe(403); expect((await request('tech-claim', { taskId: t.id })).status).toBe(403); expect( (await request('complete', { ...b, report: tracerReport(l) })).status, ).toBe(403); const r = await request('editorial-result', { ...b, brief: brief() }); expect(r.status).toBe(201); expect(r.body).toMatchObject({ state: 'VERIFYING', qaStatus: 'PENDING_INDEPENDENT_QA', retainedReservationCents: 20, }); const p = await request('editorial-proposals/' + t.id); expect(p.body.artifact.brief).toEqual(brief()); expect(sha(p.body.artifact.bytes)).toBe(r.body.artifactSha256); await expect( db.orchestrationArtifact.updateMany({ where: { runId: l.runId }, data: { metadata: { bytes: 'tampered' } }, }), ).rejects.toThrow('append-only'); expect(JSON.parse(p.body.artifact.bytes)).toMatchObject({ taskId: t.id, runId: l.runId, brief: brief(), }); expect( await db.orchestrationAttestation.count({ where: { runId: l.runId } }), ).toBe(0); expect( ( await db.orchestrationRun.findUniqueOrThrow({ where: { id: l.runId }, }) ).status, ).toBe('AWAITING_QA'); expect( ( await db.orchestrationBudgetLedger.findMany({ where: { taskId: t.id }, }) ).map((x) => [x.kind, x.amountCents]), ).toEqual([['RESERVE', 20]]); expect( (await request('editorial-result', { ...b, brief: brief() })).status, ).toBe(403); expect( ( await request( 'stage-evidence', { taskId: t.id, runId: l.runId, commitSha: l.commitSha, sha256: r.body.artifactSha256, evidenceSha256: 'b'.repeat(64), uri: 'https://isolated.invalid/fixture', }, ci, ) ).status, ).toBe(403); }); it('rejects impersonation, unsafe or incomplete briefs, and expired ownership without artifacts', async () => { const { l, b } = await lease(); for (const secret of [other, ci]) expect( (await request('editorial-result', { ...b, brief: brief() }, secret)) .status, ).toBe(403); for (const patch of [ { actualCents: 0 }, { qa: { passed: true } }, { actorId: 'pmo' }, ]) expect( ( await request('editorial-result', { ...b, brief: brief(), ...patch, }) ).status, ).toBe(400); for (const bad of [ { ...brief(), candidates: brief().candidates.slice(0, 2) }, { ...brief(), researchDate: '2026-02-30' }, { ...brief(), findings: [ { sourceUrl: 'http://127.0.0.1', sourceDate: '2026-10-20', finding: 'x', }, ], }, { ...brief(), inputAvailability: [] }, { ...brief(), candidates: brief().candidates.map((c) => ({ ...c, score: 101 })), }, { ...brief(), article: 'not allowed' }, ]) expect( (await request('editorial-result', { ...b, brief: bad })).status, ).toBe(400); expect( ( await request('editorial-result', { ...b, leaseToken: randomUUID(), brief: brief(), }) ).status, ).toBe(403); await db.prepareWorkflowOutbox.update({ where: { taskId: b.taskId }, data: { leaseUntil: new Date(Date.now() - 1000) }, }); expect( (await request('editorial-result', { ...b, brief: brief() })).status, ).toBe(403); expect( (await request('editorial-claim', { taskId: b.taskId })).status, ).toBe(403); expect( await db.orchestrationArtifact.count({ where: { runId: l.runId } }), ).toBe(0); expect( await db.orchestrationBudgetLedger.count({ where: { taskId: b.taskId }, }), ).toBe(1); }); it('enforces cancellation, approval expiry and kill switch inside ingestion', async () => { for (const mode of ['cancel', 'approval', 'kill']) { const { l, b } = await lease(mode === 'approval' ? 1500 : 60000); if (mode === 'cancel') await db.orchestrationTask.update({ where: { id: b.taskId }, data: { state: 'CANCELLED' }, }); if (mode === 'approval') await new Promise((resolve) => setTimeout(resolve, 1600)); if (mode === 'kill') await db.orchestrationKillSwitch.upsert({ where: { scope: 'GLOBAL' }, create: { scope: 'GLOBAL', level: 'STOP_AGENTS', active: true, reason: 'disposable execution test', actorId: 'fixture', }, update: { level: 'STOP_AGENTS', active: true }, }); try { expect( (await request('editorial-result', { ...b, brief: brief() })) .status, ).toBe(403); expect( await db.orchestrationArtifact.count({ where: { runId: l.runId } }), ).toBe(0); } finally { if (mode === 'kill') await db.orchestrationKillSwitch.update({ where: { scope: 'GLOBAL' }, data: { active: false }, }); } } }); it('rejects arbitrary CONTENT tasks and forged stored scope/provenance', async () => { const raw = await workflow.create( { key: 'arbitrary-' + randomUUID(), title: 'arbitrary', department: 'CONTENT', operationMode: 'PREPARE', budgetLimitCents: 20, metadata: { scope: 'editorial-research-to-brief-v1', commitSha: 'a'.repeat(40), acceptanceCriteria: ['x'], }, }, { id: 'pmo', role: 'Admin' }, ); expect((await request('editorial-proposals/' + raw.id)).status).toBe(404); expect( (await request('editorial-claim', { taskId: raw.id })).status, ).toBe(404); const t = await proposal(); await db.orchestrationTask.update({ where: { id: t.id }, data: { metadata: { ...(t.metadata as any), scope: 'editorial-research-to-brief-v1\nforged', }, }, }); expect((await request('editorial-claim', { taskId: t.id })).status).toBe( 404, ); expect(await db.orchestrationRun.count({ where: { taskId: t.id } })).toBe( 0, ); }); }, );