diff --git a/src/seed_tools/README.md b/src/seed_tools/README.md index 27002629..340fd600 100644 --- a/src/seed_tools/README.md +++ b/src/seed_tools/README.md @@ -76,16 +76,16 @@ npm run seed_tools -- split_seed_json - ``: The directory where the individual study files will be outputted. -### `validate_seed_pb` +### `validate_seed` Validates a seed protobuf. ##### Syntax ```bash -npm run seed_tools -- validate_seed_pb +npm run seed_tools -- validate_seed ``` ##### Arguments -- ``: The path to the binary-serialized `seed` protobuf. +- ``: The path to the binary-serialized `seed` protobuf. diff --git a/src/seed_tools/commands/validate_seed_pb.test.ts b/src/seed_tools/commands/validate_seed.test.ts similarity index 52% rename from src/seed_tools/commands/validate_seed_pb.test.ts rename to src/seed_tools/commands/validate_seed.test.ts index 46d2b0d8..1514119d 100644 --- a/src/seed_tools/commands/validate_seed_pb.test.ts +++ b/src/seed_tools/commands/validate_seed.test.ts @@ -6,11 +6,23 @@ import * as fs_sync from 'fs'; import * as path from 'path'; import { wsPath } from '../../base/path_utils'; -import validate_seed_pb from './validate_seed_pb'; +import validate_seed from './validate_seed'; -describe('validate_seed_pb command', () => { +describe('validate_seed command', () => { const testDataDir = wsPath('//src/test/data'); + let errorMock: jest.SpyInstance; + let exitMock: jest.SpyInstance; + + beforeEach(async () => { + errorMock = jest.spyOn(console, 'error').mockImplementation(); + exitMock = jest.spyOn(process, 'exit').mockImplementation(); + }); + + afterEach(async () => { + jest.restoreAllMocks(); + }); + describe('valid seeds', () => { const validSeedsDir = path.join(testDataDir, 'valid_seeds'); it.each(fs_sync.readdirSync(validSeedsDir))( @@ -19,11 +31,10 @@ describe('validate_seed_pb command', () => { const testCaseDir = path.join(validSeedsDir, testCase); const seedBin = path.join(testCaseDir, 'expected_seed.bin'); - await validate_seed_pb.parseAsync([ - 'node', - 'validate_seed_pb', - seedBin, - ]); + await validate_seed.parseAsync(['node', 'validate_seed', seedBin]); + + expect(errorMock).toHaveBeenCalledTimes(0); + expect(exitMock).toHaveBeenCalledWith(0); }, ); }); @@ -31,8 +42,14 @@ describe('validate_seed_pb command', () => { test('invalid seed', async () => { const seedBin = path.join(testDataDir, 'invalid_seed.bin'); expect(fs_sync.existsSync(seedBin)).toBe(true); - await expect( - validate_seed_pb.parseAsync(['node', 'validate_seed_pb', seedBin]), - ).rejects.toThrow('premature EOF'); + + await validate_seed.parseAsync(['node', 'validate_seed', seedBin]); + + expect(errorMock).toHaveBeenCalledWith( + expect.stringContaining( + 'Total probability is not 100 for study AllowCertainClientHintsStudy', + ), + ); + expect(exitMock).toHaveBeenCalledWith(1); }); }); diff --git a/src/seed_tools/commands/validate_seed_pb.ts b/src/seed_tools/commands/validate_seed.ts similarity index 76% rename from src/seed_tools/commands/validate_seed_pb.ts rename to src/seed_tools/commands/validate_seed.ts index cbbd9a21..f8d28de8 100644 --- a/src/seed_tools/commands/validate_seed_pb.ts +++ b/src/seed_tools/commands/validate_seed.ts @@ -9,14 +9,14 @@ import { VariationsSeed } from '../../proto/generated/variations_seed'; import * as seed_validation from '../utils/seed_validation'; import * as study_validation from '../utils/study_validation'; -export default new Command('validate_seed_pb') +export default new Command('validate_seed') .description('Validates seed.bin') - .argument('', 'path to a seed protobuf') + .argument('', 'path to a seed protobuf') .action(main); -async function main(seedBin: string) { +async function main(seedFilePath: string) { const variationsSeed = VariationsSeed.fromBinary( - await fs.readFile(seedBin, { encoding: null }), + await fs.readFile(seedFilePath, { encoding: null }), ); const errors = []; for (const study of variationsSeed.study) { @@ -37,11 +37,8 @@ async function main(seedBin: string) { console.log('Seed studies count:', variationsSeed.study.length); if (errors.length > 0) { - console.log('Seed validation errors:'); - for (const error of errors) { - console.log(error); - } + console.error(`Seed validation errors:\n${errors.join('\n---\n')}`); } - process.exitCode = errors.length > 0 ? 1 : 0; + process.exit(errors.length > 0 ? 1 : 0); } diff --git a/src/seed_tools/seed_tools.ts b/src/seed_tools/seed_tools.ts index 937a74bd..3a43331e 100644 --- a/src/seed_tools/seed_tools.ts +++ b/src/seed_tools/seed_tools.ts @@ -9,7 +9,7 @@ import check_study from './commands/check_study'; import compare_seeds from './commands/compare_seeds'; import create_seed from './commands/create_seed'; import split_seed_json from './commands/split_seed_json'; -import validate_seed_pb from './commands/validate_seed_pb'; +import validate_seed from './commands/validate_seed'; program .name('seed_tools') @@ -18,5 +18,5 @@ program .addCommand(compare_seeds) .addCommand(create_seed) .addCommand(split_seed_json) - .addCommand(validate_seed_pb) + .addCommand(validate_seed) .parse(); diff --git a/src/test/data/invalid_seed.bin b/src/test/data/invalid_seed.bin index f2ba8f84..988b4dd5 100644 Binary files a/src/test/data/invalid_seed.bin and b/src/test/data/invalid_seed.bin differ