feat(research): download original canonical run results (RP08)
This commit is contained in:
parent
8e9d0cf619
commit
e8a6b70968
8 changed files with 245 additions and 13 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, type ReactElement } from 'react';
|
||||
import type { Root } from 'react-dom/client';
|
||||
|
|
@ -755,6 +764,119 @@ describe('canonical research workbench', () => {
|
|||
expect(requests).toHaveLength(1);
|
||||
});
|
||||
|
||||
describe('canonical run result download', () => {
|
||||
const blobs: Blob[] = [];
|
||||
const downloads: { href: string; filename: 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:run-${blobs.length}`;
|
||||
});
|
||||
const revokeUrl = spyOn(URL, 'revokeObjectURL').mockImplementation(url => {
|
||||
revoked.push(url);
|
||||
});
|
||||
const anchorClick = 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('Download anchor was not attached');
|
||||
}
|
||||
downloads.push({ href: anchor.href, filename: anchor.download });
|
||||
}
|
||||
);
|
||||
restore = () => {
|
||||
createUrl.mockRestore();
|
||||
revokeUrl.mockRestore();
|
||||
anchorClick.mockRestore();
|
||||
};
|
||||
});
|
||||
afterEach(async () => {
|
||||
await tick();
|
||||
restore();
|
||||
});
|
||||
|
||||
test('saves the complete original result without rounding, mutation or another request', async () => {
|
||||
const value = report();
|
||||
const before = structuredClone(value);
|
||||
await mount(createElement(CanonicalResults, { result: value }));
|
||||
expect(downloads).toHaveLength(0);
|
||||
await flush(() => click('Download run result'));
|
||||
expect(downloads).toEqual([{ href: 'blob:run-1', filename: 'run-run.json' }]);
|
||||
expect(blobs[0]!.type.split(';')[0]).toBe('application/json');
|
||||
expect(JSON.parse(await blobs[0]!.text())).toEqual(before);
|
||||
expect(value).toEqual(before);
|
||||
expect(browser.document.querySelector('a[download]')).toBeNull();
|
||||
await tick();
|
||||
expect(revoked).toEqual(['blob:run-1']);
|
||||
|
||||
const other = report();
|
||||
other.runId = other.request.runId = 'another-run';
|
||||
other.request.name = 'Other retained inputs';
|
||||
await flush(() => root!.render(createElement(CanonicalResults, { result: other })));
|
||||
await flush(() => click('Download run result'));
|
||||
expect(downloads[1]!.filename).toBe('run-another-run.json');
|
||||
expect(JSON.parse(await blobs[1]!.text())).toEqual(other);
|
||||
expect(requests).toHaveLength(0);
|
||||
await flush(() =>
|
||||
root!.render(createElement(CanonicalResults, { result: other, fillsOnly: true }))
|
||||
);
|
||||
expect(host.textContent).not.toContain('Download run result');
|
||||
expect(host.textContent).not.toContain('Retry run result download');
|
||||
});
|
||||
|
||||
for (const stage of ['url', 'click'] as const) {
|
||||
test(`reports ${stage} failure inline and retries the unchanged result with cleanup`, async () => {
|
||||
const value = report();
|
||||
await mount(createElement(CanonicalResults, { result: value }));
|
||||
failure = stage;
|
||||
await flush(() => click('Download run result'));
|
||||
expect(host.querySelector('[role="alert"]')?.textContent).toBe(
|
||||
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:run-1']);
|
||||
failure = undefined;
|
||||
await flush(() => click('Retry run result download'));
|
||||
expect(host.querySelector('[role="alert"]')).toBeNull();
|
||||
expect(downloads).toHaveLength(1);
|
||||
expect(JSON.parse(await blobs.at(-1)!.text())).toEqual(value);
|
||||
expect(requests).toHaveLength(0);
|
||||
await tick();
|
||||
expect(revoked).toEqual(blobs.map((_, index) => `blob:run-${index + 1}`));
|
||||
});
|
||||
}
|
||||
|
||||
test('a different displayed run clears the earlier local download error', async () => {
|
||||
await mount(createElement(CanonicalResults, { result: report() }));
|
||||
failure = 'click';
|
||||
await flush(() => click('Download run result'));
|
||||
expect(host.querySelector('[role="alert"]')?.textContent).toBe('Browser download failed');
|
||||
const other = report();
|
||||
other.runId = other.request.runId = 'next-run';
|
||||
await flush(() => root!.render(createElement(CanonicalResults, { result: other })));
|
||||
expect(host.querySelector('[role="alert"]')).toBeNull();
|
||||
failure = undefined;
|
||||
await flush(() => click('Download run result'));
|
||||
expect(downloads[0]!.filename).toBe('run-next-run.json');
|
||||
expect(JSON.parse(await blobs.at(-1)!.text())).toEqual(other);
|
||||
expect(requests).toHaveLength(0);
|
||||
});
|
||||
});
|
||||
|
||||
test('explains a partial-session evaluation horizon alongside the unchanged retained CAGR', async () => {
|
||||
const value = report();
|
||||
value.financial.reporting.tradeStart = Date.parse('2024-10-01T14:30:00Z');
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue