late-results.test.ts (4705B)
1 import { describe, it, expect } from 'vitest'; 2 import { 3 makeFixture, 4 approveCapturedPublicSigning, 5 capturedArtifactSnapshot, 6 publicRecordSnapshot 7 } from '../e2e/harness/approved-signing.ts'; 8 import { decodePublicRecord } from '../../src/lib/persistence/records.ts'; 9 import { 10 bindLatePublicResponse, 11 latePublicResultSnapshot, 12 reviewLatePublicArtifact, 13 type LatePublicArtifact 14 } from '../../src/lib/runtime/late-results.ts'; 15 describe('original public late-result review', () => { 16 for (const kind of [30402, 5, 10050] as const) 17 it(`retains exact verified ${kind} only against its original captured operation`, () => { 18 const f = makeFixture(kind); 19 try { 20 const approval = approveCapturedPublicSigning( 21 f.record, 22 f.owner, 23 f.id, 24 'reviewed_captured_operation' 25 )!; 26 const signed = f.sign(f.template), 27 late = bindLatePublicResponse(approval, signed, Symbol())!; 28 expect(latePublicResultSnapshot(late)).toMatchObject({ 29 owner: f.owner, 30 id: f.id, 31 revision: 0 32 }); 33 expect( 34 reviewLatePublicArtifact(late, f.record, f.owner, f.id, 'unreviewed') 35 ).toBeUndefined(); 36 const reviewed = reviewLatePublicArtifact( 37 late, 38 f.record, 39 f.owner, 40 f.id, 41 'review_original_late_artifact' 42 ); 43 expect( 44 reviewed && JSON.parse(capturedArtifactSnapshot(reviewed)!.wire) 45 ).toEqual(JSON.parse(JSON.stringify(signed))); 46 } finally { 47 f.close(); 48 } 49 }); 50 it('changed full record or owner/ID cannot reinterpret the original result', () => { 51 const f = makeFixture(); 52 try { 53 const approval = approveCapturedPublicSigning( 54 f.record, 55 f.owner, 56 f.id, 57 'reviewed_captured_operation' 58 )!; 59 const late = bindLatePublicResponse( 60 approval, 61 f.sign(f.template), 62 Symbol() 63 )!; 64 const row = publicRecordSnapshot(f.record, f.owner, f.id)!; 65 if (row.family === 'public_draft') throw new Error('invalid fixture'); 66 const changed = decodePublicRecord( 67 JSON.stringify({ 68 ...row, 69 capture: { ...row.capture, targets: ['wss://other.example.org'] } 70 }), 71 f.owner, 72 f.id 73 ); 74 expect(changed.ok).toBe(true); 75 expect( 76 changed.ok && 77 reviewLatePublicArtifact( 78 late, 79 changed.value, 80 f.owner, 81 f.id, 82 'review_original_late_artifact' 83 ) 84 ).toBeUndefined(); 85 expect( 86 reviewLatePublicArtifact( 87 late, 88 f.record, 89 f.owner, 90 crypto.randomUUID(), 91 'review_original_late_artifact' 92 ) 93 ).toBeUndefined(); 94 expect( 95 reviewLatePublicArtifact( 96 late, 97 f.record, 98 '', 99 f.id, 100 'review_original_late_artifact' 101 ) 102 ).toBeUndefined(); 103 } finally { 104 f.close(); 105 } 106 }); 107 for (const mode of ['content', 'cached', 'author'] as const) 108 it(`rejects late ${mode} response despite SDK verification cache`, () => { 109 const f = makeFixture(); 110 try { 111 const approval = approveCapturedPublicSigning( 112 f.record, 113 f.owner, 114 f.id, 115 'reviewed_captured_operation' 116 )!; 117 expect( 118 bindLatePublicResponse(approval, f.sign(f.template, mode), Symbol()) 119 ).toBeUndefined(); 120 } finally { 121 f.close(); 122 } 123 }); 124 it('fake tokens and mutated disclosure cannot convey artifact authority', () => { 125 const f = makeFixture(); 126 try { 127 expect( 128 latePublicResultSnapshot({} as LatePublicArtifact) 129 ).toBeUndefined(); 130 expect( 131 reviewLatePublicArtifact( 132 {} as LatePublicArtifact, 133 f.record, 134 f.owner, 135 f.id, 136 'review_original_late_artifact' 137 ) 138 ).toBeUndefined(); 139 const approval = approveCapturedPublicSigning( 140 f.record, 141 f.owner, 142 f.id, 143 'reviewed_captured_operation' 144 )!; 145 const late = bindLatePublicResponse( 146 approval, 147 f.sign(f.template), 148 Symbol() 149 )!; 150 const view = latePublicResultSnapshot(late)!; 151 (view as { id: string }).id = crypto.randomUUID(); 152 expect(latePublicResultSnapshot(late)?.id).toBe(f.id); 153 expect( 154 reviewLatePublicArtifact( 155 late, 156 f.record, 157 f.owner, 158 f.id, 159 'review_original_late_artifact' 160 ) 161 ).toBeDefined(); 162 } finally { 163 f.close(); 164 } 165 }); 166 });