@@ -5,14 +5,15 @@ import * as NodePath from "node:path";
55import * as NodeServices from "@effect/platform-node/NodeServices" ;
66import { describe , expect , it } from "@effect/vitest" ;
77import {
8- type ClientOrchestrationCommand ,
8+ ClientOrchestrationCommand ,
99 CommandId ,
1010 ApprovalRequestId ,
1111 MessageId ,
1212 ThreadId ,
1313} from "@t3tools/contracts" ;
1414import * as Effect from "effect/Effect" ;
1515import * as Layer from "effect/Layer" ;
16+ import * as Schema from "effect/Schema" ;
1617
1718import * as ServerConfig from "../config.ts" ;
1819import * as WorkspacePaths from "../workspace/WorkspacePaths.ts" ;
@@ -24,6 +25,7 @@ const testLayer = Layer.mergeAll(
2425) . pipe ( Layer . provideMerge ( NodeServices . layer ) ) ;
2526
2627const attachmentUuid = "00000000-0000-4000-8000-0000000000aa" ;
28+ const isClientCommand = Schema . is ( ClientOrchestrationCommand ) ;
2729
2830function turnStartCommand ( input : {
2931 readonly threadId ?: string ;
@@ -361,6 +363,58 @@ describe("normalizeDispatchCommand attachments", () => {
361363} ) ;
362364
363365describe ( "question attachments" , ( ) => {
366+ it ( "requires uploaded metadata for question images, including pasted images" , ( ) => {
367+ expect (
368+ isClientCommand ( {
369+ type : "thread.user-input.respond" ,
370+ commandId : "answer" ,
371+ threadId : "thread-1" ,
372+ requestId : "request" ,
373+ answers : { q : "" } ,
374+ createdAt : "2026-08-01T00:00:00.000Z" ,
375+ attachmentsByQuestionId : {
376+ q : [
377+ {
378+ type : "image" ,
379+ name : "image.png" ,
380+ mimeType : "image/png" ,
381+ sizeBytes : 6 ,
382+ dataUrl : "data:image/png;base64,cGl4ZWxz" ,
383+ } ,
384+ ] ,
385+ } ,
386+ } ) ,
387+ ) . toBe ( false ) ;
388+ } ) ;
389+
390+ it . effect ( "preserves a __proto__ question key and cleans up its claimed files" , ( ) =>
391+ Effect . gen ( function * ( ) {
392+ const config = yield * ServerConfig . ServerConfig ;
393+ const id = `pending-${ attachmentUuid } ` ;
394+ NodeFS . writeFileSync ( NodePath . join ( config . attachmentsDir , `${ id } .png` ) , "pixels" ) ;
395+ const command : ClientOrchestrationCommand = {
396+ type : "thread.user-input.respond" ,
397+ commandId : CommandId . make ( "answer" ) ,
398+ threadId : ThreadId . make ( "thread-1" ) ,
399+ requestId : ApprovalRequestId . make ( "request" ) ,
400+ answers : { [ "__proto__" ] : "" } ,
401+ createdAt : "2026-08-01T00:00:00.000Z" ,
402+ attachmentsByQuestionId : {
403+ [ "__proto__" ] : [
404+ { type : "image" , id, name : "image.png" , mimeType : "image/png" , sizeBytes : 6 } ,
405+ ] ,
406+ } ,
407+ } ;
408+ const normalized = yield * normalizeDispatchCommand ( command ) ;
409+ if ( normalized . type !== "thread.user-input.respond" ) throw new Error ( "Wrong command" ) ;
410+ expect ( Object . keys ( normalized . attachmentsByQuestionId ! ) ) . toEqual ( [ "__proto__" ] ) ;
411+ const attachment = normalized . attachmentsByQuestionId ! [ "__proto__" ] ! [ 0 ] ! ;
412+ const claimedPath = NodePath . join ( config . attachmentsDir , `${ attachment . id } .png` ) ;
413+ expect ( NodeFS . existsSync ( claimedPath ) ) . toBe ( true ) ;
414+ yield * cleanupFailedUploadedAttachments ( command , normalized ) ;
415+ expect ( NodeFS . existsSync ( claimedPath ) ) . toBe ( false ) ;
416+ } ) . pipe ( Effect . provide ( testLayer ) ) ,
417+ ) ;
364418 it . effect (
365419 "claims images and files by question, preserves answers, and cleans up failed dispatches" ,
366420 ( ) =>
0 commit comments