Skip to content

Commit 52e6556

Browse files
committed
refactor(wasm-sdk): make data contract test matrix extensible
Refactor platform version compatibility test configuration to be data-driven and auto-generate test suites. Makes adding new contract format versions easier by centralizing configuration.
1 parent f49390f commit 52e6556

File tree

1 file changed

+57
-14
lines changed

1 file changed

+57
-14
lines changed

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

Lines changed: 57 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,54 @@ import init, * as sdk from '../../dist/sdk.compressed.js';
22
import contractFixtureV0 from './fixtures/data-contract-v0-crypto-card-game.mjs';
33
import contractFixtureV1 from './fixtures/data-contract-v1-with-docs-tokens-groups.mjs';
44

5-
// Platform version constants
6-
const PLATFORM_VERSION_CONTRACT_V0 = 1;
7-
const PLATFORM_VERSION_CONTRACT_V1 = 9; // V1 contracts introduced in Platform v9
5+
// Platform version configuration
6+
const PLATFORM_VERSIONS = {
7+
MIN: 1,
8+
MAX: 10, // Update as new platform versions are released
9+
};
10+
11+
// Contract format version configuration
12+
// To add a new format version, add an entry here with:
13+
// - The Platform version when introduced
14+
// - A fixture for the new format
15+
// Example: V2: { introduced: 12, fixture: contractFixtureV2 }
16+
const CONTRACT_FORMAT_VERSIONS = {
17+
V0: { introduced: 1, fixture: contractFixtureV0 },
18+
V1: { introduced: 9, fixture: contractFixtureV1 },
19+
};
20+
21+
// Auto-generate compatibility data for all formats
22+
const FORMATS = Object.entries(CONTRACT_FORMAT_VERSIONS).reduce((acc, [formatKey, config]) => {
23+
const compatibleVersions = Array.from(
24+
{ length: PLATFORM_VERSIONS.MAX - config.introduced + 1 },
25+
(_, i) => i + config.introduced
26+
);
27+
28+
const allVersions = Array.from(
29+
{ length: PLATFORM_VERSIONS.MAX - PLATFORM_VERSIONS.MIN + 1 },
30+
(_, i) => i + PLATFORM_VERSIONS.MIN
31+
);
32+
33+
const incompatibleVersions = allVersions.filter(v => v < config.introduced);
834

9-
// Platform version compatibility ranges
10-
const V0_COMPATIBLE_VERSIONS = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; // V0 works across all versions
11-
const V1_COMPATIBLE_VERSIONS = [9, 10]; // V1 only works from version 9+
12-
const V0_ONLY_VERSIONS = [1, 2, 3, 4, 5, 6, 7, 8]; // Versions that only support V0
13-
const LATEST_KNOWN_VERSION = Math.max(...V0_COMPATIBLE_VERSIONS);
35+
acc[formatKey] = {
36+
...config,
37+
compatibleVersions,
38+
incompatibleVersions,
39+
platformVersion: config.introduced,
40+
};
41+
42+
return acc;
43+
}, {});
44+
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+
52+
const LATEST_KNOWN_VERSION = PLATFORM_VERSIONS.MAX;
1453

1554
// Helper function for testing contract compatibility across versions
1655
const testContractAcrossVersions = (
@@ -242,12 +281,16 @@ describe('DataContract', () => {
242281
});
243282

244283
describe('Platform Version Compatibility Matrix', () => {
245-
describe('V0 Contract Compatibility', () => {
246-
testContractAcrossVersions(contractFixtureV0, 'V0', V0_COMPATIBLE_VERSIONS);
247-
});
248-
249-
describe('V1 Contract Compatibility', () => {
250-
testContractAcrossVersions(contractFixtureV1, 'V1', V1_COMPATIBLE_VERSIONS, V0_ONLY_VERSIONS);
284+
// Dynamically test all defined contract formats
285+
Object.entries(FORMATS).forEach(([formatKey, formatData]) => {
286+
describe(`${formatKey} Contract Compatibility`, () => {
287+
testContractAcrossVersions(
288+
formatData.fixture,
289+
formatKey,
290+
formatData.compatibleVersions,
291+
formatData.incompatibleVersions
292+
);
293+
});
251294
});
252295

253296
describe('Edge Cases', () => {

0 commit comments

Comments
 (0)