diff --git a/.gitignore b/.gitignore index 2168e098..e0df141b 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ seed.bin serialnumber +*.json.failed diff --git a/src/seed_tools/commands/create.test.ts b/src/seed_tools/commands/create.test.ts index af7984c5..895151d0 100644 --- a/src/seed_tools/commands/create.test.ts +++ b/src/seed_tools/commands/create.test.ts @@ -11,6 +11,26 @@ import { VariationsSeed } from 'src/proto/generated/variations_seed'; import { wsPath } from '../../base/path_utils'; import create from './create'; +// Helper function to compare protobuf with expected json. +const compareProtobuf = (actual: Uint8Array, expectedFilename: string) => { + let expectedJson = '{}'; + if (fs_sync.existsSync(expectedFilename)) { + expectedJson = fs_sync.readFileSync(expectedFilename, 'utf-8'); + } + const expected = VariationsSeed.fromJsonString(expectedJson); + const actualObj = VariationsSeed.fromBinary(actual); + try { + expect(actualObj).toEqual(expected); + } catch (error) { + const failedFilename = expectedFilename + '.failed'; + fs_sync.writeFileSync( + failedFilename, + VariationsSeed.toJsonString(actualObj, { prettySpaces: 2 }) + '\n', + ); + throw error; + } +}; + describe('create command', () => { const testDataDir = wsPath('//src/test/data'); @@ -57,10 +77,7 @@ describe('create command', () => { ]); const output = await fs.readFile(outputFile); - const expectedOutput = await fs.readFile( - path.join(testCaseDir, 'expected_seed.bin'), - ); - expect(output).toEqual(expectedOutput); + compareProtobuf(output, path.join(testCaseDir, 'expected_seed.json')); const outputSerialNumber = await fs.readFile(serialNumberPath, 'utf-8'); expect(outputSerialNumber).toEqual('1'); @@ -89,10 +106,7 @@ describe('create command', () => { await create().parseAsync(args); const output = await fs.readFile(outputFile); - const expectedOutput = await fs.readFile( - path.join(testCaseDir, 'expected_seed.bin'), - ); - expect(output).toEqual(expectedOutput); + compareProtobuf(output, path.join(testCaseDir, 'expected_seed.json')); expect(VariationsSeed.fromBinary(output).version).toEqual('1'); }; @@ -125,6 +139,9 @@ describe('create command', () => { ]); const output = await fs.readFile(outputFile); + compareProtobuf(output, path.join(testCaseDir, 'expected_seed.json')); + + // Check the binary output is the same as the expected output. const expectedOutput = await fs.readFile( path.join(testCaseDir, 'expected_seed.bin'), ); diff --git a/src/test/data/perf_seeds/test1/expected_seed.bin b/src/test/data/perf_seeds/test1/expected_seed.bin deleted file mode 100644 index aa3c8238..00000000 Binary files a/src/test/data/perf_seeds/test1/expected_seed.bin and /dev/null differ diff --git a/src/test/data/perf_seeds/test1/expected_seed.json b/src/test/data/perf_seeds/test1/expected_seed.json new file mode 100644 index 00000000..a96f0405 --- /dev/null +++ b/src/test/data/perf_seeds/test1/expected_seed.json @@ -0,0 +1,36 @@ +{ + "serialNumber": "1", + "study": [ + { + "name": "TestStudy", + "consistency": "PERMANENT", + "defaultExperimentName": "Disabled", + "experiment": [ + { + "name": "Disabled", + "probabilityWeight": 100, + "featureAssociation": { + "disableFeature": [ + "TestFeature" + ] + } + } + ], + "filter": { + "channel": [ + "CANARY", + "BETA", + "STABLE" + ], + "platform": [ + "PLATFORM_WINDOWS", + "PLATFORM_MAC", + "PLATFORM_LINUX", + "PLATFORM_ANDROID" + ] + }, + "activationType": "ACTIVATE_ON_STARTUP" + } + ], + "version": "1" +} diff --git a/src/test/data/set_seed_version/expected_seed.json b/src/test/data/set_seed_version/expected_seed.json new file mode 100644 index 00000000..da0dd2df --- /dev/null +++ b/src/test/data/set_seed_version/expected_seed.json @@ -0,0 +1,49 @@ +{ + "serialNumber": "1", + "study": [ + { + "name": "TestStudy", + "consistency": "PERMANENT", + "experiment": [ + { + "name": "Enabled", + "probabilityWeight": 100, + "featureAssociation": { + "enableFeature": [ + "TestFeature" + ] + } + }, + { + "name": "Disabled", + "probabilityWeight": 0, + "featureAssociation": { + "disableFeature": [ + "TestFeature" + ] + } + }, + { + "name": "Default", + "probabilityWeight": 0 + } + ], + "filter": { + "minVersion": "92.1.30.57", + "channel": [ + "CANARY", + "BETA", + "STABLE" + ], + "platform": [ + "PLATFORM_WINDOWS", + "PLATFORM_MAC", + "PLATFORM_LINUX", + "PLATFORM_ANDROID" + ] + }, + "activationType": "ACTIVATE_ON_STARTUP" + } + ], + "version": "test version value" +} diff --git a/src/test/data/valid_seeds/test1/expected_seed.bin b/src/test/data/valid_seeds/test1/expected_seed.bin deleted file mode 100644 index f849a771..00000000 Binary files a/src/test/data/valid_seeds/test1/expected_seed.bin and /dev/null differ diff --git a/src/test/data/valid_seeds/test1/expected_seed.json b/src/test/data/valid_seeds/test1/expected_seed.json new file mode 100644 index 00000000..07a85494 --- /dev/null +++ b/src/test/data/valid_seeds/test1/expected_seed.json @@ -0,0 +1,49 @@ +{ + "serialNumber": "1", + "study": [ + { + "name": "TestStudy", + "consistency": "PERMANENT", + "experiment": [ + { + "name": "Enabled", + "probabilityWeight": 100, + "featureAssociation": { + "enableFeature": [ + "TestFeature" + ] + } + }, + { + "name": "Disabled", + "probabilityWeight": 0, + "featureAssociation": { + "disableFeature": [ + "TestFeature" + ] + } + }, + { + "name": "Default", + "probabilityWeight": 0 + } + ], + "filter": { + "minVersion": "92.1.30.57", + "channel": [ + "CANARY", + "BETA", + "STABLE" + ], + "platform": [ + "PLATFORM_WINDOWS", + "PLATFORM_MAC", + "PLATFORM_LINUX", + "PLATFORM_ANDROID" + ] + }, + "activationType": "ACTIVATE_ON_STARTUP" + } + ], + "version": "1" +} diff --git a/src/test/data/valid_seeds/test2/expected_seed.bin b/src/test/data/valid_seeds/test2/expected_seed.bin deleted file mode 100644 index ab88897c..00000000 Binary files a/src/test/data/valid_seeds/test2/expected_seed.bin and /dev/null differ diff --git a/src/test/data/valid_seeds/test2/expected_seed.json b/src/test/data/valid_seeds/test2/expected_seed.json new file mode 100644 index 00000000..d432eef3 --- /dev/null +++ b/src/test/data/valid_seeds/test2/expected_seed.json @@ -0,0 +1,87 @@ +{ + "serialNumber": "1", + "study": [ + { + "name": "AnotherTestStudy", + "consistency": "PERMANENT", + "experiment": [ + { + "name": "Enabled_Group", + "probabilityWeight": 0, + "featureAssociation": { + "enableFeature": [ + "AnotherTestFeature" + ] + } + }, + { + "name": "Disabled_Group", + "probabilityWeight": 100, + "featureAssociation": { + "disableFeature": [ + "AnotherTestFeature" + ] + } + }, + { + "name": "Default_Group", + "probabilityWeight": 0 + } + ], + "filter": { + "minVersion": "120.*", + "channel": [ + "STABLE" + ], + "platform": [ + "PLATFORM_MAC" + ] + }, + "activationType": "ACTIVATE_ON_STARTUP" + }, + { + "name": "TestStudy", + "consistency": "PERMANENT", + "experiment": [ + { + "name": "Enabled", + "probabilityWeight": 100, + "featureAssociation": { + "enableFeature": [ + "TestFeature" + ] + } + }, + { + "name": "Disabled", + "probabilityWeight": 0, + "featureAssociation": { + "disableFeature": [ + "TestFeature" + ] + } + }, + { + "name": "Default", + "probabilityWeight": 0 + } + ], + "filter": { + "minVersion": "92.1.30.57", + "channel": [ + "CANARY", + "BETA", + "STABLE" + ], + "platform": [ + "PLATFORM_WINDOWS", + "PLATFORM_MAC", + "PLATFORM_LINUX", + "PLATFORM_ANDROID" + ] + }, + "activationType": "ACTIVATE_ON_STARTUP" + } + ], + "version": "1" +} diff --git a/src/test/data/valid_seeds/test3/expected_seed.bin b/src/test/data/valid_seeds/test3/expected_seed.bin deleted file mode 100644 index 65310248..00000000 Binary files a/src/test/data/valid_seeds/test3/expected_seed.bin and /dev/null differ diff --git a/src/test/data/valid_seeds/test3/expected_seed.json b/src/test/data/valid_seeds/test3/expected_seed.json new file mode 100644 index 00000000..35ff3baa --- /dev/null +++ b/src/test/data/valid_seeds/test3/expected_seed.json @@ -0,0 +1,125 @@ +{ + "serialNumber": "1", + "study": [ + { + "name": "AnotherTestStudy", + "consistency": "PERMANENT", + "experiment": [ + { + "name": "Enabled_Group", + "probabilityWeight": 0, + "featureAssociation": { + "enableFeature": [ + "AnotherTestFeature" + ] + } + }, + { + "name": "Disabled_Group", + "probabilityWeight": 100, + "featureAssociation": { + "disableFeature": [ + "AnotherTestFeature" + ] + } + }, + { + "name": "Default_Group", + "probabilityWeight": 0 + } + ], + "filter": { + "minVersion": "120.*", + "channel": [ + "STABLE" + ], + "platform": [ + "PLATFORM_MAC" + ] + }, + "activationType": "ACTIVATE_ON_STARTUP" + }, + { + "name": "AnotherTestStudy", + "consistency": "PERMANENT", + "experiment": [ + { + "name": "Enabled_Group", + "probabilityWeight": 50, + "featureAssociation": { + "enableFeature": [ + "AnotherTestFeature" + ] + } + }, + { + "name": "Disabled_Group", + "probabilityWeight": 50, + "featureAssociation": { + "disableFeature": [ + "AnotherTestFeature" + ] + } + }, + { + "name": "Default_Group", + "probabilityWeight": 0 + } + ], + "filter": { + "minVersion": "120.*", + "channel": [ + "BETA" + ], + "platform": [ + "PLATFORM_MAC" + ] + }, + "activationType": "ACTIVATE_ON_STARTUP" + }, + { + "name": "TestStudy", + "consistency": "PERMANENT", + "experiment": [ + { + "name": "Enabled", + "probabilityWeight": 100, + "featureAssociation": { + "enableFeature": [ + "TestFeature" + ] + } + }, + { + "name": "Disabled", + "probabilityWeight": 0, + "featureAssociation": { + "disableFeature": [ + "TestFeature" + ] + } + }, + { + "name": "Default", + "probabilityWeight": 0 + } + ], + "filter": { + "minVersion": "92.1.30.57", + "channel": [ + "CANARY", + "BETA", + "STABLE" + ], + "platform": [ + "PLATFORM_WINDOWS", + "PLATFORM_MAC", + "PLATFORM_LINUX", + "PLATFORM_ANDROID" + ] + }, + "activationType": "ACTIVATE_ON_STARTUP" + } + ], + "version": "1" +}