@@ -4,6 +4,7 @@ import * as NodePath from "node:path";
44
55import * as NodeServices from "@effect/platform-node/NodeServices" ;
66import { describe , expect , it } from "@effect/vitest" ;
7+ import { ChatAttachmentId } from "@t3tools/contracts" ;
78import * as Deferred from "effect/Deferred" ;
89import * as Effect from "effect/Effect" ;
910import * as Fiber from "effect/Fiber" ;
@@ -14,6 +15,10 @@ import * as Stream from "effect/Stream";
1415import * as ServerSecretStore from "../auth/ServerSecretStore.ts" ;
1516import { base64UrlEncode , signPayload } from "../auth/utils.ts" ;
1617import * as ServerConfig from "../config.ts" ;
18+ import {
19+ claimPendingAttachments ,
20+ releaseClaimedAttachments ,
21+ } from "../orchestration-v2/AttachmentClaims.ts" ;
1722import {
1823 ATTACHMENT_UPLOAD_ROUTE_PREFIX ,
1924 deletePendingAttachment ,
@@ -139,6 +144,47 @@ describe("AttachmentUpload", () => {
139144 } ) . pipe ( Effect . provide ( layerTest ) ) ,
140145 ) ;
141146
147+ it . effect ( "stores MIME-identified SVG files with a previewable signed extension" , ( ) =>
148+ Effect . gen ( function * ( ) {
149+ const config = yield * ServerConfig . ServerConfig ;
150+ const bytes = new TextEncoder ( ) . encode ( '<svg xmlns="http://www.w3.org/2000/svg"/>' ) ;
151+ for ( const name of [ "diagram" , "diagram.bin" ] ) {
152+ const issued = yield * issueAttachmentUploadUrl ( {
153+ type : "file" ,
154+ name,
155+ mimeType : "image/svg+xml" ,
156+ sizeBytes : bytes . length ,
157+ } ) ;
158+ const token = issued . relativeUrl . slice ( `${ ATTACHMENT_UPLOAD_ROUTE_PREFIX } /` . length ) ;
159+ const claims = yield * validateAttachmentUploadToken ( token ) ;
160+ if ( ! claims ) throw new Error ( "Expected valid SVG upload claims." ) ;
161+ expect ( claims . name ) . toBe ( `${ name } .svg` ) ;
162+ expect ( issued . attachmentId ) . toMatch ( / - s v g $ / ) ;
163+ expect ( yield * storeAttachmentUpload ( claims , bytes ) ) . toEqual ( { ok : true } ) ;
164+ expect (
165+ NodeFS . readFileSync ( NodePath . join ( config . attachmentsDir , `${ issued . attachmentId } .svg` ) ) ,
166+ ) . toEqual ( Buffer . from ( bytes ) ) ;
167+ const claimed = yield * claimPendingAttachments ( {
168+ threadId : "svg-upload-thread" ,
169+ attachments : [
170+ {
171+ type : "file" ,
172+ id : ChatAttachmentId . make ( issued . attachmentId ) ,
173+ name,
174+ mimeType : "image/svg+xml" ,
175+ sizeBytes : bytes . length ,
176+ } ,
177+ ] ,
178+ } ) ;
179+ expect ( claimed . attachments [ 0 ] ?. name ) . toBe ( `${ name } .svg` ) ;
180+ expect ( NodeFS . readFileSync ( claimed . claimedPaths [ 0 ] ! ) ) . toEqual ( Buffer . from ( bytes ) ) ;
181+ yield * releaseClaimedAttachments ( claimed . claimedPaths ) ;
182+ yield * deletePendingAttachment ( issued . attachmentId ) ;
183+ }
184+ expect ( NodeFS . readdirSync ( config . attachmentsDir ) ) . toEqual ( [ ] ) ;
185+ } ) . pipe ( Effect . provide ( layerTest ) ) ,
186+ ) ;
187+
142188 it . effect ( "removes partial streamed uploads that exceed their signed size" , ( ) =>
143189 Effect . gen ( function * ( ) {
144190 const config = yield * ServerConfig . ServerConfig ;
0 commit comments