Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

smoke-desktop-app.ts

97 lines4.4 KB

1// @effect-diagnostics nodeBuiltinImport:off globalConsole:off
2// Launches a packaged desktop app against a seeded T3 home, opens a thread, and checks its
3// messages render. Release jobs run it so a build whose backend or bundle cannot start fails
4// before it is published.
5import * as NodeFS from "node:fs";
6import * as NodeOS from "node:os";
7import * as NodePath from "node:path";
8import * as NodeTimersPromises from "node:timers/promises";
9import * as NodeUtil from "node:util";
10import { _electron, type ElectronApplication } from "playwright-core";
11​
12// A snapshot from an older build on purpose: the packaged server must migrate it forward, as it
13// does for every upgrading user. It holds one project and two threads, nothing else.
14const FIXTURE_DB = NodePath.join(import.meta.dirname, "fixtures/desktop-launch/statev2.sqlite");
15const THREAD_TITLE = "Release launch check";
16const THREAD_MESSAGE = "The release opens and shows this thread.";
17const STEP_TIMEOUT_MS = 60_000;
18​
19const { values } = NodeUtil.parseArgs({
20 options: {
21 executable: { type: "string" },
22 "artifacts-dir": { type: "string" },
23 },
24});
25const executable = values.executable;
26if (!executable)
27 throw new Error("Usage: smoke-desktop-app.ts --executable <app> [--artifacts-dir <dir>]");
28const artifactsDir = NodePath.resolve(values["artifacts-dir"] ?? "desktop-launch-check");
29​
30const home = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-desktop-launch-"));
31const t3Home = NodePath.join(home, "t3");
32NodeFS.mkdirSync(NodePath.join(t3Home, "userdata"), { recursive: true });
33NodeFS.copyFileSync(FIXTURE_DB, NodePath.join(t3Home, "userdata/statev2.sqlite"));
34​
35let step = "launching the app";
36let app: ElectronApplication | undefined;
37try {
38 app = await _electron.launch({
39 executablePath: executable,
40 // Linux CI runners do not allow the Chromium sandbox's user namespaces.
41 args: ["--no-sandbox"],
42 // HOME stays real: on macOS a fake HOME has no login keychain, and safeStorage blocks the
43 // main process on a keychain dialog. macOS keeps its Electron profile in the runner's HOME.
44 env: {
45 ...process.env,
46 XDG_CONFIG_HOME: NodePath.join(home, ".config"),
47 XDG_DATA_HOME: NodePath.join(home, ".local/share"),
48 T3CODE_HOME: t3Home,
49 T3CODE_TELEMETRY_ENABLED: "false",
50 T3CODE_DISABLE_AUTO_UPDATE: "true",
51 },
52 timeout: STEP_TIMEOUT_MS,
53 });
54 // A backend that never starts leaves no window, or only the "could not start" window, so the
55 // thread wait below fails either way and its screenshot shows the startup error.
56 step = "waiting for the first window";
57 const page = await app.firstWindow({ timeout: STEP_TIMEOUT_MS });
58 try {
59 step = `finding "${THREAD_TITLE}" in the sidebar`;
60 const thread = page.locator("li[data-thread-item]", { hasText: THREAD_TITLE });
61 await thread.waitFor({ timeout: STEP_TIMEOUT_MS });
62 step = "opening the thread";
63 await thread.click();
64 await page.getByText(THREAD_MESSAGE).waitFor({ timeout: STEP_TIMEOUT_MS });
65 } catch (error) {
66 NodeFS.mkdirSync(artifactsDir, { recursive: true });
67 await page
68 .screenshot({ path: NodePath.join(artifactsDir, "window.png"), timeout: 10_000 })
69 .catch(() => {});
70 throw error;
71 }
72 console.log("Desktop launch check passed: the app opened and showed the seeded thread.");
73} catch (error) {
74 console.error(`Desktop launch check failed while ${step}.`);
75 NodeFS.mkdirSync(artifactsDir, { recursive: true });
76 console.error(error instanceof Error ? error.message : error);
77 const logs = NodePath.join(t3Home, "userdata/logs");
78 if (NodeFS.existsSync(logs)) {
79 NodeFS.cpSync(logs, NodePath.join(artifactsDir, "logs"), { recursive: true });
80 const serverLog = NodePath.join(logs, "server-child.log");
81 if (NodeFS.existsSync(serverLog)) {
82 console.error("\nLast backend output (server-child.log):");
83 console.error(NodeFS.readFileSync(serverLog, "utf8").split("\n").slice(-20).join("\n"));
84 }
85 }
86 process.exitCode = 1;
87} finally {
88 if (app) {
89 // A hung app never finishes closing; kill it so the job does not wait for its timeout.
90 // Playwright drops the process handle once the app closes, so read it first.
91 const child = app.process();
92 const closed = app.close().catch(() => {});
93 await Promise.race([closed, NodeTimersPromises.setTimeout(10_000, undefined, { ref: false })]);
94 child.kill("SIGKILL");
95 }
96 NodeFS.rmSync(home, { recursive: true, force: true, maxRetries: 5 });