1818
1919import { describe , it , expect , beforeAll , afterAll } from 'vitest' ;
2020import { SECRET_MASK } from '@objectstack/objectql' ;
21+ import { FILE_REFERENCE_TYPES } from '@objectstack/spec/data' ;
2122import { type VerifyStack } from '@objectstack/verify' ;
2223import { bootShowcase } from './showcase-boot.js' ;
2324
@@ -27,6 +28,8 @@ describe('dogfood: field-type capability matrix round-trips over HTTP (#2004)',
2728 let record : Record < string , unknown > ;
2829 /** Resolved reference ids, keyed by field — the assertions compare to these. */
2930 const referenceIds : Record < string , string > = { } ;
31+ /** Uploaded `sys_file` ids, keyed by media field — the assertions compare to these. */
32+ const fileIds : Record < string , string > = { } ;
3033
3134 beforeAll ( async ( ) => {
3235 stack = await bootShowcase ( ) ;
@@ -54,13 +57,38 @@ describe('dogfood: field-type capability matrix round-trips over HTTP (#2004)',
5457 referenceIds [ target . field ] = id as string ;
5558 }
5659
60+ // [#26043] Upload a REAL file for each media field. A write naming an id
61+ // that is no file of the organization is now refused at the field, as one
62+ // naming a file the writer may not read is, so the matrix's stand-in ids
63+ // would be refused. What this file proves for the media family is
64+ // unchanged: the stored id round-trips as the same file.
65+ for ( const c of MATRIX ) {
66+ if ( ! FILE_REFERENCE_TYPES . has ( c . type ) || ! ( 'write' in c . check ) || c . check . write === undefined ) continue ;
67+ const bytes = `zoo ${ c . field } ` ;
68+ const presign = await stack . apiAs ( token , 'POST' , '/storage/upload/presigned' , {
69+ filename : `${ c . field } .txt` , mimeType : 'text/plain' , size : bytes . length ,
70+ } ) ;
71+ expect ( presign . status , `presign for ${ c . field } ` ) . toBe ( 200 ) ;
72+ const { data } = ( await presign . json ( ) ) as { data : { uploadUrl : string ; fileId : string ; headers ?: Record < string , string > } } ;
73+ const put = await stack . raw ( String ( data . uploadUrl ) . replace ( / ^ h t t p s ? : \/ \/ [ ^ / ] + / , '' ) , {
74+ method : 'PUT' , headers : data . headers ?? { 'content-type' : 'text/plain' } , body : bytes ,
75+ } ) ;
76+ expect ( put . status , `upload for ${ c . field } ` ) . toBeLessThan ( 300 ) ;
77+ const complete = await stack . apiAs ( token , 'POST' , '/storage/upload/complete' , { fileId : data . fileId } ) ;
78+ expect ( complete . status , `complete for ${ c . field } ` ) . toBe ( 200 ) ;
79+ fileIds [ c . field ] = String ( data . fileId ) ;
80+ }
81+
5782 // Build the create body from every entry that carries a `write` value
5883 // (+ required name). `present`/`computed` server-owned fields are skipped.
59- // A reference placeholder resolves to the id seeded above.
84+ // A reference placeholder resolves to the id seeded above, and a media
85+ // field to the file uploaded above.
6086 const body : Record < string , unknown > = { name : 'zoo-roundtrip' } ;
6187 for ( const c of MATRIX ) {
6288 if ( ! ( 'write' in c . check ) || c . check . write === undefined ) continue ;
63- body [ c . field ] = c . field in referenceIds ? referenceIds [ c . field ] : c . check . write ;
89+ body [ c . field ] = c . field in referenceIds
90+ ? referenceIds [ c . field ]
91+ : c . field in fileIds ? fileIds [ c . field ] : c . check . write ;
6492 }
6593
6694 const created = await stack . apiAs ( token , 'POST' , '/data/showcase_field_zoo' , body ) ;
@@ -88,6 +116,13 @@ describe('dogfood: field-type capability matrix round-trips over HTTP (#2004)',
88116 const actual = record [ c . field ] ;
89117 switch ( c . check . kind ) {
90118 case 'equal' :
119+ // A media field reads back in its expanded form, the file object
120+ // the stored id resolves to (ADR-0104 D3 wave 2), so its `id` is
121+ // the stored id.
122+ if ( c . field in fileIds ) {
123+ expect ( actual ) . toMatchObject ( { id : fileIds [ c . field ] } ) ;
124+ break ;
125+ }
91126 expect ( actual ) . toEqual (
92127 c . field in referenceIds ? referenceIds [ c . field ] : c . check . write ,
93128 ) ;
0 commit comments