Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
262 changes: 262 additions & 0 deletions apps/desktop/src/electron/SourceUpdater.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,262 @@
import { assert, describe, it } from "@effect/vitest";
import type { SourceUpdateConfig, SourceUpdateMessage } from "@t3tools/shared/sourceUpdate";
import * as Effect from "effect/Effect";
import * as Exit from "effect/Exit";
import * as Option from "effect/Option";

import * as ElectronUpdater from "./ElectronUpdater.ts";
import * as SourceUpdater from "./SourceUpdater.ts";

const config: SourceUpdateConfig = {
checkout: "/repo",
outputDir: "/repo/release",
};

interface FakeHostOptions {
readonly buildCommit?: string | undefined;
/** stdout lines by subcommand; messages are JSON lines, strings are log lines. */
readonly output: Record<string, ReadonlyArray<SourceUpdateMessage | string>>;
readonly exitCode?: number;
readonly stderr?: string;
}

function makeFakeHost(options: FakeHostOptions) {
const runs: Array<ReadonlyArray<string>> = [];
const detached: Array<ReadonlyArray<string>> = [];
let quits = 0;
const host: SourceUpdater.SourceUpdateHost = {
config,
configPath: "/app/Contents/Resources/source-update.json",
buildCommit: Option.fromNullishOr(
"buildCommit" in options ? options.buildCommit : "abcdef123456",
),
appBundle: "/Applications/T3 Code.app",
arch: "arm64",
pid: 4242,
run: (args, onLine) =>
Effect.sync(() => {
runs.push(args);
for (const line of options.output[args[0] ?? ""] ?? []) {
onLine(typeof line === "string" ? line : JSON.stringify(line));
}
return { exitCode: options.exitCode ?? 0, stderr: options.stderr ?? "" };
}),
spawnDetached: (args) =>
Effect.sync(() => {
detached.push(args);
}),
quit: Effect.sync(() => {
quits += 1;
}),
};
return { host, runs, detached, quits: () => quits };
}

const collectEvents = (updater: ElectronUpdater.ElectronUpdater["Service"]) =>
Effect.gen(function* () {
const events: Array<{ readonly name: string; readonly payload: unknown }> = [];
for (const name of [
"checking-for-update",
"update-available",
"update-not-available",
"download-progress",
"update-downloaded",
]) {
yield* updater.on(name, (payload: unknown) => {
events.push({ name, payload });
});
}
return events;
});

