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 6174eb6
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 24 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,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))(
Expand All @@ -19,20 +31,25 @@ 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);
},
);
});

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);
});
});
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 All @@ -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);
}
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 6174eb6

Please sign in to comment.