[skip ci] wip: preserve Hermes crank development checkpoint

Owner-authorized recovery of accumulated source changes.
Includes accepted work and in-progress changes; NOT a release or acceptance.
Parent baseline 1b6db09bd1. Runtime data, build caches and local credentials excluded.
This commit is contained in:
Hermes DevOps committed 2026-10-25 10:00:22 +00:00
1 parent 1b6db09bd1
commit 75fe5a123c
357 files changed
+118831 -5113

No files matched your search

+120
View File
@@ -36,6 +36,35 @@ async function operationSnapshot(page, workspaceId) {
}, workspaceId);
}
// Deterministic stand-in for "replay after reload": hides the CSRF token and
// makes the session restore wait until the test releases it. Every attempt that
// reaches the wait is counted, so the test can prove a cancelled attempt never
// installs itself again and never sends its POST.
async function installSessionGate(page) {
await page.evaluate(() => {
const auth = window.CrankAuth;
const realToken = auth.getCsrfToken();
if (!realToken) throw new Error('the CSRF gate needs an authenticated session token');
const original = { getCsrfToken: auth.getCsrfToken, fetchSession: auth.fetchSession };
const gate = { realToken, waits: 0, released: false, resolvers: [] };
gate.release = () => {
gate.released = true;
gate.resolvers.splice(0).forEach((resolve) => resolve(auth.getCachedSession()));
};
gate.restore = () => {
gate.release();
auth.getCsrfToken = original.getCsrfToken;
auth.fetchSession = original.fetchSession;
};
window.__csrfGate = gate;
auth.getCsrfToken = () => (gate.released ? gate.realToken : '');
auth.fetchSession = () => {
gate.waits += 1;
return new Promise((resolve) => { gate.resolvers.push(resolve); });
};
});
}
async function requestAdminControl(command) {
const requestFile = path.join(controlDirectory, 'admin-control.request');
const ackFile = path.join(controlDirectory, 'admin-control.ack');
@@ -227,3 +256,94 @@ test('[P1] AC05 real preview and plan do not mutate Operations; reduced-motion A
|| operation.operation_id === operationId)).toHaveLength(1);
expect(await storageCounts(workspace.id)).toEqual(committedCounts);
});
test('[P1] AC05 Apply waits for the CSRF session before the POST and a cancelled wait never replays', async ({ page }) => {
test.setTimeout(120_000);
await login(page);
await dismissOnboardingIfOpen(page);
const workspace = await getCurrentWorkspace(page);
expect(workspace && workspace.id).toBeTruthy();
const initialOperations = await operationSnapshot(page, workspace.id);
const applyRequests = [];
page.on('request', (request) => {
if (request.method() !== 'POST') return;
if (!/\/imports\/openapi\/[^/]+\/apply(?:\?|$)/.test(request.url())) return;
applyRequests.push({
url: request.url(),
body: request.postData(),
csrf: request.headers()['x-csrf-token'] || '',
});
});
const openPlanFor = async (operationId) => {
await openImport(page, operationId);
await page.locator('#openapi-import-server-custom').fill('https://api.example.test');
await page.locator('#openapi-import-create').click();
await expect(page.locator('[data-testid="openapi-import-apply-confirmation"]')).toBeVisible();
};
const waitForApplyResponse = () => page.waitForResponse((response) =>
response.request().method() === 'POST'
&& response.url().includes('/imports/openapi/')
&& response.url().endsWith('/apply'));
const gateWaits = () => page.evaluate(() => window.__csrfGate.waits);
// A: the first Apply must hold the POST until the session (and its CSRF
// token) is restored, and a second click must not start a second attempt.
const operationA = uniqueName('csrf_gate_a').replace(/[^a-zA-Z0-9_]/g, '_');
await openPlanFor(operationA);
await installSessionGate(page);
await page.locator('#openapi-import-plan-apply').click();
await expect.poll(gateWaits).toBe(1);
// The attempt owns the existing cancel scheme before its async wait.
await expect(page.locator('#openapi-import-cancel')).toBeVisible();
await expect(page.locator('#openapi-import-plan-apply')).toBeDisabled();
expect(applyRequests).toHaveLength(0);
const appliedA = waitForApplyResponse();
await page.evaluate(() => window.__csrfGate.release());
expect((await appliedA).status()).toBe(200);
expect(applyRequests).toHaveLength(1);
expect(applyRequests[0].csrf).toBeTruthy();
await expect(page.locator('#openapi-import-result')).toContainText(operationA);
await page.evaluate(() => window.__csrfGate.restore());
// B: closing the modal while the session restore is still pending must not
// replay the cancelled attempt; the visible retry is the only POST and it
// keeps the same URL, body and idempotency key.
await page.evaluate(() => window.CrankOpenApiImport.close());
const operationB = uniqueName('csrf_gate_b').replace(/[^a-zA-Z0-9_]/g, '_');
await openPlanFor(operationB);
await installSessionGate(page);
await page.locator('#openapi-import-plan-apply').click();
await expect.poll(gateWaits).toBe(1);
await page.keyboard.press('Escape');
await expect(page.locator('#openapi-import-modal')).toBeHidden();
await page.getByRole('button', { name: localized('Import OpenAPI', 'Импорт OpenAPI') }).click();
await expect(page.locator('#openapi-import-retry')).toBeVisible();
const replay = await page.evaluate(() =>
JSON.parse(sessionStorage.getItem('crank_openapi_plan_apply_replay_v2')));
expect(replay && replay.idempotencyKey).toBeTruthy();
await page.locator('#openapi-import-retry').click();
await expect.poll(gateWaits).toBe(2);
expect(applyRequests).toHaveLength(1);
const appliedB = waitForApplyResponse();
await page.evaluate(() => window.__csrfGate.release());
expect((await appliedB).status()).toBe(200);
await page.evaluate(() => window.__csrfGate.restore());
expect(applyRequests).toHaveLength(2);
expect(applyRequests[1].csrf).toBeTruthy();
expect(applyRequests[1].body).toContain(replay.idempotencyKey);
expect(applyRequests[1].body).toContain(replay.planDigest);
await expect(page.locator('#openapi-import-result')).toContainText(operationB);
const finalOperations = await operationSnapshot(page, workspace.id);
expect(finalOperations).toHaveLength(initialOperations.length + 2);
for (const applied of [operationA, operationB]) {
expect(finalOperations.filter((operation) => operation.name === applied
|| operation.operation_id === applied)).toHaveLength(1);
}
});