describe("SourceUpdater", () => {
it.effect("offers the branch's new commits as a stable-channel version", () =>
Effect.scoped(
Effect.gen(function* () {
const fake = makeFakeHost({
output: {
describe: [
"[source-update] fetching",
{
type: "update",
target: "0123456789abcdef0123456789abcdef01234567",
version: "0.1.13-source.20261204.4821",
releaseDate: "2026-12-04T10:00:00.000Z",
releaseNotes: "- feat: one\n- fix: two",
commitCount: 2,
url: "https://github.com/TrogonStack/t3code/commit/0123456789abcdef0123456789abcdef01234567",
},
],
},
});
const updater = SourceUpdater.make(fake.host);
const events = yield* collectEvents(updater);

yield* updater.checkForUpdates;

assert.deepEqual(fake.runs, [
["describe", "--config", fake.host.configPath, "--from", "abcdef123456"],
]);
assert.deepEqual(events, [
{ name: "checking-for-update", payload: undefined },
{
name: "update-available",
payload: {
version: "0.1.13-source.20261204.4821",
releaseNotes: [
{
version: "0.1.13-source.20261204.4821",
note: "- feat: one\n- fix: two",
url: "https://github.com/TrogonStack/t3code/commit/0123456789abcdef0123456789abcdef01234567",
},
],
releaseName: "T3 Code 0.1.13-source.20261204.4821",
releaseDate: "2026-12-04T10:00:00.000Z",
},
},
]);
}),
),
);

it.effect("reports no update when the build already has the branch's head", () =>
Effect.scoped(
Effect.gen(function* () {
const fake = makeFakeHost({
output: { describe: [{ type: "up-to-date", target: "abcdef123456" }] },
});
const updater = SourceUpdater.make(fake.host);
const events = yield* collectEvents(updater);
yield* updater.checkForUpdates;
assert.deepEqual(
events.map((event) => event.name),
["checking-for-update", "update-not-available"],
);
}),
),
);

it.effect("fails the check with the script's own error line", () =>
Effect.scoped(
Effect.gen(function* () {
const fake = makeFakeHost({
output: { describe: [{ type: "error", message: "git fetch failed: no network" }] },
exitCode: 1,
stderr: "ignored when the script explains itself",
});
const updater = SourceUpdater.make(fake.host);
const exit = yield* Effect.exit(updater.checkForUpdates);
assert.isTrue(Exit.isFailure(exit));
const error = Exit.isFailure(exit) ? exit.cause : undefined;
assert.include(String(error), "git fetch failed: no network");
}),
),
);

it.effect("refuses to check a build that records no commit", () =>
Effect.scoped(
Effect.gen(function* () {
const fake = makeFakeHost({ buildCommit: undefined, output: {} });
const updater = SourceUpdater.make(fake.host);
const exit = yield* Effect.exit(updater.checkForUpdates);
assert.isTrue(Exit.isFailure(exit));
assert.deepEqual(fake.runs, []);
}),
),
);

it.effect("rebuilds the offered commit, relaying progress, then installs the staged app", () =>
Effect.scoped(
Effect.gen(function* () {
const fake = makeFakeHost({
output: {
describe: [
{
type: "update",
target: "0123456789abcdef0123456789abcdef01234567",
version: "0.1.13-source.20261204.4821",
releaseDate: "2026-12-04T10:00:00.000Z",
releaseNotes: "- feat: one",
commitCount: 1,
},
],
update: [
{ type: "progress", percent: 10, step: "rebasing" },
"{ not a message",
{ type: "progress", percent: 90, step: "staging" },
{
type: "built",
app: "/repo/release/app/T3 Code.app",
version: "0.1.13-source.20261204.4821",
},
],
},
});
const updater = SourceUpdater.make(fake.host);
const events = yield* collectEvents(updater);

yield* updater.checkForUpdates;
yield* updater.downloadUpdate;
yield* updater.quitAndInstall({ isSilent: true, isForceRunAfter: true });

assert.deepEqual(fake.runs[1], [
"update",
"--config",
fake.host.configPath,
"--from",
"abcdef123456",
"--target",
"0123456789abcdef0123456789abcdef01234567",
"--version",
"0.1.13-source.20261204.4821",
"--arch",
"arm64",
]);
assert.deepEqual(events.slice(2), [
{ name: "download-progress", payload: { percent: 10 } },
{ name: "download-progress", payload: { percent: 90 } },
{
name: "update-downloaded",
payload: {
version: "0.1.13-source.20261204.4821",
downloadedFile: "/repo/release/app/T3 Code.app",
},
},
]);
assert.equal(fake.detached.length, 1);
const install = fake.detached[0]!;
assert.deepEqual(install.slice(0, 7), [
"install",
"--pid",
"4242",
"--current",
"/Applications/T3 Code.app",
"--staged",
"/repo/release/app/T3 Code.app",
]);
assert.equal(install[7], "--backup-dir");
assert.match(install[8]!, /^\/repo\/release\/backup-\d{8}T\d{6}$/);
assert.equal(install[9], "--relaunch");
assert.equal(fake.quits(), 1);
}),
),
);

it.effect("refuses to build or install without an offer or a staged app", () =>
Effect.scoped(
Effect.gen(function* () {
const fake = makeFakeHost({ output: {} });
const updater = SourceUpdater.make(fake.host);
const download = yield* Effect.exit(updater.downloadUpdate);
assert.isTrue(Exit.isFailure(download));
const install = yield* Effect.exit(
updater.quitAndInstall({ isSilent: true, isForceRunAfter: true }),
);
assert.isTrue(Exit.isFailure(install));
assert.deepEqual(fake.runs, []);
assert.deepEqual(fake.detached, []);
assert.equal(fake.quits(), 0);
}),
),
);
});
Loading
Loading