feat(research): download exact saved revisions (RP26)
This commit is contained in:
parent
6ce3b68b3d
commit
5434950263
7 changed files with 320 additions and 3 deletions
|
|
@ -1,4 +1,13 @@
|
|||
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, test } from 'bun:test';
|
||||
import {
|
||||
afterAll,
|
||||
afterEach,
|
||||
beforeAll,
|
||||
beforeEach,
|
||||
describe,
|
||||
expect,
|
||||
spyOn,
|
||||
test,
|
||||
} from 'bun:test';
|
||||
import { GlobalWindow } from 'happy-dom';
|
||||
import { act, createElement } from 'react';
|
||||
import type { Root } from 'react-dom/client';
|
||||
|
|
@ -281,6 +290,192 @@ afterAll(async () => {
|
|||
});
|
||||
|
||||
describe('research workspace', () => {
|
||||
describe('saved revision download', () => {
|
||||
const blobs: Blob[] = [];
|
||||
const downloads: string[] = [];
|
||||
const revoked: string[] = [];
|
||||
let failure: 'url' | 'click' | undefined;
|
||||
let restore: () => void;
|
||||
const tick = () => new Promise(resolve => setTimeout(resolve, 0));
|
||||
beforeEach(() => {
|
||||
blobs.length = downloads.length = revoked.length = 0;
|
||||
failure = undefined;
|
||||
const createUrl = spyOn(URL, 'createObjectURL').mockImplementation(blob => {
|
||||
if (failure === 'url') {
|
||||
throw new Error('Browser file creation failed');
|
||||
}
|
||||
blobs.push(blob as Blob);
|
||||
return `blob:research-${blobs.length}`;
|
||||
});
|
||||
const revokeUrl = spyOn(URL, 'revokeObjectURL').mockImplementation(url => revoked.push(url));
|
||||
const clickAnchor = spyOn(browser.HTMLAnchorElement.prototype, 'click').mockImplementation(
|
||||
() => {
|
||||
if (failure === 'click') {
|
||||
throw new Error('Browser download failed');
|
||||
}
|
||||
const anchor = browser.document.querySelector('a[download]');
|
||||
if (!(anchor instanceof browser.HTMLAnchorElement)) {
|
||||
throw new Error('Missing download anchor');
|
||||
}
|
||||
downloads.push(anchor.download);
|
||||
}
|
||||
);
|
||||
restore = () => {
|
||||
createUrl.mockRestore();
|
||||
revokeUrl.mockRestore();
|
||||
clickAnchor.mockRestore();
|
||||
};
|
||||
});
|
||||
afterEach(async () => {
|
||||
await tick();
|
||||
restore();
|
||||
});
|
||||
|
||||
test.each(['hypothesis', 'note', 'finding'] as const)(
|
||||
'%s download excludes unsaved edits and issues no requests',
|
||||
async kind => {
|
||||
const value = kind === 'hypothesis' ? hypothesis() : artifact('a1', { kind });
|
||||
await mount(
|
||||
`/research/p1?${kind === 'hypothesis' ? 'hypothesis' : 'artifact'}=${value.id}`
|
||||
);
|
||||
expect(host.textContent).not.toContain('Download saved revision');
|
||||
await flush(() => replyProject(project(), [hypothesis()], [artifact()]));
|
||||
await flush(() => {
|
||||
if ('hypothesisId' in value.latestRevision) {
|
||||
replyHypothesis(value as ResearchHypothesis);
|
||||
} else {
|
||||
replyArtifact(value as ResearchArtifact);
|
||||
}
|
||||
});
|
||||
const original = structuredClone(value.latestRevision);
|
||||
await flush(() => click('Edit latest revision'));
|
||||
await flush(() => edit('Revision body', 'Unsaved research interpretation'));
|
||||
const before = requests.length;
|
||||
await flush(() => click('Download saved revision'));
|
||||
expect(host.textContent).toContain('Unsaved edits are excluded');
|
||||
expect(JSON.parse(await blobs[0]!.text())).toEqual({
|
||||
kind: 'research-revision-export/v1',
|
||||
projectId: 'p1',
|
||||
entry: {
|
||||
id: value.id,
|
||||
kind,
|
||||
...(kind !== 'hypothesis' ? { hypothesisRevisionId: null } : {}),
|
||||
},
|
||||
revision: original,
|
||||
});
|
||||
expect(downloads).toEqual([`research-${value.id}-${original.id}.json`]);
|
||||
expect(field('Revision body').value).toBe('Unsaved research interpretation');
|
||||
expect(value.latestRevision).toEqual(original);
|
||||
expect(requests).toHaveLength(before);
|
||||
expect(browser.document.querySelector('a[download]')).toBeNull();
|
||||
await tick();
|
||||
expect(revoked).toEqual(['blob:research-1']);
|
||||
await flush(() => click('Save new revision'));
|
||||
expect(button('Download saved revision').disabled).toBe(true);
|
||||
await flush(() => click('Download saved revision'));
|
||||
expect(blobs).toHaveLength(1);
|
||||
}
|
||||
);
|
||||
|
||||
test('historical archived finding exports its own source, raw values and unavailable reasons', async () => {
|
||||
const original = {
|
||||
...artifact().latestRevision,
|
||||
title: 'Original interpretation',
|
||||
source: {
|
||||
kind: 'experiment/v1' as const,
|
||||
experimentId: '22222222-2222-4222-8222-222222222222',
|
||||
previewHash: 'a'.repeat(64),
|
||||
},
|
||||
evidenceState: 'referenced' as const,
|
||||
evidence: [0.123456789012345, null].map((value, index) => ({
|
||||
runId: 'run',
|
||||
backtestId: 'backtest',
|
||||
strategyVersionId: 'version',
|
||||
metricsVersion: 'canonical-analytics/v1' as const,
|
||||
metricPath: index ? 'analytics.metrics.sharpeRatio' : 'analytics.metrics.totalReturn',
|
||||
value,
|
||||
unit: 'fraction' as const,
|
||||
availability: index ? ('unavailable' as const) : ('available' as const),
|
||||
unavailableReason: index ? 'insufficient-data' : null,
|
||||
resultKind: 'canonical' as const,
|
||||
financialEngine: 'rust' as const,
|
||||
datasetHash: 'b'.repeat(64),
|
||||
limitations: [{ code: 'fixture', message: 'Retained limitation' }],
|
||||
})),
|
||||
};
|
||||
const latest = {
|
||||
...original,
|
||||
id: 'a1-r2',
|
||||
ordinal: 2,
|
||||
title: 'Latest interpretation',
|
||||
source: null,
|
||||
};
|
||||
const value = artifact('a1', {
|
||||
kind: 'finding',
|
||||
archived: true,
|
||||
hypothesisRevisionId: 'h1-r1',
|
||||
latestRevision: latest,
|
||||
});
|
||||
await mount('/research/p1?artifact=a1&revision=a1-r1');
|
||||
await flush(() => replyProject(project('p1', { archived: true }), [hypothesis()], [value]));
|
||||
await flush(() => replyArtifact(value, [original, latest]));
|
||||
const before = requests.length;
|
||||
await flush(() => click('Download saved revision'));
|
||||
expect(JSON.parse(await blobs[0]!.text())).toEqual({
|
||||
kind: 'research-revision-export/v1',
|
||||
projectId: 'p1',
|
||||
entry: { id: 'a1', kind: 'finding', hypothesisRevisionId: 'h1-r1' },
|
||||
revision: original,
|
||||
});
|
||||
expect(downloads).toEqual(['research-a1-a1-r1.json']);
|
||||
expect(requests).toHaveLength(before);
|
||||
expect(value.latestRevision).toEqual(latest);
|
||||
});
|
||||
|
||||
test.each(['url', 'click'] as const)(
|
||||
'%s failure retries the saved revision with cleanup',
|
||||
async stage => {
|
||||
await mount('/research/p1?artifact=a1');
|
||||
await flush(() => replyProject());
|
||||
await flush(() => replyArtifact());
|
||||
failure = stage;
|
||||
const before = requests.length;
|
||||
await flush(() => click('Download saved revision'));
|
||||
expect(host.textContent).toContain(
|
||||
stage === 'url' ? 'Browser file creation failed' : 'Browser download failed'
|
||||
);
|
||||
expect(downloads).toHaveLength(0);
|
||||
expect(browser.document.querySelector('a[download]')).toBeNull();
|
||||
await tick();
|
||||
expect(revoked).toEqual(stage === 'url' ? [] : ['blob:research-1']);
|
||||
failure = undefined;
|
||||
await flush(() => click('Retry saved revision download'));
|
||||
expect(downloads).toHaveLength(1);
|
||||
expect(JSON.parse(await blobs.at(-1)!.text()).revision).toEqual(artifact().latestRevision);
|
||||
expect(requests).toHaveLength(before);
|
||||
await flush(() => click('View revision 1'));
|
||||
await flush(() => replyArtifact());
|
||||
expect(host.textContent).not.toContain('Retry saved revision download');
|
||||
}
|
||||
);
|
||||
|
||||
test('missing or failed history exposes no download action', async () => {
|
||||
await mount('/research/p1?artifact=a1&revision=missing');
|
||||
await flush(() => replyProject());
|
||||
await flush(() => replyArtifact());
|
||||
expect(host.textContent).toContain('requested revision does not belong');
|
||||
expect(host.textContent).not.toContain('Download saved revision');
|
||||
await flush(() => click('Retry research entry'));
|
||||
await flush(() => {
|
||||
reply(request('analysis-artifacts/a1'), { error: 'Entry unavailable' }, 503);
|
||||
reply(request('analysis-artifacts/a1/revisions'), { revisions: [] });
|
||||
});
|
||||
expect(host.textContent).toContain('Entry unavailable');
|
||||
expect(host.textContent).not.toContain('Download saved revision');
|
||||
expect(blobs).toHaveLength(0);
|
||||
});
|
||||
});
|
||||
|
||||
test('project listing distinguishes loading, error and empty and retries reads', async () => {
|
||||
await mount('/research');
|
||||
expect(host.textContent).toContain('Loading research projects');
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue