Skip to content

Commit aa2d3df

Browse files
authored
test(ui): wait for persisted new-session drafts (#125548)
1 parent 8df3deb commit aa2d3df

2 files changed

Lines changed: 69 additions & 0 deletions

File tree

‎ui/src/e2e/new-session-page.prompt-attachments.e2e.test.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
pastePng,
1717
pollLocatorText,
1818
reconnectProofArtifactDir,
19+
waitForPersistedNewSessionDraft,
1920
} from "./new-session-page.test-support.ts";
2021

2122
const suite = createNewSessionPageE2eSuite();
@@ -47,6 +48,7 @@ suite.define(() => {
4748
await firstPage.goto(`${suite.server.baseUrl}new`);
4849
const firstMessage = firstPage.locator(".new-session-page__message");
4950
await firstMessage.fill(text);
51+
await waitForPersistedNewSessionDraft(firstPage, text, 0);
5052
await firstPage.reload();
5153
await expect.poll(() => firstMessage.inputValue()).toBe(text);
5254
await firstPage.close();
@@ -86,10 +88,16 @@ suite.define(() => {
8688
const incognito = firstPage.getByRole("switch", { name: "Incognito" });
8789
await incognito.click();
8890
await expect.poll(() => incognito.getAttribute("aria-checked")).toBe("true");
91+
await waitForPersistedNewSessionDraft(firstPage, null, 0);
8992
await incognito.click();
9093
await expect.poll(() => incognito.getAttribute("aria-checked")).toBe("false");
9194
await firstMessage.fill("restore this prompt after restart and incognito");
9295
await expect.poll(() => firstPage.locator(".chat-attachment-thumb").count()).toBe(1);
96+
await waitForPersistedNewSessionDraft(
97+
firstPage,
98+
"restore this prompt after restart and incognito",
99+
1,
100+
);
93101
await firstPage.reload();
94102
await expect
95103
.poll(() => firstMessage.inputValue())

‎ui/src/e2e/new-session-page.test-support.ts‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,67 @@ export async function pastePng(target: Locator, count = 1) {
194194
);
195195
}
196196

197+
export async function waitForPersistedNewSessionDraft(
198+
page: Page,
199+
expectedText: string | null,
200+
expectedAttachmentCount: number,
201+
): Promise<void> {
202+
// Filling only proves DOM state. The durable read waits for the IndexedDB
203+
// transaction so reload or navigation cannot beat the snapshot write.
204+
await page.waitForFunction(
205+
async ({ text, attachmentCount }) => {
206+
try {
207+
const app = document.querySelector("openclaw-app") as HTMLElement & {
208+
runtime?: {
209+
context: {
210+
gateway: {
211+
connection: { gatewayUrl: string };
212+
snapshot: { client: { recoveryScope?: string } | null };
213+
};
214+
};
215+
};
216+
};
217+
const gateway = app.runtime?.context.gateway;
218+
const recoveryScope = gateway?.snapshot.client?.recoveryScope;
219+
if (!gateway || !recoveryScope) {
220+
return false;
221+
}
222+
const storeUrl = performance
223+
.getEntriesByType("resource")
224+
.map((entry) => entry.name)
225+
.find((name) => /\/composer-draft-store\.runtime-[^/]+\.js$/u.test(name));
226+
if (!storeUrl) {
227+
return false;
228+
}
229+
const draftStore = (await import(
230+
/* @vite-ignore */ storeUrl
231+
)) as typeof import("../lib/chat/composer-draft-store.runtime.ts");
232+
const params = new URLSearchParams(window.location.search);
233+
const result = await draftStore.readDurableComposerDraft({
234+
gatewayOwner: gateway.connection.gatewayUrl.trim() || "default",
235+
recoveryScope,
236+
scopeKey: JSON.stringify([
237+
params.get("agent")?.trim() ?? "",
238+
params.get("catalog")?.trim() ?? "",
239+
params.get("group")?.trim() ?? "",
240+
]),
241+
});
242+
if (text === null) {
243+
return result.status === "not-found" && result.revision !== undefined;
244+
}
245+
return (
246+
result.status === "found" &&
247+
result.draft.text === text &&
248+
result.draft.attachments.length === attachmentCount
249+
);
250+
} catch {
251+
return false;
252+
}
253+
},
254+
{ text: expectedText, attachmentCount: expectedAttachmentCount },
255+
);
256+
}
257+
197258
export async function replaceGatewayClient(page: Page) {
198259
await page.evaluate(() => {
199260
const app = document.querySelector("openclaw-app") as HTMLElement & {

0 commit comments

Comments
 (0)