11import { assert , describe , it } from "@effect/vitest"
22import { Schema , SchemaRepresentation } from "effect"
3+ import { runInNewContext } from "node:vm"
34import { assertInclude , throws } from "../../utils/assert.ts"
45
56function expectError ( thunk : ( ) => void , expected : string | Error ) : void {
@@ -105,7 +106,9 @@ describe("SchemaRepresentation.toCodeDocument annotations", () => {
105106 symbol : Symbol . for ( "shared" ) ,
106107 nan : NaN ,
107108 positive : Infinity ,
108- negative : - Infinity
109+ negative : - Infinity ,
110+ nullPrototype : Object . create ( null ) ,
111+ crossRealm : runInNewContext ( "({ value: 1 })" ) as unknown
109112 } ,
110113 omitted : { value : 1 , callback : ( ) => 2 }
111114 } ) . ast
@@ -117,10 +120,33 @@ describe("SchemaRepresentation.toCodeDocument annotations", () => {
117120 assertInclude ( runtime , `"nan": NaN` )
118121 assertInclude ( runtime , `"positive": Infinity` )
119122 assertInclude ( runtime , `"negative": -Infinity` )
123+ assertInclude ( runtime , `"nullPrototype": {}` )
124+ assertInclude ( runtime , `"crossRealm": { "value": 1 }` )
120125 assert . isFalse ( runtime . includes ( "omitted" ) )
121126 assert . isFalse ( runtime . includes ( "callback" ) )
122127 } )
123128
129+ it ( "omits unsupported object annotations atomically" , ( ) => {
130+ const schema = Schema . String . annotate ( {
131+ kept : { value : 1 } ,
132+ map : new Map ( [ [ "a" , 1 ] ] ) ,
133+ set : new Set ( [ 1 ] ) ,
134+ date : new Date ( 0 ) ,
135+ bytes : new Uint8Array ( [ 1 , 2 ] ) ,
136+ instance : new ( class {
137+ value = 1
138+ } ) ( ) ,
139+ nested : { value : 1 , map : new Map ( [ [ "a" , 1 ] ] ) } ,
140+ array : [ 1 , new Set ( [ 1 ] ) ]
141+ } )
142+ const document = SchemaRepresentation . toRepresentations ( [ schema . ast ] )
143+
144+ assert . strictEqual (
145+ SchemaRepresentation . toCodeDocument ( document ) . codes [ 0 ] . runtime ,
146+ `Schema.String.annotate({ "kept": { "value": 1 } })`
147+ )
148+ } )
149+
124150 it ( "preserves top-level and nested __proto__ annotations in generated code" , ( ) => {
125151 const value = { [ "__proto__" ] : { trusted : true } }
126152 const annotations = { ...value , examples : [ value ] }
@@ -153,7 +179,8 @@ describe("SchemaRepresentation.toCodeDocument annotations", () => {
153179 type : StringRepresentation ,
154180 annotations : {
155181 element : { value : 1 } ,
156- omitted : { callback : ( ) => 1 }
182+ omitted : { callback : ( ) => 1 } ,
183+ map : new Map ( [ [ "a" , 1 ] ] )
157184 }
158185 } ] ,
159186 rest : [ ] ,
@@ -166,7 +193,7 @@ describe("SchemaRepresentation.toCodeDocument annotations", () => {
166193 type : NumberRepresentation ,
167194 isOptional : false ,
168195 isMutable : false ,
169- annotations : { property : true }
196+ annotations : { property : true , nested : { set : new Set ( [ 1 ] ) } }
170197 } ] ,
171198 indexSignatures : [ ] ,
172199 checks : [ ]
0 commit comments