Skip to content

Commit 0892d09

Browse files
committed
fix: test
1 parent fdaa00a commit 0892d09

File tree

10 files changed

+304
-212
lines changed

10 files changed

+304
-212
lines changed

packages/evolution/.tsbuildinfo/build.tsbuildinfo

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

packages/evolution/package.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,16 @@
3737
"clean": "rm -rf dist .turbo docs"
3838
},
3939
"devDependencies": {
40-
"typescript": "^5.4.0",
41-
"@types/dockerode": "^3.3.0"
40+
"@types/dockerode": "^3.3.0",
41+
"tsx": "^4.20.3",
42+
"typescript": "^5.4.0"
4243
},
4344
"dependencies": {
44-
"effect": "^3.0.0",
45+
"@effect/platform": "^0.90.0",
46+
"@effect/platform-node": "^0.94.0",
4547
"@scure/base": "^1.1.0",
46-
"@anastasia-labs/cardano-multiplatform-lib-nodejs": "^6.0.0",
4748
"dockerode": "^4.0.0",
48-
"@effect/platform-node": "^0.63.0"
49+
"effect": "^3.17.3"
4950
},
5051
"keywords": [
5152
"cardano",

packages/evolution/src/TSchema.ts

Lines changed: 46 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -33,32 +33,20 @@ export const ByteArray: ByteArray = Data.BytesSchema.annotations({
3333
// }
3434
// );
3535

36-
interface Integer extends Schema.transform<typeof Data.IntSchema, typeof Schema.BigIntFromSelf> {}
36+
interface Integer extends Schema.SchemaClass<bigint, bigint, never> {}
3737

3838
/**
3939
* Creates a schema for integers using Plutus Data Integer transformation
4040
* The integer is represented as a BigInt
4141
*
4242
* @since 2.0.0
4343
*/
44-
// export const Integer: Integer = Schema.transform(
45-
// Data.IntSchema,
46-
// Schema.BigIntFromSelf,
47-
// {
48-
// strict: true,
49-
// encode: (value) => value,
50-
// decode: (value) => value,
51-
// }
52-
// ).annotations({
53-
// identifier: "TSchema.Integer",
54-
// });
55-
56-
export const Integer = Data.IntSchema.annotations({
44+
export const Integer: Integer = Data.IntSchema.annotations({
5745
identifier: "TSchema.Integer"
5846
})
5947

6048
interface Literal<Literals extends NonEmptyReadonlyArray<SchemaAST.LiteralValue>>
61-
extends Schema.transform<typeof Data.Constr, Schema.Literal<[...Literals]>> {}
49+
extends Schema.transform<Schema.SchemaClass<Data.Constr, Data.Constr, never>, Schema.Literal<[...Literals]>> {}
6250

6351
/**
6452
* Creates a schema for literal types with Plutus Data Constructor transformation
@@ -68,22 +56,22 @@ interface Literal<Literals extends NonEmptyReadonlyArray<SchemaAST.LiteralValue>
6856
export const Literal = <Literals extends NonEmptyReadonlyArray<Exclude<SchemaAST.LiteralValue, null | bigint>>>(
6957
...self: Literals
7058
): Literal<Literals> =>
71-
Schema.transform(Data.Constr, Schema.Literal(...self), {
59+
Schema.transform(Schema.typeSchema(Data.Constr), Schema.Literal(...self), {
7260
strict: true,
73-
encode: (value) => Data.constr(BigInt(self.indexOf(value)), []),
61+
encode: (value) => new Data.Constr({ index: BigInt(self.indexOf(value)), fields: [] }),
7462
decode: (value) => self[Number(value.index)]
7563
})
7664

7765
interface OneLiteral<Single extends Exclude<SchemaAST.LiteralValue, null | bigint>>
78-
extends Schema.transform<typeof Data.Constr, Schema.Literal<[Single]>> {}
66+
extends Schema.transform<Schema.SchemaClass<Data.Constr, Data.Constr, never>, Schema.Literal<[Single]>> {}
7967

8068
export const OneLiteral = <Single extends Exclude<SchemaAST.LiteralValue, null | bigint>>(
8169
self: Single
8270
): OneLiteral<Single> =>
83-
Schema.transform(Data.Constr, Schema.Literal(self), {
71+
Schema.transform(Schema.typeSchema(Data.Constr), Schema.Literal(self), {
8472
strict: true,
8573

86-
encode: (_value) => Data.constr(0n, []),
74+
encode: (_value) => new Data.Constr({ index: 0n, fields: [] }),
8775

8876
decode: (_value) => self
8977
})
@@ -116,48 +104,22 @@ interface Map<K extends Schema.Schema.Any, V extends Schema.Schema.Any>
116104
* @since 1.0.0
117105
*/
118106
export const Map = <K extends Schema.Schema.Any, V extends Schema.Schema.Any>(key: K, value: V): Map<K, V> =>
119-
Schema.transform(
120-
Schema.typeSchema(Data.MapSchema),
121-
Schema.MapFromSelf({ key, value }).annotations({
122-
identifier: "TSchema.Map"
123-
}),
124-
{
125-
strict: false,
126-
encode: (tsMap) => {
127-
// Transform TypeScript Map<K_TS, V_TS> to Data Map<K_Data, V_Data>
128-
// The individual key/value transformations are handled by the schema framework
129-
return new globalThis.Map([...tsMap])
130-
},
131-
decode: (dataMap) => {
132-
// Transform Data Map<K_Data, V_Data> to TypeScript Map<K_TS, V_TS>
133-
// The individual key/value transformations are handled by the schema framework
134-
return new globalThis.Map([...dataMap])
135-
}
136-
}
137-
).annotations({
138-
identifier: "TSchema.MapFromDataMap",
139-
message: (issue) => {
140-
const actual = issue.actual
141-
const isMap =
142-
typeof actual === "object" &&
143-
actual !== null &&
144-
typeof (actual as Record<string, unknown>).size === "number" &&
145-
typeof (actual as Record<string, unknown>).set === "function"
146-
147-
if (!isMap) {
148-
const actualType =
149-
typeof actual === "bigint"
150-
? "bigint"
151-
: typeof actual === "object" && actual !== null && globalThis.Array.isArray(actual)
152-
? "array"
153-
: typeof actual
154-
return `Invalid value for Map: received ${actualType} (${JSON.stringify(actual)}), expected Map`
155-
}
156-
return `Invalid Map: check that all keys and values match the expected types`
107+
Schema.transform(Schema.typeSchema(Data.MapSchema), Schema.MapFromSelf({ key, value }), {
108+
strict: false,
109+
encode: (tsMap) => {
110+
// Transform TypeScript Map<K_TS, V_TS> to Data Map<K_Data, V_Data>
111+
// The individual key/value transformations are handled by the schema framework
112+
return new globalThis.Map([...tsMap])
113+
},
114+
decode: (dataMap) => {
115+
// Transform Data Map<K_Data, V_Data> to TypeScript Map<K_TS, V_TS>
116+
// The individual key/value transformations are handled by the schema framework
117+
return new globalThis.Map([...dataMap])
157118
}
158119
})
159120

160-
interface NullOr<S extends Schema.Schema.All> extends Schema.transform<typeof Data.Constr, Schema.NullOr<S>> {}
121+
interface NullOr<S extends Schema.Schema.All>
122+
extends Schema.transform<Schema.SchemaClass<Data.Constr, Data.Constr, never>, Schema.NullOr<S>> {}
161123

162124
/**
163125
* Creates a schema for nullable types that transforms to/from Plutus Data Constructor
@@ -168,13 +130,15 @@ interface NullOr<S extends Schema.Schema.All> extends Schema.transform<typeof Da
168130
* @since 2.0.0
169131
*/
170132
export const NullOr = <S extends Schema.Schema.All>(self: S): NullOr<S> =>
171-
Schema.transform(Data.Constr, Schema.NullOr(self), {
133+
Schema.transform(Schema.typeSchema(Data.Constr), Schema.NullOr(self), {
172134
strict: true,
173-
encode: (value) => (value === null ? Data.constr(1n, []) : Data.constr(0n, [value])),
135+
encode: (value) =>
136+
value === null ? new Data.Constr({ index: 1n, fields: [] }) : new Data.Constr({ index: 0n, fields: [value] }),
174137
decode: (value) => (value.index === 1n ? null : (value.fields[0] as Schema.Schema.Type<S>))
175138
})
176139

177-
interface UndefineOr<S extends Schema.Schema.Any> extends Schema.transform<typeof Data.Constr, Schema.UndefinedOr<S>> {}
140+
interface UndefineOr<S extends Schema.Schema.Any>
141+
extends Schema.transform<Schema.SchemaClass<Data.Constr, Data.Constr, never>, Schema.UndefinedOr<S>> {}
178142

179143
/**
180144
* Creates a schema for undefined types that transforms to/from Plutus Data Constructor
@@ -185,9 +149,12 @@ interface UndefineOr<S extends Schema.Schema.Any> extends Schema.transform<typeo
185149
* @since 2.0.0
186150
*/
187151
export const UndefinedOr = <S extends Schema.Schema.Any>(self: S): UndefineOr<S> =>
188-
Schema.transform(Data.Constr, Schema.UndefinedOr(self), {
152+
Schema.transform(Schema.typeSchema(Data.Constr), Schema.UndefinedOr(self), {
189153
strict: true,
190-
encode: (value) => (value === undefined ? Data.constr(1n, []) : Data.constr(0n, [value])),
154+
encode: (value) =>
155+
value === undefined
156+
? new Data.Constr({ index: 1n, fields: [] })
157+
: new Data.Constr({ index: 0n, fields: [value] }),
191158
decode: (value) => (value.index === 1n ? undefined : (value.fields[0] as Schema.Schema.Type<S>))
192159
})
193160

@@ -225,7 +192,7 @@ export const Boolean: Boolean = Schema.transform(
225192
})
226193

227194
interface Struct<Fields extends Schema.Struct.Fields>
228-
extends Schema.transform<typeof Data.Constr, Schema.Struct<Fields>> {}
195+
extends Schema.transform<Schema.SchemaClass<Data.Constr, Data.Constr, never>, Schema.Struct<Fields>> {}
229196

230197
/**
231198
* Creates a schema for struct types using Plutus Data Constructor
@@ -234,12 +201,22 @@ interface Struct<Fields extends Schema.Struct.Fields>
234201
* @since 2.0.0
235202
*/
236203
export const Struct = <Fields extends Schema.Struct.Fields>(fields: Fields): Struct<Fields> =>
237-
Schema.transform(Data.Constr, Schema.Struct(fields), {
204+
Schema.transform(Schema.typeSchema(Data.Constr), Schema.Struct(fields), {
238205
strict: false,
239-
encode: (obj) => Data.constr(0n, Object.values(obj) as globalThis.Array<Data.Data>),
240-
decode: (constr: { readonly index: bigint; readonly fields: ReadonlyArray<any> }) => {
206+
encode: (encodedStruct) => {
207+
// encodedStruct is the result of Schema.Struct(fields), which has already transformed all fields
208+
return new Data.Constr({
209+
index: 0n,
210+
fields: Object.values(encodedStruct)
211+
})
212+
},
213+
decode: (fromA) => {
241214
const keys = Object.keys(fields)
242-
return Object.fromEntries(keys.map((key, index) => [key, constr.fields[index]]))
215+
const result = {} as Record<string, Data.Data>
216+
keys.forEach((key, index) => {
217+
result[key] = fromA.fields[index]
218+
})
219+
return result as { [K in keyof Schema.Struct.Encoded<Fields>]: Schema.Struct.Encoded<Fields>[K] }
243220
}
244221
})
245222

packages/evolution/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export * as CommitteeColdCredential from "./CommitteeColdCredential.js"
3030
export * as CommitteeHotCredential from "./CommitteeHotCredential.js"
3131
export * as Credential from "./Credential.js"
3232
export * as Data from "./Data.js"
33-
export * as DataJson from "./DataJson.js"
33+
// export * as DataJson from "./DataJson.js" // Temporarily disabled due to cardano-multiplatform-lib dependency
3434
export * as DatumOption from "./DatumOption.js"
3535
export * as Devnet from "./Devnet/Devnet.js"
3636
export * as DevnetDefault from "./Devnet/DevnetDefault.js"

packages/evolution/test/CBOR.test.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ describe("CBOR Implementation Tests", () => {
9999
}
100100
]
101101

102-
testCases.forEach(({ description, expectedMajorType, value }) => {
102+
testCases.forEach(({ expectedMajorType, value }) => {
103103
const encoded = Codec.Encode.cborBytes(value)
104104
const firstByte = encoded[0]
105105
const majorType = firstByte >> 5
@@ -147,7 +147,6 @@ describe("CBOR Implementation Tests", () => {
147147
it("should handle arrays with length just over 4GB threshold", () => {
148148
// Test for 8-byte length encoding - using a smaller test due to memory constraints
149149
// This verifies the 8-byte length support is in place
150-
const largeLength = 4294967297 // 2^32 + 1
151150
// Create a smaller version to actually test without memory issues
152151
const testArray = Array.from({ length: 100 }, (_, i) => BigInt(i))
153152

@@ -182,7 +181,7 @@ describe("CBOR Implementation Tests", () => {
182181
const decoded = Codec.Decode.cborBytes(encoded) as Map<bigint, string>
183182
expect(decoded).toEqual(map65536)
184183
expect(decoded.size).toBe(65536)
185-
})
184+
}, 30000) // 30 second timeout for large map test
186185
})
187186
})
188187

@@ -203,7 +202,7 @@ describe("CBOR Implementation Tests", () => {
203202
{ name: "8-byte encoding (4294967296+)", value: 4294967296n }
204203
]
205204

206-
testCases.forEach(({ name, value }) => {
205+
testCases.forEach(({ value }) => {
207206
const encoded = Codec.Encode.cborBytes(value)
208207
const decoded = Codec.Decode.cborBytes(encoded)
209208
expect(decoded).toBe(value)
@@ -230,7 +229,7 @@ describe("CBOR Implementation Tests", () => {
230229
}
231230
]
232231

233-
boundaryTestCases.forEach(({ name, value }) => {
232+
boundaryTestCases.forEach(({ value }) => {
234233
const encoded = Codec.Encode.cborBytes(value)
235234
const decoded = Codec.Decode.cborBytes(encoded)
236235
expect(decoded).toBe(value)
@@ -741,7 +740,7 @@ describe("CBOR Implementation Tests", () => {
741740
}
742741
]
743742

744-
it.each(taggedTestCases)("should handle $description correctly", ({ description, hex }) => {
743+
it.each(taggedTestCases)("should handle $description correctly", ({ hex }) => {
745744
expect(() => {
746745
const decodedValue = Codec.Decode.cborHex(hex)
747746
expect(decodedValue).toBeDefined()

packages/evolution/test/Data.golden.test.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -115,17 +115,6 @@ interface GoldenEntry {
115115
}
116116
}
117117

118-
/**
119-
* Summary information for golden test data
120-
*
121-
*/
122-
interface GoldenSummary {
123-
readonly seed: number
124-
readonly samplesPerType: number
125-
readonly types: ReadonlyArray<string>
126-
readonly totalSamples: number
127-
}
128-
129118
/**
130119
* Reviver function for parsing JSON with BigInt support
131120
*
@@ -359,7 +348,7 @@ describe("Data Golden Tests", () => {
359348
const encoded = Codec.Encode.cborHex(plutusData)
360349
expect(encoded, `Failed at sample index ${entry.index}`).toBe(entry.cborHex)
361350
})
362-
})
351+
}, 30000) // 30 second timeout for large test cases
363352

364353
it("should encode lists to CBOR bytes consistently", () => {
365354
const testCases = getTestCases("list", "encoding")

0 commit comments

Comments
 (0)