From 67ead7347bcfb63a18dc3c499e9733bdb52a27aa Mon Sep 17 00:00:00 2001 From: Thomas Date: Wed, 4 Nov 2026 23:33:49 -0500 Subject: [PATCH 1/3] Add update-t3code agent skill Describe what the Flatpak repackage should be and the workflow for each upstream release, for Claude and Codex: refresh the patches, review the release, get product decisions, verify, then ship within the request. --- .agents/skills/update-t3code/SKILL.md | 129 ++++++++++++++++++++++++++ .claude/skills | 1 + 2 files changed, 130 insertions(+) create mode 100644 .agents/skills/update-t3code/SKILL.md create mode 120000 .claude/skills diff --git a/.agents/skills/update-t3code/SKILL.md b/.agents/skills/update-t3code/SKILL.md new file mode 100644 index 0000000..aeef7ce --- /dev/null +++ b/.agents/skills/update-t3code/SKILL.md @@ -0,0 +1,129 @@ +--- +name: update-t3code +description: Update the T3 Code Flatpak to a new upstream release. Refresh the patches, check that the package still works the way it should, bring new upstream features into the Flatpak where they fit, and flag anything that needs a product decision. +--- + +# Update T3 Code + +Handle the patch work and routine checks without asking. Ask only about +product decisions, and explain them in terms of what users see, not the code. + +## What the package should be + +This is upstream T3 Code, repackaged to be a good Flatpak citizen. + +- **Stay close to upstream.** Follow upstream's behavior wherever it works in + the Flatpak. Patch only what the Flatpak needs, and keep each patch as small + as it can be. Leave upstream bugs to upstream. +- **Keep the system clean.** The Flatpak is a packaging mechanism, not a strict + security boundary. T3 Code's own components and data stay where removing the + Flatpak with its data cleans them up. It never installs things onto the host, + changes system configuration or sets up host software for the user. Work the + user asks for, such as agents editing projects or files saved to Downloads, + is fine. +- **Users own their system.** When a feature needs something on the host, + document the requirement instead of building workarounds or setting it up + for them. +- **Flatpak owns updates.** The desktop app and the local server it starts are + updated through Flatpak, never by themselves. Remote servers managed + separately keep upstream's update behavior. +- **Use the desktop's own facilities.** Prefer portals and the user's normal + desktop options over custom integrations. +- **Don't quietly lose features.** If something can't work as upstream built + it, find the simplest way to keep what users actually get from it, often + through the app's UI. If the feature isn't worth keeping, hide it so users + never reach a broken option. +- **Prefer simple.** Choose the option with the least patch to maintain, unless + a slightly bigger change clearly serves users better. + +## Workflow + +### 1. Start from the update PR + +The **Check for T3 Code releases** workflow opens a draft PR on +`automation/update-t3code-vX`. If there isn't one yet, run that workflow. Work +on the PR branch. + +### 2. Refresh the patches + +Rebase the patch series in `patches/` onto the new upstream tag, using an +upstream checkout in the gitignored `build-flatpak/` directory. Keep one commit +per patch so the files can be regenerated from them. Follow each conflicting +upstream change's intent: + +- Drop what upstream now does itself or no longer needs, including whole + patches. Remove a retired patch's file and its manifest entry. +- Keep the Flatpak's behavior where both apply. +- Make sure the regenerated files reproduce the tree you tested. + +### 3. Review the release + +A clean rebase doesn't mean the package still works. Read the upstream changes +and check each of these. + +- **Existing patches still hold.** Check that each patch's behavior still works, + not just that it applies. Upstream often adds a new path next to the one a + patch covers. +- **New features that need Flatpak support.** These are things users should + get that break across the sandbox and host boundary. The desktop UI runs in + the sandbox and the server runs on the host, so look at what crosses between + them: environment, paths, files, startup arguments, and host tools the desktop + calls. Check new settings, notices and recovery screens too, by tracing what + they actually do. +- **Features that don't fit.** These are things that conflict with what the + package should be, such as installing onto the host, setting up host + software or self-updating. Judge each by what it does for users and what it + does to the host, not by how upstream exposes it. Recommend keeping, adapting + or hiding it. +- **Build requirements.** Compare upstream's toolchain needs, such as Node, + pnpm and Electron, with the manifest's SDK extensions and build commands. + +Also note existing gaps you come across, even if the release didn't cause them, +and anything worth testing on the currently installed version. + +### 4. Report and get decisions + +- Summarize the patch refresh briefly, in plain terms. Say what changed and why. +- Raise only the items that need a decision. Go one at a time, with the user + impact, your recommendation and what it costs to maintain. Don't report + things that need no action. +- Don't make product changes until they're approved. + +### 5. Implement and verify + +- Make the agreed changes in the style of the existing patches. +- Run `./scripts/build-flatpak build` from the repo root. It runs the desktop + tests, the server tests listed in `flatpak/modules/t3code.yml` and both + typechecks, but no web tests. Check web changes directly. Make sure the test + files the manifest lists still exist. +- Check that the packaged app still works where the release touched it: + startup, the host server starting and stopping, and the user flows that + changed. Use isolated data where practical. Say what you tested, what you + only checked by reading code, and what's unverified. +- Update the user documentation when host requirements, setup or supported + behavior change. +- Before handing off, do a final review pass over the whole change set for + bloat, leftovers and anything outside what was agreed. +- Ask before replacing the installed copy of the app to test a build. It shares + data with the instance that's running. + +### 6. Ship within the request + +Do only as much as the current request asks. Merge and release only when +asked to. + +- **PR:** push to the PR branch, rewrite the description to cover the update and + any packaging changes, mark it ready, and wait for **Validate Flatpak + update**. Bot-created runs need approval in the PR. +- **Metainfo:** keep the release checker's formatting. Leave the release + description empty for a plain upstream update. Write a short, specific note + when the package itself changes. Packaging-only releases append a revision, + such as `0.1.7-1`. If a release ships upstream and packaging changes + together, use one entry for it, not two. +- **Merge:** squash-merge and delete the branch. **Publish Flatpak repository** + then runs on `main` by itself. Check that it succeeds. A "release" means + publishing the Flatpak repository, not creating a GitHub Release. +- **After release:** clean up the local and remote branches. The published + update then gets tested on a real install. Fix regressions as the next `-N` + revision. The app's server logs are under + `~/.var/app/com.t3tools.t3code/data/t3/userdata/logs/`. diff --git a/.claude/skills b/.claude/skills new file mode 120000 index 0000000..2b7a412 --- /dev/null +++ b/.claude/skills @@ -0,0 +1 @@ +../.agents/skills \ No newline at end of file From 0c60411d83aaedccd5f32b72c69c75db5c642e13 Mon Sep 17 00:00:00 2001 From: Thomas Date: Thu, 5 Nov 2026 21:42:34 -0500 Subject: [PATCH 2/3] Test the packaged app across the sandbox and host in CI Launch the built Flatpak headless in the CI container and check where the sandbox meets the host: the host server, agent CLIs on the login PATH, downloads, terminals and git, editors on the host PATH and installed as Flatpaks, SSH, Tailscale, managed policy, database maintenance, and the recovery screen when the host is out of reach. The checks are typechecked TypeScript using Playwright, with setup in tests/packaged and failure screenshots and logs uploaded from CI. scripts/test-packaged runs the same checks locally against a local build or a published commit with Docker or Podman, and the update skill points to it. --- .agents/skills/update-t3code/SKILL.md | 9 +- .github/workflows/validate-flatpak.yml | 13 + docs/building.md | 21 + docs/publishing.md | 5 +- scripts/test-packaged | 38 ++ tests/packaged/app.ts | 196 ++++++++ tests/packaged/fixtures/fake-command | 23 + .../fixtures/sandbox-database-maintenance.sh | 15 + tests/packaged/host.ts | 164 +++++++ tests/packaged/main.ts | 50 +++ tests/packaged/package-lock.json | 420 ++++++++++++++++++ tests/packaged/package.json | 13 + tests/packaged/packaged.test.ts | 292 ++++++++++++ tests/packaged/run.sh | 14 + tests/packaged/tsconfig.json | 16 + 15 files changed, 1284 insertions(+), 5 deletions(-) create mode 100755 scripts/test-packaged create mode 100644 tests/packaged/app.ts create mode 100755 tests/packaged/fixtures/fake-command create mode 100644 tests/packaged/fixtures/sandbox-database-maintenance.sh create mode 100644 tests/packaged/host.ts create mode 100644 tests/packaged/main.ts create mode 100644 tests/packaged/package-lock.json create mode 100644 tests/packaged/package.json create mode 100644 tests/packaged/packaged.test.ts create mode 100755 tests/packaged/run.sh create mode 100644 tests/packaged/tsconfig.json diff --git a/.agents/skills/update-t3code/SKILL.md b/.agents/skills/update-t3code/SKILL.md index aeef7ce..cbfe2d2 100644 --- a/.agents/skills/update-t3code/SKILL.md +++ b/.agents/skills/update-t3code/SKILL.md @@ -96,10 +96,11 @@ and anything worth testing on the currently installed version. tests, the server tests listed in `flatpak/modules/t3code.yml` and both typechecks, but no web tests. Check web changes directly. Make sure the test files the manifest lists still exist. -- Check that the packaged app still works where the release touched it: - startup, the host server starting and stopping, and the user flows that - changed. Use isolated data where practical. Say what you tested, what you - only checked by reading code, and what's unverified. +- Run `./scripts/test-packaged` to check the built package where the sandbox + and host meet. When the release changes a flow these checks don't cover, + add a check to `tests/packaged/` or test it by hand with isolated data. Say + what you tested, what you only checked by reading code, and what's + unverified. - Update the user documentation when host requirements, setup or supported behavior change. - Before handing off, do a final review pass over the whole change set for diff --git a/.github/workflows/validate-flatpak.yml b/.github/workflows/validate-flatpak.yml index d4c20d7..a905e71 100644 --- a/.github/workflows/validate-flatpak.yml +++ b/.github/workflows/validate-flatpak.yml @@ -8,6 +8,7 @@ on: - flatpak/** - '!flatpak/pages/**' - patches/** + - tests/packaged/** workflow_dispatch: permissions: @@ -40,3 +41,15 @@ jobs: repo-dir: build-flatpak/validation-repo state-dir: build-flatpak/builder-state cache-key: flatpak-builder-x86_64-${{ hashFiles('flatpak/**', '!flatpak/pages/**', 'patches/**') }} + + # Launches the built package and checks the sandbox and host still work together. + - name: Test the packaged app + run: tests/packaged/run.sh --repo build-flatpak/validation-repo + + - name: Upload packaged app test results + if: failure() + uses: actions/upload-artifact@v7 + with: + name: packaged-app-test-results + path: build-flatpak/packaged-test-artifacts + if-no-files-found: ignore diff --git a/docs/building.md b/docs/building.md index 9efc5ff..1e4a02a 100644 --- a/docs/building.md +++ b/docs/building.md @@ -24,3 +24,24 @@ To build and install the package for your user, then launch it: ``` Build state and output stay in `build-flatpak/`. + +## Testing the packaged app + +The packaged app checks install a build in a throwaway container, launch it +headless, and check the places where the sandbox and host meet: the host +server, agent CLIs found through the login shell, downloads, terminals, +editors, SSH, Tailscale, managed policy, database maintenance, and the recovery +screen when the host is out of reach. Docker runs them by default; set +`CONTAINER_ENGINE=podman` to use Podman. + +```sh +./scripts/test-packaged # the build from ./scripts/build-flatpak build +./scripts/test-packaged published # a build from the published repository +``` + +The container stands in for a user's machine, so nothing is installed on yours. +A run needs network access to fetch runtimes and upstream test fixtures. The +fixtures come from the upstream release in the manifest, so an older published +commit is checked against the current fixtures. The app logs, plus screenshots +from failed checks, are written to `build-flatpak/packaged-test-artifacts/`. The +checks live in `tests/packaged/` and are typechecked before each run. diff --git a/docs/publishing.md b/docs/publishing.md index a4daade..12edb5c 100644 --- a/docs/publishing.md +++ b/docs/publishing.md @@ -24,7 +24,10 @@ skip package validation and do not change the build cache key. The workflow applies the patches, builds the Flatpak, runs desktop tests and the editor launcher test, and typechecks the desktop and server. The WSL test file is excluded because some of its tests inspect host processes that the -Flatpak build sandbox cannot see. +Flatpak build sandbox cannot see. It then installs the build in the CI +container and runs the packaged app checks described in +[Building](building.md#testing-the-packaged-app). Failed checks upload their +screenshots and logs as a workflow artifact. ## Publishing diff --git a/scripts/test-packaged b/scripts/test-packaged new file mode 100755 index 0000000..628aac1 --- /dev/null +++ b/scripts/test-packaged @@ -0,0 +1,38 @@ +#!/usr/bin/env bash +set -euo pipefail + +PROJECT_ROOT="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)" +readonly PROJECT_ROOT +readonly IMAGE="ghcr.io/flathub-infra/flatpak-github-actions:freedesktop-25.08" +readonly ENGINE="${CONTAINER_ENGINE:-docker}" +readonly ARTIFACTS="$PROJECT_ROOT/build-flatpak/packaged-test-artifacts" + +# The container gets a read-only checkout; only test artifacts come back out. +run() { + local owner=() + # Root in a rootless Podman container is already you; Docker needs the files handed back. + if [ "$ENGINE" = docker ]; then + owner=(-e "HOST_UID=$(id -u)") + fi + rm -rf "$ARTIFACTS" + mkdir -p "$ARTIFACTS" + "$ENGINE" run --rm --privileged \ + -v "$PROJECT_ROOT:/src:ro" \ + -v "$ARTIFACTS:/artifacts:Z" \ + -e T3_TEST_ARTIFACTS=/artifacts \ + "${owner[@]}" \ + "$IMAGE" /src/tests/packaged/run.sh "$@" +} + +case "${1:-local}" in + local) + run --repo /src/build-flatpak/repo + ;; + published) + run --commit "${2:?Usage: $0 published }" + ;; + *) + echo "Usage: $0 [local | published ]" >&2 + exit 2 + ;; +esac diff --git a/tests/packaged/app.ts b/tests/packaged/app.ts new file mode 100644 index 0000000..f3a1116 --- /dev/null +++ b/tests/packaged/app.ts @@ -0,0 +1,196 @@ +// Starts the installed Flatpak and inspects it from the host side of the CI container. +import { spawn, spawnSync } from "node:child_process"; +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; +import { chromium, type Browser, type Page } from "playwright-core"; + +export const APP_ID = "com.t3tools.t3code"; +const DEBUG_URL = "http://127.0.0.1:9222"; +const APP_URL = "t3code://app"; + +/** The repository checkout. run.sh runs these files from a copy, so it passes the original. */ +export const checkout = process.env.T3_TEST_CHECKOUT ?? ""; +if (!checkout) throw new Error("Run the packaged app checks through tests/packaged/run.sh."); +export const artifactsDir = + process.env.T3_TEST_ARTIFACTS ?? path.join(checkout, "build-flatpak/packaged-test-artifacts"); +/** Where fixtures/fake-command records each call that reached the host. */ +export const CALL_LOG = "/tmp/t3-test-calls.log"; +export const upstreamDir = "/opt/t3-upstream"; +export const fixtureDatabase = path.join(upstreamDir, "scripts/fixtures/desktop-launch/statev2.sqlite"); +export const hostRepo = "/srv/t3-host-repo"; +export const userdata = path.join(os.homedir(), ".var/app", APP_ID, "data/t3/userdata"); + +export async function waitUntil( + what: string, + check: () => T | undefined | null | false | Promise, + timeoutMs = 60_000, +): Promise { + const deadline = Date.now() + timeoutMs; + while (Date.now() < deadline) { + const result = await check(); + if (result) return result; + await new Promise((resolve) => setTimeout(resolve, 500)); + } + throw new Error(`Timed out waiting for ${what}`); +} + +let launchCount = 0; + +/** Launches the app the way the desktop entry does, plus a debugging port for Playwright. */ +export function launchApp(): string { + launchCount += 1; + fs.mkdirSync(artifactsDir, { recursive: true }); + const logPath = path.join(artifactsDir, `app-${launchCount}.log`); + const log = fs.openSync(logPath, "w"); + // Chromium's own sandbox cannot run as root in the CI container. + spawn("flatpak", ["run", APP_ID, "--no-sandbox", "--remote-debugging-port=9222"], { + stdio: ["ignore", log, log], + detached: true, + }).unref(); + return logPath; +} + +/** Attaches to an app window, waiting for it to load after a launch or relaunch. */ +export async function connect(urlPrefix = APP_URL): Promise<{ browser: Browser; page: Page }> { + await waitUntil( + `a window at ${urlPrefix}`, + async () => { + const targets: Array<{ url: string }> = await fetch(`${DEBUG_URL}/json`) + .then((response) => response.json()) + .catch(() => []); + return targets.some((target) => target.url.startsWith(urlPrefix)); + }, + 90_000, + ); + const browser = await chromium.connectOverCDP(DEBUG_URL); + const page = browser + .contexts() + .flatMap((context) => context.pages()) + .find((candidate) => candidate.url().startsWith(urlPrefix)); + if (!page) throw new Error(`The window at ${urlPrefix} is not available over CDP.`); + return { browser, page }; +} + +/** Closes the window like a user would and waits for the app and its host server to exit. */ +export async function closeApp(page: Page): Promise { + await page.close({ runBeforeUnload: true }).catch(() => {}); + await waitUntil("the app and its host server to exit", () => appProcesses().length === 0, 30_000); +} + +/** Every process running the packaged binary, inside or outside the sandbox. */ +export function appProcesses(): number[] { + return processes().filter((pid) => readProc(pid, "comm").trim() === "t3code"); +} + +/** The T3 server the desktop starts for itself. */ +export function hostServerPid(): number | undefined { + return processes().find((pid) => { + const args = readProc(pid, "cmdline").split("\0"); + return args.includes("--bootstrap-fd") && !args[0]?.endsWith("flatpak-spawn"); + }); +} + +export function mountNamespace(pid: number | "self"): string { + return fs.readlinkSync(`/proc/${pid}/ns/mnt`); +} + +function processes(): number[] { + return fs + .readdirSync("/proc") + .filter((entry) => /^\d+$/.test(entry)) + .map(Number); +} + +function readProc(pid: number, file: string): string { + try { + return fs.readFileSync(`/proc/${pid}/${file}`, "utf8"); + } catch { + return ""; + } +} + +/** Flatpak's host-command service, which `flatpak-spawn --host` goes through. */ +function sessionHelperPath(): string { + const flatpak = spawnSync("sh", ["-c", "command -v flatpak"], { encoding: "utf8" }).stdout.trim(); + return path.join(path.dirname(flatpak), "../libexec/flatpak-session-helper"); +} + +/** Starts the host-command service, which desktop sessions start on demand. */ +export function startSessionHelper() { + spawn(sessionHelperPath(), [], { stdio: "ignore", detached: true }).unref(); +} + +/** Stops the host-command service, which leaves the sandbox with no way to reach the host. */ +export async function stopSessionHelper() { + const helper = sessionHelperPath(); + const running = () => processes().filter((pid) => readProc(pid, "cmdline").split("\0")[0] === helper); + for (const pid of running()) process.kill(pid); + await waitUntil("the host-command service to stop", () => running().length === 0); +} + +/** Calls that reached the fake host commands in fixtures/fake-command. */ +export function hostCalls(): string[] { + return fs.existsSync(CALL_LOG) ? fs.readFileSync(CALL_LOG, "utf8").trim().split("\n") : []; +} + +/** The local server URL the desktop reports once its backend is ready. */ +export async function backendPort(logPath: string): Promise { + const match = await waitUntil("the backend to become ready", () => + fs.readFileSync(logPath, "utf8").match(/backend ready[\s\S]*?url: http:\/\/127\.0\.0\.1:(\d+)\//), + ); + return Number(match[1]); +} + +const IPV6_NAMES: Record = { + "00000000000000000000000000000000": "::", + "00000000000000000000000001000000": "::1", +}; + +/** Addresses listening on `port`, read from the kernel so the check sees what the server bound. */ +export function listeningAddresses(port: number): string[] { + const portHex = port.toString(16).toUpperCase().padStart(4, "0"); + return ["/proc/net/tcp", "/proc/net/tcp6"].flatMap((table) => + fs + .readFileSync(table, "utf8") + .trim() + .split("\n") + .slice(1) + .map((line) => line.trim().split(/\s+/)) + .filter(([, local, , state]) => state === "0A" && local?.endsWith(`:${portHex}`)) + .map(([, local]) => { + const hex = local!.split(":")[0]!; + if (hex.length > 8) return IPV6_NAMES[hex] ?? hex; + // The kernel prints IPv4 addresses in little-endian byte order. + return [3, 2, 1, 0].map((index) => parseInt(hex.slice(index * 2, index * 2 + 2), 16)).join("."); + }), + ); +} + +/** Runs the bundled CLI on the host, as the Flatpak's host server does. */ +export function runBundledCli(args: string[]): void { + const location = spawnSync("flatpak", ["info", "--show-location", APP_ID], { encoding: "utf8" }); + const runtime = path.join(location.stdout.trim(), "files/t3code"); + const result = spawnSync( + path.join(runtime, "t3code"), + [path.join(runtime, "resources/app.asar/apps/server/dist/bin.mjs"), ...args], + { encoding: "utf8", env: { ...process.env, ELECTRON_RUN_AS_NODE: "1" } }, + ); + if (result.status !== 0) throw new Error(`t3 ${args.join(" ")} failed:\n${result.stderr}`); +} + +/** Runs a shell script inside the app's sandbox. */ +export function runInSandbox(script: string): { status: number | null; output: string } { + const result = spawnSync("flatpak", ["run", "--command=sh", APP_ID, "-s"], { + input: script, + encoding: "utf8", + }); + return { status: result.status, output: `${result.stdout}${result.stderr}` }; +} + +export async function openSettings(page: Page, route: string) { + await page.goto(`t3code://app/#/settings/${route}`); + const dialog = page.getByRole("dialog").filter({ hasText: "Settings" }); + await dialog.waitFor(); + return dialog; +} diff --git a/tests/packaged/fixtures/fake-command b/tests/packaged/fixtures/fake-command new file mode 100755 index 0000000..e3cacf4 --- /dev/null +++ b/tests/packaged/fixtures/fake-command @@ -0,0 +1,23 @@ +#!/bin/sh +# Stands in for host tools the app should reach outside the sandbox. host.ts +# installs it as claude, codex, tailscale, code and ssh, and as zed in a stand-in +# Flatpak. The tests read its log (CALL_LOG in app.ts) to see which calls made it +# to the host. +name=$(basename "$0") +echo "$name $*" >> /tmp/t3-test-calls.log + +case "$name $*" in + "tailscale status --json"*) + echo '{"Self":{"DNSName":"ci-box.tail1234.ts.net.","TailscaleIPs":["100.101.102.103"],"CapMap":{"https":null}}}' + ;; + "tailscale serve status --json"*) + echo '{}' + ;; + ssh*) + echo "ssh: connect to host ci-box port 22: Connection refused" >&2 + exit 255 + ;; + *--version*) + echo "9.9.9 ($name test double)" + ;; +esac diff --git a/tests/packaged/fixtures/sandbox-database-maintenance.sh b/tests/packaged/fixtures/sandbox-database-maintenance.sh new file mode 100644 index 0000000..d122c58 --- /dev/null +++ b/tests/packaged/fixtures/sandbox-database-maintenance.sh @@ -0,0 +1,15 @@ +# Runs inside the sandbox and starts host database maintenance with the same +# command the desktop uses after its confirmation dialog, which CI cannot click. +# Keep it in step with runHostMaintenance in patch 0011. +app=$(awk ' + /^\[/ { in_instance = ($0 == "[Instance]") } + in_instance && /^app-path=/ { sub(/^app-path=/, ""); print; exit } +' /.flatpak-info) +runtime="$app/t3code" + +exec /usr/bin/flatpak-spawn --host --watch-bus "--directory=$runtime" \ + "--env=T3CODE_HOME=$XDG_DATA_HOME/t3" \ + "--env=T3CODE_FLATPAK_SERVER=$runtime/t3code" \ + "--env=T3CODE_FLATPAK_ENTRY=$runtime/resources/app.asar/apps/server/dist/bin.mjs" \ + "--env=T3CODE_FLATPAK_COMPILE_CACHE=$runtime/resources/app.asar/apps/desktop/dist-electron/compileCache.cjs" \ + /bin/sh "$runtime/host-server.sh" --database-maintenance --compact --remove-legacy diff --git a/tests/packaged/host.ts b/tests/packaged/host.ts new file mode 100644 index 0000000..4ed2871 --- /dev/null +++ b/tests/packaged/host.ts @@ -0,0 +1,164 @@ +// Turns the disposable CI container into a stand-in for a user's desktop and installs the +// app there. Every step changes the system it runs on, so main.ts checks for a container first. +import { execFileSync, spawn } from "node:child_process"; +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; + +import { + APP_ID, + CALL_LOG, + artifactsDir, + checkout, + fixtureDatabase, + hostRepo, + startSessionHelper, + upstreamDir, + userdata, +} from "./app.ts"; + +const PUBLISHED_REPO = "https://t3code.thomasberrios.com/t3code.flatpakrepo"; +const LOGIN_BIN = "/opt/t3-test/bin"; +const FAKE_COMMAND = path.join(import.meta.dirname, "fixtures/fake-command"); + +export type AppSource = { repo: string } | { commit: string }; + +function run(command: string, ...args: string[]): string { + return execFileSync(command, args, { encoding: "utf8", stdio: ["ignore", "pipe", "inherit"] }); +} + +export function isDisposableContainer(): boolean { + return fs.existsSync("/.dockerenv") || fs.existsSync("/run/.containerenv"); +} + +/** Installs a local build's repository, or a commit from the published repository. */ +export function installApp(source: AppSource) { + if ("repo" in source) { + run("flatpak", "remote-add", "--system", "--no-gpg-verify", "t3code-test", path.resolve(source.repo)); + run("flatpak", "install", "--system", "-y", "--noninteractive", "t3code-test", `${APP_ID}//stable`); + return; + } + const repoFile = path.join(os.tmpdir(), "t3code.flatpakrepo"); + run("curl", "-fsSL", PUBLISHED_REPO, "-o", repoFile); + run("flatpak", "remote-add", "--system", "t3code", repoFile); + run("flatpak", "install", "--system", "-y", "--noninteractive", "t3code", APP_ID); + run("flatpak", "update", "--system", "-y", "--noninteractive", `--commit=${source.commit}`, APP_ID); +} + +function installFakeCommand(target: string) { + fs.mkdirSync(path.dirname(target), { recursive: true }); + fs.copyFileSync(FAKE_COMMAND, target); + fs.chmodSync(target, 0o755); +} + +/** + * Host tools the app should only reach outside the sandbox. Agents, Tailscale and VS Code + * come from the login shell's PATH, and ssh from the host service's default PATH. + */ +export function installFakeHostCommands() { + for (const name of ["claude", "codex", "tailscale", "code"]) { + installFakeCommand(path.join(LOGIN_BIN, name)); + } + installFakeCommand("/usr/local/bin/ssh"); + for (const file of [".profile", ".bashrc"]) { + fs.appendFileSync(path.join(os.homedir(), file), `export PATH=${LOGIN_BIN}:$PATH\n`); + } +} + +/** + * A stand-in Zed installed as a Flatpak. Flatpak apps put no command on the host PATH, so + * the host server can only find it through `flatpak list`. + */ +export function installFlatpakEditor() { + const appId = "dev.zed.Zed"; + const build = fs.mkdtempSync(path.join(os.tmpdir(), "editor-build-")); + const repo = fs.mkdtempSync(path.join(os.tmpdir(), "editor-repo-")); + // The app's own runtime is already installed, and a shell script needs nothing more. + const [runtime, , branch] = run("flatpak", "info", "--show-runtime", APP_ID).trim().split("/"); + if (!runtime || !branch) throw new Error(`${APP_ID} reports no runtime.`); + run("flatpak", "build-init", build, appId, runtime, runtime, branch); + installFakeCommand(path.join(build, "files/bin/zed")); + // Shares /tmp so the stand-in can write to the call log. + run("flatpak", "build-finish", build, "--command=zed", "--filesystem=/tmp"); + run("flatpak", "build-export", repo, build); + run("flatpak", "remote-add", "--system", "--no-gpg-verify", "t3code-test-editor", repo); + run("flatpak", "install", "--system", "-y", "--noninteractive", "t3code-test-editor", appId); +} + +/** Folders and config that every desktop session has. */ +export function configureDesktop() { + const home = os.homedir(); + fs.mkdirSync(path.join(home, "Downloads"), { recursive: true }); + fs.mkdirSync(path.join(home, ".config"), { recursive: true }); + fs.writeFileSync(path.join(home, ".config/user-dirs.dirs"), 'XDG_DOWNLOAD_DIR="$HOME/Downloads"\n'); + fs.mkdirSync(path.join(home, ".ssh"), { recursive: true }); + fs.writeFileSync(path.join(home, ".ssh/config"), "Host ci-box\n HostName 10.9.8.7\n User ci\n"); +} + +/** A git project outside every filesystem permission the sandbox has. */ +export function createHostRepo() { + fs.mkdirSync(hostRepo, { recursive: true }); + run("git", "-C", hostRepo, "init", "-q", "-b", "main"); + run("git", "-C", hostRepo, "-c", "user.name=t3", "-c", "user.email=t3@example.invalid", + "commit", "-q", "--allow-empty", "-m", "init"); +} + +/** Upstream's launch fixture and free replay agent, from the release the manifest packages. */ +export function fetchUpstreamFixtures() { + const manifest = fs.readFileSync(path.join(checkout, "flatpak/modules/t3code.yml"), "utf8"); + const tag = manifest.match(/^\s*tag:\s*(\S+)/m)?.[1]; + if (!tag) throw new Error("The manifest does not pin an upstream tag."); + run("git", "-c", "advice.detachedHead=false", "clone", "-q", "--depth", "1", "--filter=blob:none", + "--sparse", "--branch", tag, "https://github.com/pingdotgg/t3code.git", upstreamDir); + run("git", "-C", upstreamDir, "sparse-checkout", "set", "--no-cone", + "/scripts/fixtures/desktop-launch/", + "/apps/server/scripts/dev-replay-agent.ts", + "/apps/server/scripts/dev-replay/", + "/apps/server/src/orchestration-v2/testkit/fixtures/"); +} + +/** Seeds the fixture history and registers the replay agent the way upstream's dev servers do. */ +export function seedAppData() { + fs.mkdirSync(userdata, { recursive: true }); + fs.copyFileSync(fixtureDatabase, path.join(userdata, "statev2.sqlite")); + const settings = { + providerInstances: { + replay: { + displayName: "Replay", + driver: "acpRegistry", + config: { + source: "local", + commandPath: process.execPath, + commandArgs: [path.join(upstreamDir, "apps/server/scripts/dev-replay-agent.ts")], + }, + }, + }, + }; + fs.writeFileSync(path.join(userdata, "settings.json"), JSON.stringify(settings, null, 2)); +} + +/** + * Starts a headless display, a session bus, and Flatpak's host-command service. The app + * inherits them through this process's environment. + */ +export function startDesktopSession() { + const runtimeDir = `/run/user/${os.userInfo().uid}`; + fs.mkdirSync(runtimeDir, { recursive: true, mode: 0o700 }); + Object.assign(process.env, { DISPLAY: ":99", XDG_RUNTIME_DIR: runtimeDir, SHELL: "/bin/bash" }); + + spawn("Xvfb", [":99", "-screen", "0", "1280x800x24"], { stdio: "ignore", detached: true }).unref(); + process.env.DBUS_SESSION_BUS_ADDRESS = run("dbus-daemon", "--session", "--fork", "--print-address").trim(); + startSessionHelper(); +} + +/** Copies logs next to the failure screenshots, owned by the user who ran the checks. */ +export function collectArtifacts() { + fs.mkdirSync(artifactsDir, { recursive: true }); + for (const source of [path.join(userdata, "logs"), CALL_LOG]) { + if (fs.existsSync(source)) { + fs.cpSync(source, path.join(artifactsDir, path.basename(source)), { recursive: true }); + } + } + const owner = Number(process.env.HOST_UID); + if (Number.isInteger(owner)) run("chown", "-R", String(owner), artifactsDir); +} diff --git a/tests/packaged/main.ts b/tests/packaged/main.ts new file mode 100644 index 0000000..2c07c89 --- /dev/null +++ b/tests/packaged/main.ts @@ -0,0 +1,50 @@ +// Prepares the CI container as a stand-in desktop, then runs the packaged app checks. +import { spawnSync } from "node:child_process"; +import { parseArgs } from "node:util"; + +import { + type AppSource, + collectArtifacts, + configureDesktop, + createHostRepo, + fetchUpstreamFixtures, + installApp, + installFakeHostCommands, + installFlatpakEditor, + isDisposableContainer, + seedAppData, + startDesktopSession, +} from "./host.ts"; + +const { values } = parseArgs({ options: { repo: { type: "string" }, commit: { type: "string" } } }); +let source: AppSource | undefined; +if (values.repo) source = { repo: values.repo }; +if (values.commit) source = { commit: values.commit }; +if (!source) { + console.error("Usage: main.ts --repo | --commit "); + process.exit(2); +} +if (!isDisposableContainer()) { + console.error("These checks change the system they run on. Run them with scripts/test-packaged."); + process.exit(1); +} + +installApp(source); +installFakeHostCommands(); +installFlatpakEditor(); +configureDesktop(); +createHostRepo(); +fetchUpstreamFixtures(); +seedAppData(); +startDesktopSession(); + +try { + const tests = spawnSync( + process.execPath, + ["--test", "--test-concurrency=1", "packaged.test.ts"], + { cwd: import.meta.dirname, stdio: "inherit" }, + ); + process.exitCode = tests.status ?? 1; +} finally { + collectArtifacts(); +} diff --git a/tests/packaged/package-lock.json b/tests/packaged/package-lock.json new file mode 100644 index 0000000..280e90b --- /dev/null +++ b/tests/packaged/package-lock.json @@ -0,0 +1,420 @@ +{ + "name": "t3code-flatpak-packaged-tests", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "t3code-flatpak-packaged-tests", + "devDependencies": { + "@types/node": "24.19.2", + "playwright-core": "1.60.0", + "typescript": "7.0.2" + } + }, + "node_modules/@types/node": { + "version": "24.19.2", + "resolved": "https://registry.npmjs.org/@types/node/-/node-24.19.2.tgz", + "integrity": "sha512-93d49wCWJkCyN87MxLpNN6KXPCwFw7bVUg79mGUV1OS40JGWwVCr8n61DXCSqz7CV6/VfPkb7xec7uP2M+QHWQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "undici-types": ">=7.24.0 <7.24.7" + } + }, + "node_modules/@typescript/typescript-aix-ppc64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-aix-ppc64/-/typescript-aix-ppc64-7.0.2.tgz", + "integrity": "sha512-MTKKkWB7p/0E9xi1d1tHtZ5PiLkGEMIq88pK2CubZjOsLtYTLqhgIgi6zepFa+9GHZ6h05NMCkQxGKiPXMxXtQ==", + "cpu": [ + "ppc64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "aix" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-darwin-arm64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-darwin-arm64/-/typescript-darwin-arm64-7.0.2.tgz", + "integrity": "sha512-gowzar9MwS/aRWp6f3a4KUqzRjAZjOsmGNCM6LcTgXum+dBfgsBVMN+AgvOCCbguXyick6LJhpBszxMebJ8syA==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-darwin-x64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-darwin-x64/-/typescript-darwin-x64-7.0.2.tgz", + "integrity": "sha512-SZ9xZInqApNlNGc9s0W1VSsktYSOe9cFqNOIqmN1Gs8SmkjKZYFt017G4VwPxASInODuAdbTW7sXiFUf893RgA==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-freebsd-arm64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-freebsd-arm64/-/typescript-freebsd-arm64-7.0.2.tgz", + "integrity": "sha512-W5NH4y/J0plIIS5b2xvTEkU7JFxyqdMAOgf+Ilhl0vHQXKO5dZoxd+C/jEtq56c4F3wk71RB4BMRQ2XdI+bwYQ==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "freebsd" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-freebsd-x64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-freebsd-x64/-/typescript-freebsd-x64-7.0.2.tgz", + "integrity": "sha512-UMGDx5sTpzNw3WiPebH7l90IWfJggEd+egHt/q6p7/Cm3zqoV7VxkGXt+3DxPIw8CcmvAB0j3sVVfbhX+M4Tpw==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "freebsd" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-linux-arm": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-linux-arm/-/typescript-linux-arm-7.0.2.tgz", + "integrity": "sha512-gffT3xPz9sR7j/YJExkyPntrI0P2EP9XbOyWzth2/Gs0RstK+90RBcO0ncXoXy/beYll1SXw846Nf2zdnEz0QQ==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-linux-arm64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-linux-arm64/-/typescript-linux-arm64-7.0.2.tgz", + "integrity": "sha512-Qh4eU4/y3yDjnfjjyPYihMj5/ODIlmt+Bzu17OI+fiSRDW57QmU5SiN63exPRNJPKUzcc1INa1NXdrJ+MqHjUQ==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-linux-loong64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-linux-loong64/-/typescript-linux-loong64-7.0.2.tgz", + "integrity": "sha512-uEHck9i8hoAzXPiYRib1O7miOnz23SxIeVl6F4LXox+qov1K35jHcEW6VHKvZI+pyvl7fZEP4MCU5LYvIq1GuQ==", + "cpu": [ + "loong64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-linux-mips64el": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-linux-mips64el/-/typescript-linux-mips64el-7.0.2.tgz", + "integrity": "sha512-R4KvAMnE43W5Qeqb0Ly56O3mWMWIAgsMyz36DCaycd5nbg/9kzm0liw3JocfRqyJY0KPmzFjbswozXyW0DnIYA==", + "cpu": [ + "mips64el" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-linux-ppc64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-linux-ppc64/-/typescript-linux-ppc64-7.0.2.tgz", + "integrity": "sha512-DORx5b3sd/4S7eayxm4FQv+A7CrkUIGRaHiwI8oiHTAI1fAPWhF4J0vAlkC8biAlHSVVwxMQ3tjZ2/DVbnQiiA==", + "cpu": [ + "ppc64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-linux-riscv64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-linux-riscv64/-/typescript-linux-riscv64-7.0.2.tgz", + "integrity": "sha512-wf0jqEDOjrPRnKwYRyyJDRo11KMbvMFrU+q4zqKyChODBzvlkbhNQfKvLxQCcwTpdDaXSHZTVuh0JoCrKCUMHQ==", + "cpu": [ + "riscv64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-linux-s390x": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-linux-s390x/-/typescript-linux-s390x-7.0.2.tgz", + "integrity": "sha512-IkwJc3L7yhytWd/ewjyxNDfOmswCm9GWMJT/ue/dU4aZNbwZeYAetq42VyLmsmSjvoX7z74X6ZaYCtzAr0EuGw==", + "cpu": [ + "s390x" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-linux-x64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-linux-x64/-/typescript-linux-x64-7.0.2.tgz", + "integrity": "sha512-EYdf2cNg7rgCWJnxCdJ+F3V39O8ihb37eHAu1LK8oAFizgTQbPOK7zHHXbPt8rX24COqODXeI3sIf0fCXG7H/A==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-netbsd-arm64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-netbsd-arm64/-/typescript-netbsd-arm64-7.0.2.tgz", + "integrity": "sha512-+polYF4MF04aPpO5FTkHran9yUQDSXqy5GiSDKpsll5jy3l3+g9QLhpf39T+ePtefhXLOGrLl0QIjkQP6VnelA==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "netbsd" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-netbsd-x64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-netbsd-x64/-/typescript-netbsd-x64-7.0.2.tgz", + "integrity": "sha512-8YIT0EHM/3dq10ZOVF/A7pc/YSMtbcecct4rWtexrnSCHOPcpC2KTLXfTCR6vDpnSiY12heNb1GiN/wu+T/FyA==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "netbsd" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-openbsd-arm64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-openbsd-arm64/-/typescript-openbsd-arm64-7.0.2.tgz", + "integrity": "sha512-APT8+ClYnuYm1u9+kgGXoMj2VzWzcymwh2gNSQVySHfkRDGOTVkoWLjCmOQSaO+PoqQ57B0flRp9SA+7GnnkzQ==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "openbsd" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-openbsd-x64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-openbsd-x64/-/typescript-openbsd-x64-7.0.2.tgz", + "integrity": "sha512-yX7s+Q0Dln0Dt9tEzZsAjXXR/+ytBM7AlglaqyeMPxQszJ1JhlJdZ6jLA+IzldHtflX81em7lDao1xXu+aRRkg==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "openbsd" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-sunos-x64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-sunos-x64/-/typescript-sunos-x64-7.0.2.tgz", + "integrity": "sha512-dLJDGaLZ1D4HPQn62u1n8mBDkJREwMsAkCdkwd4Ieqw+x3TUyTsqY0YiBCtE6H6OzzgGk3iuZ3vFWRS+E8/d1g==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "sunos" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-win32-arm64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-win32-arm64/-/typescript-win32-arm64-7.0.2.tgz", + "integrity": "sha512-Gyl1Vy6OsWesLzmq+EP0Fb7b4Nid5232AvcA2SFcdYreldpNtYFFofPjnt62y9hQy7VTaZp65ICJjuAQRaVcIQ==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-win32-x64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-win32-x64/-/typescript-win32-x64-7.0.2.tgz", + "integrity": "sha512-0BQ3HkAHHlKLSp1qRvf3SUhGpGsDuhB/jgFw75guyqbxJqEaS0Cw/VFO8i2nHglJUzQCRtMMR/IBAKE3ETMC4g==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/playwright-core": { + "version": "1.60.0", + "resolved": "https://registry.npmjs.org/playwright-core/-/playwright-core-1.60.0.tgz", + "integrity": "sha512-9bW6zvX/m0lEbgTKJ6YppOKx8H3VOPBMOCFh2irXFOT4BbHgrx5hPjwJYLT40Lu+4qtD36qKc/Hn56StUW57IA==", + "dev": true, + "license": "Apache-2.0", + "bin": { + "playwright-core": "cli.js" + }, + "engines": { + "node": ">=18" + } + }, + "node_modules/typescript": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-7.0.2.tgz", + "integrity": "sha512-8FYau96o3NKOhbjKi/qNvG/W5jhzxkbdm5sj9AbZ/5T5sWqn3hJgLfGx27sRKZWTvyzCP8dLRBTf5tBTSRVUNA==", + "dev": true, + "license": "Apache-2.0", + "bin": { + "tsc": "bin/tsc" + }, + "engines": { + "node": ">=16.20.0" + }, + "optionalDependencies": { + "@typescript/typescript-aix-ppc64": "7.0.2", + "@typescript/typescript-darwin-arm64": "7.0.2", + "@typescript/typescript-darwin-x64": "7.0.2", + "@typescript/typescript-freebsd-arm64": "7.0.2", + "@typescript/typescript-freebsd-x64": "7.0.2", + "@typescript/typescript-linux-arm": "7.0.2", + "@typescript/typescript-linux-arm64": "7.0.2", + "@typescript/typescript-linux-loong64": "7.0.2", + "@typescript/typescript-linux-mips64el": "7.0.2", + "@typescript/typescript-linux-ppc64": "7.0.2", + "@typescript/typescript-linux-riscv64": "7.0.2", + "@typescript/typescript-linux-s390x": "7.0.2", + "@typescript/typescript-linux-x64": "7.0.2", + "@typescript/typescript-netbsd-arm64": "7.0.2", + "@typescript/typescript-netbsd-x64": "7.0.2", + "@typescript/typescript-openbsd-arm64": "7.0.2", + "@typescript/typescript-openbsd-x64": "7.0.2", + "@typescript/typescript-sunos-x64": "7.0.2", + "@typescript/typescript-win32-arm64": "7.0.2", + "@typescript/typescript-win32-x64": "7.0.2" + } + }, + "node_modules/undici-types": { + "version": "7.24.6", + "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-7.24.6.tgz", + "integrity": "sha512-WRNW+sJgj5OBN4/0JpHFqtqzhpbnV0GuB+OozA9gCL7a993SmU+1JBZCzLNxYsbMfIeDL+lTsphD5jN5N+n0zg==", + "dev": true, + "license": "MIT" + } + } +} diff --git a/tests/packaged/package.json b/tests/packaged/package.json new file mode 100644 index 0000000..4f32e10 --- /dev/null +++ b/tests/packaged/package.json @@ -0,0 +1,13 @@ +{ + "name": "t3code-flatpak-packaged-tests", + "private": true, + "type": "module", + "scripts": { + "typecheck": "tsc" + }, + "devDependencies": { + "@types/node": "24.19.2", + "playwright-core": "1.60.0", + "typescript": "7.0.2" + } +} diff --git a/tests/packaged/packaged.test.ts b/tests/packaged/packaged.test.ts new file mode 100644 index 0000000..981723e --- /dev/null +++ b/tests/packaged/packaged.test.ts @@ -0,0 +1,292 @@ +// Checks the installed Flatpak where the sandbox and host meet. The tests share one +// sequence of app launches and run in order: later steps build on earlier state. +import assert from "node:assert/strict"; +import fs from "node:fs"; +import http from "node:http"; +import os from "node:os"; +import path from "node:path"; +import { after, before, test } from "node:test"; +import type { Browser, Page } from "playwright-core"; + +import { + APP_ID, + appProcesses, + artifactsDir, + backendPort, + closeApp, + connect, + fixtureDatabase, + hostCalls, + hostRepo, + hostServerPid, + launchApp, + listeningAddresses, + mountNamespace, + openSettings, + runBundledCli, + runInSandbox, + startSessionHelper, + stopSessionHelper, + userdata, + waitUntil, +} from "./app.ts"; + +// Upstream's desktop launch fixture, an older database the packaged server must migrate. +const FIXTURE_THREAD = "Release launch check"; +const FIXTURE_MESSAGE = "The release opens and shows this thread."; +const POLICY_PATH = "/etc/t3code/policy.json"; + +let browser: Browser | undefined; +let page: Page; + +async function attach(urlPrefix?: string) { + await browser?.close().catch(() => {}); + ({ browser, page } = await connect(urlPrefix)); +} + +/** A test that leaves a screenshot of the window behind when it fails. */ +function appTest(name: string, run: () => Promise) { + test(name, async () => { + try { + // Close anything an earlier failure left open, such as a popover. + if (!page.isClosed()) await page.keyboard.press("Escape"); + await run(); + } catch (error) { + const file = `${name.replace(/[^a-z0-9]+/gi, "-").toLowerCase()}.png`; + await page?.screenshot({ path: path.join(artifactsDir, file) }).catch(() => {}); + throw error; + } + }); +} + +async function openSeededThread() { + await page.goto("t3code://app/#/"); + await page.locator("li[data-thread-item]", { hasText: FIXTURE_THREAD }).click(); + await page.getByText(FIXTURE_MESSAGE).waitFor(); +} + +before(async () => { + launchApp(); + await attach(); +}); + +after(async () => { + if (page && !page.isClosed()) await closeApp(page).catch(() => {}); + await browser?.close().catch(() => {}); +}); + +appTest("opens and migrates the seeded history", openSeededThread); + +appTest("runs its server on the host, outside the sandbox", async () => { + const pid = await waitUntil("the host server", hostServerPid); + assert.equal(mountNamespace(pid), mountNamespace("self")); +}); + +appTest("finds agent CLIs that only the host login shell knows", async () => { + const dialog = await openSettings(page, "providers"); + await waitUntil("both agents to report a version", async () => { + const text = await dialog.innerText(); + return /Codex\s+v9\.9\.9/.test(text) && /Claude\s+v9\.9\.9/.test(text); + }); + const calls = hostCalls(); + assert.ok(calls.includes("claude --version"), calls.join("\n")); + assert.ok(calls.includes("codex app-server"), calls.join("\n")); +}); + +appTest("leaves updates to Flatpak and offers no host installs", async () => { + const about = await openSettings(page, "about"); + await about.getByText("Updates are managed by Flatpak.").first().waitFor(); + assert.ok(await about.getByLabel("Update track").isDisabled()); + + const general = await openSettings(page, "general"); + await general.getByText("Notifications").first().waitFor(); + assert.doesNotMatch(await general.innerText(), /from any terminal/); +}); + +appTest("saves preview downloads to the host Downloads folder", async () => { + const content = "t3code-flatpak download check\n"; + const server = http + .createServer((request, response) => { + if (request.url === "/file") { + response.writeHead(200, { + "Content-Type": "application/octet-stream", + "Content-Disposition": 'attachment; filename="t3-download-check.txt"', + }); + response.end(content); + return; + } + response.writeHead(200, { "Content-Type": "text/html" }); + // A large link at the top leaves blank space below it for the focusing click. + response.end( + 'Download fixtureDownload', + ); + }) + .listen(8765, "127.0.0.1"); + try { + await openSeededThread(); + const address = page.getByPlaceholder("Search or enter URL"); + if (!(await address.isVisible())) { + if (!(await page.getByText("Open a surface").isVisible())) { + await page.getByRole("button", { name: "Toggle right panel" }).click(); + } + await page.getByText("Browser", { exact: true }).first().click(); + } + await address.fill("http://127.0.0.1:8765/"); + await address.press("Enter"); + + await page.locator("webview").first().waitFor(); + await page.getByText("Download fixture").first().waitFor(); + + // Downloads only count when a person starts them, so click with the mouse. The first + // click into the webview only focuses it, so make that one on blank space. + const view = await page.locator("webview").first().boundingBox(); + assert.ok(view, "The preview webview is not on screen."); + await page.mouse.click(view.x + view.width / 2, view.y + view.height - 20); + + // The desktop treats a download within 5 seconds of the server driving the tab as the + // agent's and keeps it out of Downloads. Click until one lands, spacing the clicks past + // that window. + const saved = path.join(os.homedir(), "Downloads/t3-download-check.txt"); + await waitUntil("the download in ~/Downloads", async () => { + await page.mouse.click(view.x + 50, view.y + 100); + await page.waitForTimeout(6_000); + return fs.existsSync(saved); + }); + assert.equal(fs.readFileSync(saved, "utf8"), content); + } finally { + server.close(); + } +}); + +appTest("runs a free agent turn on the host and shows it", async () => { + // The UI adds projects through a portal file chooser, so start the thread with + // the bundled CLI against the running server instead. + const baseDir = path.dirname(userdata); + runBundledCli(["project", "add", "--base-dir", baseDir, "--title", "Host Repo", hostRepo]); + runBundledCli([ + "thread", "start", "--base-dir", baseDir, "--title", "Host tour", + "--provider", "replay", "--model", "showcase", hostRepo, "Give me the tour", + ]); + await page.goto("t3code://app/#/"); + await page.locator("li[data-thread-item]", { hasText: "Host tour" }).click(); + await page.getByText("and the tests pass").first().waitFor({ timeout: 90_000 }); +}); + +appTest("runs terminals and git in a project only the host can see", async () => { + const terminal = page.locator('[class*="terminal-background"]').first(); + if (!(await terminal.isVisible())) { + await page.getByRole("button", { name: "Toggle terminal drawer" }).click(); + } + await terminal.waitFor(); + await terminal.click(); + const marker = path.join(hostRepo, ".t3-terminal-check"); + await page.keyboard.type(`pwd > ${marker}; git branch --show-current >> ${marker}\n`); + + await waitUntil("the terminal command on the host", () => + fs.existsSync(marker) && fs.readFileSync(marker, "utf8").includes("main"), + ); + assert.equal(fs.readFileSync(marker, "utf8"), `${hostRepo}\nmain\n`); +}); + +appTest("opens the project in editors on the host PATH and installed as Flatpaks", async () => { + // VS Code is a fake command on the login PATH, and Zed a stand-in Flatpak app. + await page.getByRole("button", { name: "Toggle thread details panel" }).click(); + for (const editor of ["VS Code", "Zed"]) { + await page.getByRole("button", { name: "Choose editor" }).click(); + await page.getByRole("menuitem", { name: editor }).click(); + await page.getByRole("button", { name: `Open in ${editor}` }).click(); + } + await page.keyboard.press("Escape"); + await waitUntil("both editors to open the project", () => { + const calls = hostCalls(); + return calls.includes(`code ${hostRepo}`) && calls.includes(`zed ${hostRepo}`); + }); +}); + +appTest("reaches the host's SSH through the wrapper", async () => { + // The app's SSH flows start with these calls. Running them in the sandbox directly keeps + // the check free of the connection state the app would save. + const result = runInSandbox("cat ~/.ssh/config && ssh -G ci-box"); + assert.match(result.output, /HostName 10\.9\.8\.7/); + assert.ok(hostCalls().includes("ssh -G ci-box"), hostCalls().join("\n")); +}); + +appTest("relaunches with Network access and reads the host's Tailscale", async () => { + const access = await openSettings(page, "access"); + await access.getByRole("switch", { name: "Enable network access" }).click(); + const previousRun = appProcesses(); + await page.getByRole("button", { name: "Restart and enable" }).click(); + // Until the old run exits, its closing window can still answer on the debugging port. + await waitUntil( + "the previous run to exit", + () => !appProcesses().some((pid) => previousRun.includes(pid)), + 30_000, + ); + await attach(); + + const relaunched = await openSettings(page, "access"); + await relaunched.getByText("Reachable at").waitFor(); + await relaunched.getByText("Use Tailscale Serve").waitFor(); + assert.ok(hostCalls().includes("tailscale status --json")); + // The later policy check relies on Network access really reaching the server. + const reachable = (await relaunched.innerText()).match(/Reachable at\s+http:\/\/\S+:(\d+)/); + assert.ok(reachable, "Access settings show no address with a port."); + const addresses = listeningAddresses(Number(reachable[1])); + assert.ok(addresses.some((address) => address === "0.0.0.0" || address === "::"), addresses.join()); +}); + +appTest("stops the host server when the window closes", async () => { + await closeApp(page); + assert.equal(hostServerPid(), undefined); +}); + +appTest("starts local-only under a host policy despite saved Network access", async () => { + fs.mkdirSync(path.dirname(POLICY_PATH), { recursive: true }); + fs.writeFileSync( + POLICY_PATH, + JSON.stringify({ + schemaVersion: 1, + organization: "Packaged Test Org", + limits: { clients: ["desktop", "local-browser"] }, + }), + ); + try { + const log = launchApp(); + await attach(); + const port = await backendPort(log); + const access = await openSettings(page, "access"); + await access.getByText("managed by Packaged Test Org").waitFor(); + assert.deepEqual(listeningAddresses(port), ["127.0.0.1"]); + } finally { + fs.rmSync(POLICY_PATH); + } +}); + +appTest("cleans up the database through host-server.sh", async () => { + // CI cannot click the desktop's native confirmation, so this runs the host wrapper + // and server command it starts. The desktop also stops its server first. + await closeApp(page); + const legacy = path.join(userdata, "state.sqlite"); + fs.copyFileSync(fixtureDatabase, legacy); + const result = runInSandbox( + fs.readFileSync(path.join(import.meta.dirname, "fixtures/sandbox-database-maintenance.sh"), "utf8"), + ); + assert.equal(result.status, 0, result.output); + assert.match(result.output, /Deleted the database from before 0\.1\.0\./); + assert.ok(!fs.existsSync(legacy)); +}); + +appTest("shows Flatpak recovery steps while the host is out of reach, then recovers", async () => { + await stopSessionHelper(); + try { + launchApp(); + await attach("data:text/html"); + await page.getByText(`flatpak update ${APP_ID}`).waitFor(); + } finally { + startSessionHelper(); + } + // The desktop keeps retrying its server, so it starts once the host is back. The seeded + // thread also shows the history survived the restarts and database maintenance. + await attach(); + await openSeededThread(); +}); diff --git a/tests/packaged/run.sh b/tests/packaged/run.sh new file mode 100755 index 0000000..266687c --- /dev/null +++ b/tests/packaged/run.sh @@ -0,0 +1,14 @@ +#!/bin/sh +# Runs the packaged app checks in the CI container image. Validation calls it after +# the build, and scripts/test-packaged runs it locally. Arguments go to main.ts. +set -eu + +here=$(cd -- "$(dirname -- "$0")" && pwd) +export T3_TEST_CHECKOUT="$here/../.." + +# Install dependencies in a copy so a read-only checkout stays untouched. +work=$(mktemp -d) +cp -r "$here/." "$work" +# npm ships with Node, so this needs nothing beyond the image's Node. +(cd "$work" && npm ci --silent --no-audit --no-fund && npm run --silent typecheck) +exec node "$work/main.ts" "$@" diff --git a/tests/packaged/tsconfig.json b/tests/packaged/tsconfig.json new file mode 100644 index 0000000..d3be475 --- /dev/null +++ b/tests/packaged/tsconfig.json @@ -0,0 +1,16 @@ +{ + "compilerOptions": { + "target": "esnext", + "module": "nodenext", + "moduleResolution": "nodenext", + "types": ["node"], + "strict": true, + "noEmit": true, + "allowImportingTsExtensions": true, + "erasableSyntaxOnly": true, + "verbatimModuleSyntax": true, + "noUncheckedIndexedAccess": true, + "skipLibCheck": true + }, + "include": ["*.ts"] +} From 2595ac5b6e0adfc5c1682cbb9dd68bd715d1cdcc Mon Sep 17 00:00:00 2001 From: Thomas Date: Fri, 6 Nov 2026 18:52:51 -0500 Subject: [PATCH 3/3] Trim patch tests that only mirror their code Drop the display name label check from 0001 and the pass-through field checks from 0002, keeping the assertions for what each patch changes. --- patches/0001-flatpak-desktop-integration.patch | 7 +++---- patches/0002-flatpak-host-backend.patch | 12 ++---------- 2 files changed, 5 insertions(+), 14 deletions(-) diff --git a/patches/0001-flatpak-desktop-integration.patch b/patches/0001-flatpak-desktop-integration.patch index 865368f..f894629 100644 --- a/patches/0001-flatpak-desktop-integration.patch +++ b/patches/0001-flatpak-desktop-integration.patch @@ -33,21 +33,20 @@ index 3c571bcf02..4f1cf91396 100644 preference, env: input.env, diff --git a/apps/desktop/src/app/DesktopEnvironment.test.ts b/apps/desktop/src/app/DesktopEnvironment.test.ts -index 200745f111..2b9aa94ef0 100644 +index 200745f111..b0f21ccc7d 100644 --- a/apps/desktop/src/app/DesktopEnvironment.test.ts +++ b/apps/desktop/src/app/DesktopEnvironment.test.ts -@@ -38,6 +38,18 @@ const makeEnvironment = ( +@@ -38,6 +38,17 @@ const makeEnvironment = ( ) => DesktopEnvironment.DesktopEnvironment.pipe(Effect.provide(layerEnvironment(overrides, env))); describe("DesktopEnvironment", () => { -+ it.effect("uses a distinct Flatpak display name", () => ++ it.effect("names the Flatpak desktop entry after the app ID", () => + Effect.gen(function* () { + const environment = yield* makeEnvironment( + { platform: "linux" }, + { FLATPAK_ID: "com.t3tools.t3code" }, + ); + -+ assert.equal(environment.displayName, "T3 Code (Flatpak)"); + assert.equal(environment.linuxDesktopEntryName, "com.t3tools.t3code.desktop"); + }), + ); diff --git a/patches/0002-flatpak-host-backend.patch b/patches/0002-flatpak-host-backend.patch index f205bd9..a43b207 100644 --- a/patches/0002-flatpak-host-backend.patch +++ b/patches/0002-flatpak-host-backend.patch @@ -38,10 +38,10 @@ index 69a70b68a4..bcf48615be 100644 diff --git a/apps/desktop/src/backend/flatpakBackend.test.ts b/apps/desktop/src/backend/flatpakBackend.test.ts new file mode 100644 -index 0000000000..cfc536d2e7 +index 0000000000..229b483064 --- /dev/null +++ b/apps/desktop/src/backend/flatpakBackend.test.ts -@@ -0,0 +1,86 @@ +@@ -0,0 +1,78 @@ +import { assert, describe, it } from "@effect/vitest"; +import * as Option from "effect/Option"; + @@ -104,20 +104,12 @@ index 0000000000..cfc536d2e7 + `${runtime}/host-server.sh`, + ]); + assert.equal(result.cwd, "/app/t3code"); -+ assert.equal(result.entryPath, config.entryPath); + assert.deepEqual(result.env, {}); -+ assert.equal(result.bootstrapDelivery, "fd3"); -+ assert.equal(result.bootstrap.desktopBootstrapToken, "test-token"); -+ assert.equal(result.bootstrap.desktopTelemetryFd, 4); -+ assert.equal(result.bootstrap.desktopTelemetryControlFd, 5); + assert.isFalse(result.bootstrap.shellEnvironmentPrepared); -+ assert.equal(result.bootstrap.desktopBrowserFd, 6); -+ assert.equal(result.bootstrap.desktopBrowserControlFd, 7); + assert.equal( + result.bootstrap.resourceMonitorPath, + `${runtime}/resources/resource-monitor/t3-resource-monitor`, + ); -+ assert.equal(result.httpBaseUrl, config.httpBaseUrl); + }); + + it("fails preflight instead of falling back to a sandbox backend when the deployment path is missing", () => {