|
| 1 | +/// <reference lib="dom" /> |
| 2 | +import { CommandId, ThreadId } from "@t3tools/contracts"; |
| 3 | +import { expect } from "e2e"; |
| 4 | +import * as Effect from "effect/Effect"; |
| 5 | + |
| 6 | +import { T3_HOME } from "../e2e.config.ts"; |
| 7 | +import { rpcClient, runPromise, test, WEB } from "./t3.ts"; |
| 8 | + |
| 9 | +test( |
| 10 | + "An offline settle leaves a newer pin reorder in place", |
| 11 | + WEB, |
| 12 | + async ({ t3, app, browser, screen }) => { |
| 13 | + const project = await t3.project("demo"); |
| 14 | + const moved = await t3.thread({ project, title: "First pinned thread", messages: ["Start"] }); |
| 15 | + const other = await t3.thread({ project, title: "Second pinned thread", messages: ["Start"] }); |
| 16 | + |
| 17 | + const withClient = <A, E>( |
| 18 | + use: (client: Effect.Success<ReturnType<typeof rpcClient>>) => Effect.Effect<A, E>, |
| 19 | + ) => runPromise(rpcClient(T3_HOME, app.baseUrl ?? "").pipe(Effect.flatMap(use), Effect.scoped)); |
| 20 | + |
| 21 | + await withClient((client) => |
| 22 | + Effect.gen(function* () { |
| 23 | + for (const [thread, orderKey] of [ |
| 24 | + [moved, "g"], |
| 25 | + [other, "m"], |
| 26 | + ] as const) { |
| 27 | + yield* client["orchestration.dispatchCommand"]({ |
| 28 | + type: "thread.pin", |
| 29 | + commandId: CommandId.make(`scenario:pin:${thread.threadId}`), |
| 30 | + threadId: ThreadId.make(thread.threadId), |
| 31 | + orderKey, |
| 32 | + }); |
| 33 | + } |
| 34 | + }), |
| 35 | + ); |
| 36 | + |
| 37 | + await browser.addInitScript(() => { |
| 38 | + localStorage.setItem("t3code:sidebar:settled-expanded", "true"); |
| 39 | + const sockets = new Set<WebSocket>(); |
| 40 | + let offline = false; |
| 41 | + Object.defineProperty(navigator, "onLine", { get: () => !offline }); |
| 42 | + const NativeWebSocket = window.WebSocket; |
| 43 | + window.WebSocket = class extends NativeWebSocket { |
| 44 | + constructor(url: string | URL, protocols?: string | string[]) { |
| 45 | + super(url, protocols); |
| 46 | + if (new URL(url, location.href).pathname !== "/ws") return; |
| 47 | + sockets.add(this); |
| 48 | + this.addEventListener("close", () => sockets.delete(this), { once: true }); |
| 49 | + if (offline) this.close(); |
| 50 | + } |
| 51 | + }; |
| 52 | + document.addEventListener("scenario-offline", () => { |
| 53 | + offline = true; |
| 54 | + window.dispatchEvent(new Event("offline")); |
| 55 | + for (const socket of sockets) socket.close(); |
| 56 | + }); |
| 57 | + document.addEventListener("scenario-online", () => { |
| 58 | + offline = false; |
| 59 | + window.dispatchEvent(new Event("online")); |
| 60 | + }); |
| 61 | + }); |
| 62 | + |
| 63 | + await t3.open(other); |
| 64 | + const row = browser.locator(`li[data-thread-item$=":${moved.threadId}"]`); |
| 65 | + await row.getByRole("button", "Unpin thread", { exact: true }).waitFor(); |
| 66 | + await browser.evaluate(() => { |
| 67 | + document.dispatchEvent(new Event("scenario-offline")); |
| 68 | + return null; |
| 69 | + }); |
| 70 | + await screen.getByRole("button", "Environment disconnected", { exact: true }).waitFor(); |
| 71 | + await row.hover(); |
| 72 | + await row.getByRole("button", "Settle thread", { exact: true }).tap(); |
| 73 | + const pending = row.getByText("Pending", { exact: true }); |
| 74 | + await pending.waitFor(); |
| 75 | + |
| 76 | + await withClient((client) => |
| 77 | + client["orchestration.dispatchCommand"]({ |
| 78 | + type: "thread.pin.reorder", |
| 79 | + commandId: CommandId.make(`scenario:reorder:${moved.threadId}`), |
| 80 | + threadId: ThreadId.make(moved.threadId), |
| 81 | + orderKey: "t", |
| 82 | + }), |
| 83 | + ); |
| 84 | + await browser.evaluate(() => { |
| 85 | + document.dispatchEvent(new Event("scenario-online")); |
| 86 | + return null; |
| 87 | + }); |
| 88 | + await pending.waitFor({ state: "detached" }); |
| 89 | + |
| 90 | + await app.screenshot("pin-after-reconnect"); |
| 91 | + const { thread } = await withClient((client) => |
| 92 | + client["orchestration.getThreadProjection"]({ threadId: ThreadId.make(moved.threadId) }), |
| 93 | + ); |
| 94 | + expect({ pinned: thread.pinnedAt != null, pinOrderKey: thread.pinOrderKey }).toEqual({ |
| 95 | + pinned: true, |
| 96 | + pinOrderKey: "t", |
| 97 | + }); |
| 98 | + }, |
| 99 | +); |
0 commit comments