fix(research): verify creation acknowledgements before leaving drafts (RP31)

This commit is contained in:
Boki 2026-10-28 09:27:39 -04:00
parent b09b2de406
commit 57ba86d035
8 changed files with 552 additions and 22 deletions

View file

@ -40,6 +40,7 @@ import type {
ResearchFindingSource,
ResearchHypothesis,
ResearchHypothesisRevision,
ResearchProject,
} from '@stock-bot/types';
import {
createDatasetV2,
@ -2188,6 +2189,228 @@ test('canonical_native_smoke', async () => {
}
});
test('creation_acknowledgements_preserve_proposals_and_reopen_committed_records_browser', async () => {
const f = await fixture(true);
try {
await f.login();
await createCanonicalInBrowser(f, 'Creation acknowledgement oracle', true);
const runId = f.page.url().split('/').at(-1)!;
f.dispatcher.start();
await visible(f.page, 'Canonical research result');
const native = (await f.request(`/api/v2/runs/${runId}/results`)) as CanonicalRunResult;
f.page.on('dialog', event => void event.accept());
await f.page.goto(`${f.origin}/research`);
await f.page.getByRole('button', { name: 'New project', exact: true }).click();
const projectTitle = ' Research creation custody ';
const projectDescription = ' Preserve this project draft.\n';
await f.page.getByLabel('Project title', { exact: true }).fill(projectTitle);
await f.page.getByLabel('Project description', { exact: true }).fill(projectDescription);
let projectPosts = 0;
let committedProject: ResearchProject | undefined;
const projectEndpoint = '**/api/v2/research-projects';
await f.page.route(projectEndpoint, async route => {
if (route.request().method() !== 'POST') {
await route.continue();
return;
}
projectPosts++;
const response = await route.fetch();
expect(response.status()).toBe(201);
const payload = (await response.json()) as { project: ResearchProject };
committedProject = structuredClone(payload.project);
payload.project.description = 'Substituted acknowledgement only.';
await route.fulfill({ response, json: payload });
});
await f.page.getByRole('button', { name: 'Create project', exact: true }).click();
await visible(f.page, 'The saved project response could not be verified.');
expect(await f.page.getByLabel('Project title', { exact: true }).inputValue()).toBe(
projectTitle
);
expect(await f.page.getByLabel('Project description', { exact: true }).inputValue()).toBe(
projectDescription
);
expect(
await f.page.getByRole('button', { name: 'Create project', exact: true }).isDisabled()
).toBe(true);
expect(f.page.url()).toBe(`${f.origin}/research`);
await f.page.getByRole('button', { name: 'Check saved projects', exact: true }).click();
await f.page.getByRole('link', { name: projectTitle.trim(), exact: true }).waitFor();
expect(await f.page.getByLabel('Project title', { exact: true }).inputValue()).toBe(
projectTitle
);
expect(projectPosts).toBe(1);
if (!committedProject) {
throw new Error('Expected the ordinary project write to commit');
}
const project = committedProject;
expect((await f.request(`/api/v2/research-projects/${project.id}`)).project).toEqual(project);
await f.page.unroute(projectEndpoint);
await f.page.getByRole('button', { name: 'Discard project draft', exact: true }).click();
await f.page.getByRole('link', { name: projectTitle.trim(), exact: true }).click();
await f.page.waitForURL(`${f.origin}/research/${project.id}`);
let savedHypothesis: ResearchHypothesis | undefined;
for (const kind of ['hypothesis', 'note'] as const) {
await f.page.getByRole('button', { name: `New ${kind}`, exact: true }).click();
const title = ` Original ${kind} proposal `;
const body = ` Preserve this ${kind} proposal.\n`;
await f.page
.getByLabel(kind === 'hypothesis' ? 'Hypothesis title' : 'Note title', { exact: true })
.fill(title);
await f.page
.getByLabel(kind === 'hypothesis' ? 'Proposition' : 'Note body', { exact: true })
.fill(body);
await f.page.getByLabel('Declared content origin', { exact: true }).selectOption('ai');
if (kind === 'hypothesis') {
await f.page
.getByLabel('Validation criteria', { exact: true })
.fill('Untouched validation required.');
}
const path = kind === 'hypothesis' ? 'hypotheses' : 'analysis-artifacts';
const endpoint = `**/api/v2/research-projects/${project.id}/${path}`;
let posts = 0;
let committed: ResearchHypothesis | ResearchArtifact | undefined;
await f.page.route(endpoint, async route => {
if (route.request().method() !== 'POST') {
await route.continue();
return;
}
posts++;
const response = await route.fetch();
expect(response.status()).toBe(201);
const payload = (await response.json()) as {
hypothesis: ResearchHypothesis;
artifact: ResearchArtifact;
};
if (kind === 'hypothesis') {
committed = structuredClone(payload.hypothesis);
savedHypothesis = structuredClone(payload.hypothesis);
payload.hypothesis.latestRevision.proposition = 'Substituted acknowledgement only.';
} else {
committed = structuredClone(payload.artifact);
payload.artifact.latestRevision.body = 'Substituted acknowledgement only.';
}
await route.fulfill({ response, json: payload });
});
await f.page.getByRole('button', { name: `Create ${kind}`, exact: true }).click();
await visible(f.page, 'The saved research entry response could not be verified.');
expect(
await f.page
.getByLabel(kind === 'hypothesis' ? 'Hypothesis title' : 'Note title', { exact: true })
.inputValue()
).toBe(title);
expect(
await f.page
.getByLabel(kind === 'hypothesis' ? 'Proposition' : 'Note body', { exact: true })
.inputValue()
).toBe(body);
expect(await f.page.getByLabel('Declared content origin', { exact: true }).inputValue()).toBe(
'ai'
);
expect(
await f.page.getByRole('button', { name: `Create ${kind}`, exact: true }).isDisabled()
).toBe(true);
await f.page.getByRole('button', { name: 'Check saved records', exact: true }).click();
await f.page.getByRole('button', { name: title.trim(), exact: true }).waitFor();
expect(
await f.page.getByRole('button', { name: `Create ${kind}`, exact: true }).isDisabled()
).toBe(true);
expect(posts).toBe(1);
if (!committed) {
throw new Error('Expected the ordinary narrative write to commit');
}
const reopened = await f.request(`/api/v2/${path}/${committed.id}`);
expect(reopened[kind === 'hypothesis' ? 'hypothesis' : 'artifact']).toEqual(committed);
await f.page.unroute(endpoint);
await f.page.getByRole('button', { name: 'Discard entry draft', exact: true }).click();
await f.page.getByRole('button', { name: title.trim(), exact: true }).click();
await f.page.getByRole('button', { name: 'Edit latest revision', exact: true }).waitFor();
const history = await f.request(`/api/v2/${path}/${committed.id}/revisions`);
expect(history.revisions).toEqual([committed.latestRevision]);
}
if (!savedHypothesis) {
throw new Error('Expected the original hypothesis revision');
}
await f.page.goto(`${f.origin}/backtests/${native.backtestId}/run/${runId}`);
await f.page.getByRole('button', { name: 'Save finding', exact: true }).click();
const dialog = f.page.getByRole('dialog', { name: 'Save finding', exact: true });
await dialog.getByLabel('Finding project', { exact: true }).selectOption(project.id);
await dialog
.getByLabel('Finding hypothesis', { exact: true })
.selectOption(savedHypothesis.latestRevision.id);
await dialog.getByLabel('Finding title', { exact: true }).fill('Original finding narrative');
await dialog.getByLabel('Finding body', { exact: true }).fill('Preserve the finding proposal.');
const findingEndpoint = `**/api/v2/research-projects/${project.id}/analysis-artifacts`;
let findingPosts = 0;
let committedFinding: ResearchArtifact | undefined;
await f.page.route(findingEndpoint, async route => {
if (route.request().method() !== 'POST') {
await route.continue();
return;
}
findingPosts++;
const response = await route.fetch();
expect(response.status()).toBe(201);
const payload = (await response.json()) as { artifact: ResearchArtifact };
committedFinding = structuredClone(payload.artifact);
payload.artifact.latestRevision.body = 'Substituted narrative; original citations retained.';
await route.fulfill({ response, json: payload });
});
await dialog.getByRole('button', { name: 'Save finding', exact: true }).click();
await visible(f.page, 'The saved finding response could not be verified.');
expect(await dialog.getByLabel('Finding body', { exact: true }).inputValue()).toBe(
'Preserve the finding proposal.'
);
expect(
await dialog.getByRole('button', { name: 'Save finding', exact: true }).isDisabled()
).toBe(true);
expect(
await dialog.getByRole('link', { name: 'Open saved finding', exact: true }).count()
).toBe(0);
expect(findingPosts).toBe(1);
if (!committedFinding) {
throw new Error('Expected the original finding write to commit');
}
expect(committedFinding.latestRevision.evidence).toHaveLength(1);
expect(committedFinding.hypothesisRevisionId).toBe(savedHypothesis.latestRevision.id);
expect(committedFinding.latestRevision.evidence[0]!.value).toBe(
native.analytics.metrics.totalReturn
);
await f.page.unroute(findingEndpoint);
await dialog.getByRole('button', { name: 'Close', exact: true }).click();
await f.page.goto(`${f.origin}/research/${project.id}?artifact=${committedFinding.id}`);
await f.page.getByRole('button', { name: 'Edit latest revision', exact: true }).waitFor();
expect((await f.request(`/api/v2/analysis-artifacts/${committedFinding.id}`)).artifact).toEqual(
committedFinding
);
await f.page.reload();
await f.page.getByRole('button', { name: 'Edit latest revision', exact: true }).waitFor();
expect(
(
await f.pool.query(`SELECT
(SELECT count(*)::int FROM runs) AS runs,
(SELECT count(*)::int FROM backtest_dispatch) AS dispatch,
(SELECT count(*)::int FROM research_projects) AS projects,
(SELECT count(*)::int FROM research_hypotheses) AS hypotheses,
(SELECT count(*)::int FROM research_hypothesis_revisions) AS hypothesis_revisions,
(SELECT count(*)::int FROM analysis_artifacts) AS artifacts,
(SELECT count(*)::int FROM analysis_artifact_revisions) AS artifact_revisions`)
).rows[0]
).toEqual({
runs: 1,
dispatch: 1,
projects: 1,
hypotheses: 1,
hypothesis_revisions: 1,
artifacts: 2,
artifact_revisions: 2,
});
expect(await f.request(`/api/v2/runs/${runId}/results`)).toEqual(native);
expect(f.unexpectedNetwork).toEqual([]);
} finally {
await f.close();
}
});
test('revision_acknowledgements_preserve_drafts_and_reopen_exact_committed_history_browser', async () => {
const f = await fixture(true);
try {