Skip to content

Commit 4bf18f7

Browse files
committed
refactor(wasm-sdk): make contract format version compatibility fully dynamic
Replace hardcoded version arrays with auto-generated compatibility data from CONTRACT_FORMAT_VERSIONS config. Tests now dynamically iterate over all defined formats, eliminating need for manual updates when adding new contract versions.
1 parent 52e6556 commit 4bf18f7

File tree

1 file changed

+18
-25
lines changed

1 file changed

+18
-25
lines changed

packages/wasm-sdk/tests/unit/data-contract.spec.mjs

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,6 @@ const FORMATS = Object.entries(CONTRACT_FORMAT_VERSIONS).reduce((acc, [formatKey
4242
return acc;
4343
}, {});
4444

45-
// Backward compatibility: Keep existing constant names as aliases
46-
const PLATFORM_VERSION_CONTRACT_V0 = FORMATS.V0.platformVersion;
47-
const PLATFORM_VERSION_CONTRACT_V1 = FORMATS.V1.platformVersion;
48-
const V0_COMPATIBLE_VERSIONS = FORMATS.V0.compatibleVersions;
49-
const V1_COMPATIBLE_VERSIONS = FORMATS.V1.compatibleVersions;
50-
const V0_ONLY_VERSIONS = FORMATS.V0.incompatibleVersions;
51-
5245
const LATEST_KNOWN_VERSION = PLATFORM_VERSIONS.MAX;
5346

5447
// Helper function for testing contract compatibility across versions
@@ -87,7 +80,7 @@ describe('DataContract', () => {
8780

8881
describe('Contract Creation', () => {
8982
it('should create a V0 contract from JSON and expose all properties', async () => {
90-
const contract = sdk.DataContract.fromJSON(contractFixtureV0, PLATFORM_VERSION_CONTRACT_V0);
83+
const contract = sdk.DataContract.fromJSON(contractFixtureV0, FORMATS.V0.platformVersion);
9184

9285
expect(contract).to.be.ok();
9386
expect(contract.id()).to.equal(contractFixtureV0.id);
@@ -113,7 +106,7 @@ describe('DataContract', () => {
113106

114107
// TODO: enable test once an SDK fix to support this is merged
115108
it.skip('should create a V1 contract from JSON and expose all properties including tokens and groups', async () => {
116-
const contract = sdk.DataContract.fromJSON(contractFixtureV1, PLATFORM_VERSION_CONTRACT_V1);
109+
const contract = sdk.DataContract.fromJSON(contractFixtureV1, FORMATS.V1.platformVersion);
117110

118111
expect(contract).to.be.ok();
119112
expect(contract.id()).to.equal(contractFixtureV1.id);
@@ -144,7 +137,7 @@ describe('DataContract', () => {
144137

145138
it('should create a contract with only document schemas (no tokens)', () => {
146139
// V0 fixture already has only documents, no tokens - verify it works
147-
const contract = sdk.DataContract.fromJSON(contractFixtureV0, PLATFORM_VERSION_CONTRACT_V0);
140+
const contract = sdk.DataContract.fromJSON(contractFixtureV0, FORMATS.V0.platformVersion);
148141
const roundTripped = contract.toJSON();
149142

150143
expect(roundTripped.documentSchemas.card).to.exist();
@@ -162,7 +155,7 @@ describe('DataContract', () => {
162155

163156
const contract = sdk.DataContract.fromJSON(
164157
contractWithOnlyTokens,
165-
PLATFORM_VERSION_CONTRACT_V1,
158+
FORMATS.V1.platformVersion,
166159
);
167160
const roundTripped = contract.toJSON();
168161

@@ -175,23 +168,23 @@ describe('DataContract', () => {
175168
describe('Version Compatibility', () => {
176169
it('should fail to create a V1 contract with V0 platform version', async () => {
177170
expect(() => {
178-
sdk.DataContract.fromJSON(contractFixtureV1, PLATFORM_VERSION_CONTRACT_V0);
171+
sdk.DataContract.fromJSON(contractFixtureV1, FORMATS.V0.platformVersion);
179172
}).to.throw(/dpp unknown version.*known versions.*\[0\].*received.*1/);
180173
});
181174
});
182175

183176
describe('Validation', () => {
184177
it('should handle invalid JSON input gracefully', () => {
185178
expect(() => {
186-
sdk.DataContract.fromJSON(null, PLATFORM_VERSION_CONTRACT_V0);
179+
sdk.DataContract.fromJSON(null, FORMATS.V0.platformVersion);
187180
}).to.throw();
188181

189182
expect(() => {
190-
sdk.DataContract.fromJSON({}, PLATFORM_VERSION_CONTRACT_V0);
183+
sdk.DataContract.fromJSON({}, FORMATS.V0.platformVersion);
191184
}).to.throw();
192185

193186
expect(() => {
194-
sdk.DataContract.fromJSON({ id: 'invalid' }, PLATFORM_VERSION_CONTRACT_V0);
187+
sdk.DataContract.fromJSON({ id: 'invalid' }, FORMATS.V0.platformVersion);
195188
}).to.throw();
196189
});
197190

@@ -201,23 +194,23 @@ describe('DataContract', () => {
201194
sdk.DataContract.fromJSON({
202195
...contractFixtureV0,
203196
id: 'invalid-not-base58!',
204-
}, PLATFORM_VERSION_CONTRACT_V0);
197+
}, FORMATS.V0.platformVersion);
205198
}).to.throw();
206199

207200
// Test negative version number
208201
expect(() => {
209202
sdk.DataContract.fromJSON({
210203
...contractFixtureV0,
211204
version: -1,
212-
}, PLATFORM_VERSION_CONTRACT_V0);
205+
}, FORMATS.V0.platformVersion);
213206
}).to.throw();
214207

215208
// Test invalid ownerId
216209
expect(() => {
217210
sdk.DataContract.fromJSON({
218211
...contractFixtureV0,
219212
ownerId: 'not-a-valid-id',
220-
}, PLATFORM_VERSION_CONTRACT_V0);
213+
}, FORMATS.V0.platformVersion);
221214
}).to.throw();
222215
});
223216

@@ -232,18 +225,18 @@ describe('DataContract', () => {
232225
};
233226

234227
expect(() => {
235-
sdk.DataContract.fromJSON(contractWithEmptySchemas, PLATFORM_VERSION_CONTRACT_V0);
228+
sdk.DataContract.fromJSON(contractWithEmptySchemas, FORMATS.V0.platformVersion);
236229
}).to.throw(/must have at least one document type or token defined/);
237230
});
238231
});
239232

240233
describe('Data Preservation', () => {
241234
it('should preserve all data through JSON round-trip for V0 contract', async () => {
242-
const contract = sdk.DataContract.fromJSON(contractFixtureV0, PLATFORM_VERSION_CONTRACT_V0);
235+
const contract = sdk.DataContract.fromJSON(contractFixtureV0, FORMATS.V0.platformVersion);
243236
const roundTripped = contract.toJSON();
244237

245238
// Create a new contract from the round-tripped JSON
246-
const contract2 = sdk.DataContract.fromJSON(roundTripped, PLATFORM_VERSION_CONTRACT_V0);
239+
const contract2 = sdk.DataContract.fromJSON(roundTripped, FORMATS.V0.platformVersion);
247240
const roundTripped2 = contract2.toJSON();
248241

249242
expect(roundTripped2).to.deep.equal(roundTripped);
@@ -253,11 +246,11 @@ describe('DataContract', () => {
253246
});
254247

255248
it('should preserve all data through JSON round-trip for V1 contract', async () => {
256-
const contract = sdk.DataContract.fromJSON(contractFixtureV1, PLATFORM_VERSION_CONTRACT_V1);
249+
const contract = sdk.DataContract.fromJSON(contractFixtureV1, FORMATS.V1.platformVersion);
257250
const roundTripped = contract.toJSON();
258251

259252
// Create a new contract from the round-tripped JSON
260-
const contract2 = sdk.DataContract.fromJSON(roundTripped, PLATFORM_VERSION_CONTRACT_V1);
253+
const contract2 = sdk.DataContract.fromJSON(roundTripped, FORMATS.V1.platformVersion);
261254
const roundTripped2 = contract2.toJSON();
262255

263256
expect(roundTripped2).to.deep.equal(roundTripped);
@@ -269,8 +262,8 @@ describe('DataContract', () => {
269262

270263
describe('Memory Management', () => {
271264
it('should handle memory management properly with multiple contracts', async () => {
272-
const contract1 = sdk.DataContract.fromJSON(contractFixtureV0, PLATFORM_VERSION_CONTRACT_V0);
273-
const contract2 = sdk.DataContract.fromJSON(contractFixtureV1, PLATFORM_VERSION_CONTRACT_V1);
265+
const contract1 = sdk.DataContract.fromJSON(contractFixtureV0, FORMATS.V0.platformVersion);
266+
const contract2 = sdk.DataContract.fromJSON(contractFixtureV1, FORMATS.V1.platformVersion);
274267

275268
expect(contract1.id()).to.equal(contractFixtureV0.id);
276269
expect(contract2.id()).to.equal(contractFixtureV1.id);

0 commit comments

Comments
 (0)