feat(research): download validated retained datasets (RP07)

This commit is contained in:
Boki 2026-10-27 10:59:19 -04:00
parent 43ea2cc9b6
commit b959779ada
14 changed files with 1076 additions and 11 deletions

View file

@ -230,6 +230,9 @@ describe.skipIf(!url)('source-export preparation with retained PostgreSQL custod
await expect(readCanonicalDataset(f.pool, prepared.hash)).rejects.toThrow(
prepared.reason ?? 'missing reason'
);
const exported = await f.routes.request(`/api/v2/datasets/canonical/${prepared.hash}/export`);
expect(exported.status).toBe(422);
expect(exported.headers.get('Content-Disposition')).toBeNull();
expect((await f.pool.query('SELECT count(*) AS n FROM runs')).rows).toEqual([{ n: '0' }]);
});
@ -255,16 +258,21 @@ describe.skipIf(!url)('source-export preparation with retained PostgreSQL custod
await expect(readCanonicalDataset(f.pool, dataset.manifest.hash)).rejects.toThrow(
'content validation'
);
const exported = await f.routes.request(
`/api/v2/datasets/canonical/${dataset.manifest.hash}/export`
);
expect(exported.status).toBe(409);
expect(exported.headers.get('Content-Disposition')).toBeNull();
expect(await f.snapshot()).toEqual(before);
});
test('existing --file CLI retains the same v2 identity and safely repeats against the web-prepared row', async () => {
test('validated export feeds the existing --file CLI with identical retained identity and no custody writes', async () => {
const f = await fixture(),
input = datasetInput(),
dataset = createDatasetV2(input);
const directory = await mkdtemp(join(tmpdir(), 'source-preparation-cli-'));
cleanup.push(() => rm(directory, { recursive: true, force: true }));
const file = join(directory, 'dataset.json');
await writeFile(file, JSON.stringify(dataset));
const connection = new URL(url ?? '');
connection.searchParams.set('options', `-c search_path=${f.schema}`);
const execute = async (args: string[]) => {
@ -288,6 +296,22 @@ describe.skipIf(!url)('source-export preparation with retained PostgreSQL custod
200
);
const before = await f.snapshot();
const reads = f.queryCount;
const exported = await f.routes.request(
`/api/v2/datasets/canonical/${dataset.manifest.hash}/export`
);
expect(exported.status).toBe(200);
expect(exported.headers.get('Content-Type')).toContain('application/json');
expect(exported.headers.get('Cache-Control')).toBe('no-store');
expect(exported.headers.get('X-Content-Type-Options')).toBe('nosniff');
expect(exported.headers.get('Content-Disposition')).toBe(
`attachment; filename="dataset-${dataset.manifest.hash}.json"`
);
const exportedText = await exported.text();
expect(validateDatasetV2(JSON.parse(exportedText))).toEqual(dataset);
expect(f.queryCount).toBe(reads + 1);
expect(await f.snapshot()).toEqual(before);
await writeFile(file, exportedText);
const cli = await execute(['--file', file]);
expect(cli.code).toBe(0);
expect(JSON.parse(cli.out)).toEqual({
@ -296,6 +320,7 @@ describe.skipIf(!url)('source-export preparation with retained PostgreSQL custod
eligibility: dataset.manifest.eligibility,
limitations: dataset.manifest.limitations,
});
expect((await execute(['--file', file])).code).toBe(0);
expect(await f.snapshot()).toEqual(before);
const flags = await execute(['--source-hash', dataset.manifest.hash]);
expect(flags.code).toBe(1);