Skip to content

Commit

Permalink
Rename validate_seed_pb to validate_seed, update tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
goodov committed Aug 20, 2024
1 parent 7c7178a commit 2320048
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 19 deletions.
6 changes: 3 additions & 3 deletions src/seed_tools/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,16 @@ npm run seed_tools -- split_seed_json <seed_json_path> <output_dir>
- `<output_dir>`: 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 <seed_bin>
npm run seed_tools -- validate_seed <seed_file>
```

##### Arguments

- `<seed_bin>`: The path to the binary-serialized `seed` protobuf.
- `<seed_file>`: The path to the binary-serialized `seed` protobuf.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
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');

describe('valid seeds', () => {
Expand All @@ -19,20 +19,36 @@ 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]);
},
);
});

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');

const consoleLogMock = jest
.spyOn(console, 'log')
.mockImplementation(() => {});

let exitCodeMock = 0;
jest
.spyOn(process, 'exitCode', 'get')
.mockImplementation(() => exitCodeMock);
jest
.spyOn(process, 'exitCode', 'set')
.mockImplementation((code: number) => {
exitCodeMock = code;
});

await validate_seed.parseAsync(['node', 'validate_seed', seedBin]);

expect(consoleLogMock).toHaveBeenCalledWith(
'Total probability is not 100 for study AllowCertainClientHintsStudy',
);
expect(exitCodeMock).toBe(1);

jest.restoreAllMocks();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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('<seed_bin>', 'path to a seed protobuf')
.argument('<seed_file>', '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) {
Expand Down
4 changes: 2 additions & 2 deletions src/seed_tools/seed_tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -18,5 +18,5 @@ program
.addCommand(compare_seeds)
.addCommand(create_seed)
.addCommand(split_seed_json)
.addCommand(validate_seed_pb)
.addCommand(validate_seed)
.parse();
Binary file modified src/test/data/invalid_seed.bin
Binary file not shown.

0 comments on commit 2320048

Please sign in to comment.