feat(assessment): record capture hints and keep one window per attempt
This commit is contained in:
1 parent
c975a0d506
commit
bb917bf62e
108 files changed
+6649
-139
No files matched your search
@@ -35,9 +35,21 @@ export type SignalEvent = {
|
||||
[field: string]: unknown;
|
||||
};
|
||||
|
||||
/**
|
||||
* The work session of the attempt as the server holds it. Pages that mock the
|
||||
* same attempt share one object, so a second tab meets the hold of the first.
|
||||
*/
|
||||
export type LeaseState = { holder: string | null };
|
||||
|
||||
export type MockedAttempt = {
|
||||
/** Events of every signal batch the page sent, in order. */
|
||||
signals: SignalEvent[];
|
||||
/** The page identifier of every hold or renewal the page sent. */
|
||||
holds: string[];
|
||||
/** The page identifier of every release the page sent. */
|
||||
releases: string[];
|
||||
/** The page identifier of every session start the page sent (null: no header). */
|
||||
starts: Array<string | null>;
|
||||
/** Bodies of the answers the page sent. */
|
||||
answers: unknown[];
|
||||
/** Calls to the notice acknowledgement, to see what was stored. */
|
||||
@@ -46,7 +58,7 @@ export type MockedAttempt = {
|
||||
|
||||
export const watermarkCode = "7K3QD-9XM2T";
|
||||
/** The notice text version the page knows; the mock server requires the same one. */
|
||||
export const noticeVersion = 3;
|
||||
export const noticeVersion = 4;
|
||||
|
||||
type Options = {
|
||||
content: MockItemContent;
|
||||
@@ -56,6 +68,10 @@ type Options = {
|
||||
prompt?: string;
|
||||
/** The visible code of the attempt, or null for an attempt without one. */
|
||||
watermark?: string | null;
|
||||
/** The work session shared by the pages of one attempt; one page has its own by default. */
|
||||
lease?: LeaseState;
|
||||
/** How often the server asks the page to renew its hold. */
|
||||
renewAfterSeconds?: number;
|
||||
};
|
||||
|
||||
function isoSeconds(ms: number): string {
|
||||
@@ -93,7 +109,15 @@ function attemptBody(version: number, options: Options) {
|
||||
|
||||
/** Answers the API calls of the attempt page; every other API call gets a 404. */
|
||||
export async function mockAttemptApi(page: Page, options: Options): Promise<MockedAttempt> {
|
||||
const recorded: MockedAttempt = { signals: [], answers: [], acknowledgements: [] };
|
||||
const recorded: MockedAttempt = {
|
||||
signals: [],
|
||||
holds: [],
|
||||
releases: [],
|
||||
starts: [],
|
||||
answers: [],
|
||||
acknowledgements: [],
|
||||
};
|
||||
const lease = options.lease ?? { holder: null };
|
||||
let acknowledged = options.noticeAcknowledged ?? true;
|
||||
|
||||
await page.route("**/api/v1/**", async (route: Route) => {
|
||||
@@ -133,9 +157,40 @@ export async function mockAttemptApi(page: Page, options: Options): Promise<Mock
|
||||
acknowledged_at: acknowledged ? "2026-10-10T10:00:00Z" : null,
|
||||
});
|
||||
}
|
||||
const client = request.headers()["x-attempt-client"] ?? null;
|
||||
const heldByAnother = lease.holder !== null && lease.holder !== client;
|
||||
const busy = () =>
|
||||
reply(
|
||||
{
|
||||
type: "urn:lincodex:problem:state_conflict",
|
||||
title: "Conflict",
|
||||
status: 409,
|
||||
code: "state_conflict",
|
||||
},
|
||||
409,
|
||||
);
|
||||
if (path === `${base}/work-session`) {
|
||||
if (method === "DELETE") {
|
||||
recorded.releases.push(client ?? "");
|
||||
if (lease.holder === client) lease.holder = null;
|
||||
return route.fulfill({ status: 204, headers: { "cache-control": "no-store" } });
|
||||
}
|
||||
recorded.holds.push(client ?? "");
|
||||
if (heldByAnother) return busy();
|
||||
lease.holder = client;
|
||||
return reply({
|
||||
held_until: isoSeconds(Date.now() + 120_000),
|
||||
renew_after_seconds: options.renewAfterSeconds ?? 15,
|
||||
});
|
||||
}
|
||||
if (path === base && method === "GET") return reply(attemptBody(1, options));
|
||||
if (path === `${base}/sessions`) return reply(attemptBody(2, options));
|
||||
if (path === `${base}/sessions`) {
|
||||
recorded.starts.push(client);
|
||||
if (heldByAnother) return busy();
|
||||
return reply(attemptBody(2, options));
|
||||
}
|
||||
if (path === `${base}/answers`) {
|
||||
if (heldByAnother) return busy();
|
||||
recorded.answers.push(JSON.parse(request.postData() ?? "{}"));
|
||||
return reply(attemptBody(3, options));
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user