Skip to content
Merged
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
130 changes: 126 additions & 4 deletions apps/server/src/cloud/bootService.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { DESKTOP_UPDATE_RESTART_MARKER_FILE } from "@t3tools/contracts";
// @effect-diagnostics-next-line nodeBuiltinImport:off -- Cross-process launcher contention uses real filesystem leases and IPC barriers.
import * as NodeChildProcess from "node:child_process";
import * as NodeSqlite from "node:sqlite";
Expand Down Expand Up @@ -204,11 +205,14 @@ const makeHarness = Effect.fn("test.make_boot_service_harness")(function* (
yield* fs.writeFileString(runtime.entryPath, "#!/bin/sh\n");
yield* fs.writeFileString(runtime.sentinelPath, "1.2.3\n");

const markersAtStop: boolean[] = [];
const markersAtPreparation: boolean[] = [];
const commands: string[] = [];
const invocations: ProcessRunner.ProcessRunInput[] = [];
const timeouts = new Map<string, unknown>();
const control: {
failCommand: string | undefined;
interruptCommand?: string;
stateAfterStop?: string;
/** `undefined` makes `loginctl show-user` fail, as when logind does not know the user. */
linger: string | undefined;
Expand Down Expand Up @@ -240,6 +244,23 @@ const makeHarness = Effect.fn("test.make_boot_service_harness")(function* (
) {
const command = `${input.command} ${input.args.join(" ")}`;
commands.push(command);
const marker = path.join(baseDir, "runtime", DESKTOP_UPDATE_RESTART_MARKER_FILE);
if (
command === "systemctl --user stop t3code.service" ||
command === "systemctl --user disable --now t3code.service" ||
command === "systemctl --user restart --no-block t3code.service" ||
command.startsWith("launchctl bootout --wait ") ||
(input.command === "schtasks" && input.args[0] === "/End")
) {
markersAtStop.push(yield* fs.exists(marker));
}
if (
command === "systemctl --user daemon-reload" ||
(input.command === "schtasks" && input.args[0] === "/Create")
) {
markersAtPreparation.push(yield* fs.exists(marker));
}
if (command === control.interruptCommand) return yield* Effect.interrupt;
invocations.push(input);
timeouts.set(command, input.timeout);
if (command === "systemctl --user reset-failed t3code.service") control.startLimitHit = false;
Expand Down Expand Up @@ -403,12 +424,51 @@ const makeHarness = Effect.fn("test.make_boot_service_harness")(function* (
commands,
invocations,
timeouts,
markersAtStop,
markersAtPreparation,
control,
runtime,
};
});

it.layer(NodeServices.layer)("boot service install", (it) => {
it.effect(
"marks immediate and deferred service restarts without blocking on a marker error",
() =>
Effect.gen(function* () {
const { service, fs, baseDir, control, markersAtStop, makeService } = yield* makeHarness();
const path = yield* Path.Path;
const marker = path.join(baseDir, "runtime", DESKTOP_UPDATE_RESTART_MARKER_FILE);
yield* service.install();
expect(markersAtStop).toEqual([]);
expect(yield* fs.exists(marker)).toBe(false);

const newer = yield* makeService(undefined, "1.2.4");
yield* newer.install();
expect(markersAtStop).toEqual([true]);
expect(yield* fs.exists(marker)).toBe(false);

const next = yield* makeService(undefined, "1.2.5");
yield* next.install({ start: false });
expect(markersAtStop).toEqual([true]);
expect(yield* fs.exists(marker)).toBe(false);
yield* next.restart;
expect(markersAtStop).toEqual([true, true]);
expect(yield* fs.exists(marker)).toBe(false);

control.failCommand = "systemctl --user stop t3code.service";
const error = yield* next.restart.pipe(Effect.flip);
expect(error._tag).toBe("BootServiceCommandError");
expect(markersAtStop).toEqual([true, true, true]);
expect(yield* fs.exists(marker)).toBe(false);

control.failCommand = undefined;
yield* fs.makeDirectory(marker);
expect(yield* next.restart).toBe("restarted");
expect((yield* fs.stat(marker)).type).toBe("Directory");
}),
);

it.effect(
"fails before installing files or validating a runtime when lingering needs an administrator",
() =>
Expand Down Expand Up @@ -1158,12 +1218,13 @@ process.disconnect();

it.effect("pauses only this home's running service for maintenance, even when it fails", () =>
Effect.gen(function* () {
const { service, commands, control, makeService } = yield* makeHarness();
const { service, commands, control, makeService, markersAtStop } = yield* makeHarness();
yield* service.install();
const failure = yield* service
.withStopped(Effect.failSync(() => (control.active ? "running" : "repair failed")))
.pipe(Effect.flip);
expect(failure).toBe("repair failed");
expect(markersAtStop).toEqual([false]);
expect(control.active).toBe(true);

control.failCommand = "systemctl --user stop t3code.service";
Expand Down Expand Up @@ -1299,9 +1360,19 @@ process.disconnect();
"queues an in-service restart without stopping the caller, even on handoff failure",
() =>
Effect.gen(function* () {
const { service, fs, statePath, commands, control, makeService } = yield* makeHarness();
const {
service,
fs,
statePath,
commands,
control,
makeService,
markersAtStop,
markersAtPreparation,
} = yield* makeHarness();
const plan = yield* service.install();
const state = yield* fs.readFileString(statePath);
const relayMarker = `${plan.baseDir}/runtime/${DESKTOP_UPDATE_RESTART_MARKER_FILE}`;
const marker = `${plan.baseDir}/runtime/${SERVICE_RESTART_PENDING_FILE}`;
yield* fs.writeFileString(marker, "1.2.3\n");
const inside = yield* makeService(undefined, "1.2.3", undefined, true);
Expand All @@ -1312,6 +1383,23 @@ process.disconnect();
expect(error).toMatchObject({ _tag: "BootServiceCommandError", exitCode: 1 });
expect(commands).toEqual(["systemctl --user daemon-reload"]);
expect(control.active).toBe(true);
expect(yield* fs.exists(relayMarker)).toBe(false);
expect(markersAtPreparation.at(-1)).toBe(false);
expect(markersAtStop).toEqual([]);

const queueCommand = "systemctl --user restart --no-block t3code.service";
control.failCommand = queueCommand;
expect((yield* inside.restart.pipe(Effect.flip))._tag).toBe("BootServiceCommandError");
expect(markersAtStop).toEqual([true]);
expect(yield* fs.exists(relayMarker)).toBe(false);

control.failCommand = undefined;
control.interruptCommand = queueCommand;
const interrupted = yield* inside.restart.pipe(Effect.forkScoped);
expect((yield* Fiber.await(interrupted))._tag).toBe("Failure");
expect(markersAtStop).toEqual([true, true]);
expect(yield* fs.exists(relayMarker)).toBe(false);
delete control.interruptCommand;

control.failCommand = undefined;
commands.length = 0;
Expand All @@ -1323,6 +1411,9 @@ process.disconnect();
"systemctl --user restart --no-block t3code.service",
]);
expect(outcome).toBe("queued");
expect(markersAtStop).toEqual([true, true, true]);
expect(markersAtPreparation).not.toContain(true);
expect(yield* fs.exists(relayMarker)).toBe(true);
expect(yield* fs.readFileString(statePath)).toBe(state);
expect(yield* fs.readFileString(marker)).toBe("1.2.3\n");
}),
Expand Down Expand Up @@ -1406,7 +1497,8 @@ process.disconnect();

it.effect("installs, reports current state, and uninstalls on macOS", () =>
Effect.gen(function* () {
const { service, fs, statePath, commands, timeouts, runtime } = yield* makeHarness("darwin");
const { service, fs, statePath, commands, timeouts, runtime, markersAtStop } =
yield* makeHarness("darwin");
const path = yield* Path.Path;
const plan = yield* service.install();

Expand All @@ -1429,7 +1521,13 @@ process.disconnect();
current: true,
installedVersion: "1.2.3",
});
const relayMarker = path.join(plan.baseDir, "runtime", DESKTOP_UPDATE_RESTART_MARKER_FILE);
expect(yield* service.restart).toBe("restarted");
expect(markersAtStop).toEqual([true]);
expect(yield* fs.exists(relayMarker)).toBe(false);
expect(yield* service.uninstall).toBe(true);
expect(markersAtStop).toEqual([true, false]);
expect(yield* fs.exists(relayMarker)).toBe(false);
expect((yield* service.status).installed).toBe(false);
expect(commands.some((command) => command.startsWith("systemctl "))).toBe(false);
// launchd can take 90s to bootout; the runner's 60s default could let bootstrap race it.
Expand Down Expand Up @@ -1532,9 +1630,19 @@ process.disconnect();

it.effect("hands an in-service install and restart to Task Scheduler on Windows", () =>
Effect.gen(function* () {
const { service, fs, statePath, commands, makeService } = yield* makeHarness("win32");
const {
service,
fs,
statePath,
commands,
control,
makeService,
markersAtStop,
markersAtPreparation,
} = yield* makeHarness("win32");
const plan = yield* service.install();
const inside = yield* makeService(undefined, "1.2.4", undefined, true);
const relayMarker = `${plan.baseDir}/runtime/${DESKTOP_UPDATE_RESTART_MARKER_FILE}`;
const handoff = [
`schtasks /Create /XML ${plan.unitPath} /TN t3code-PC-theo /F`,
"schtasks /End /TN t3code-PC-theo",
Expand All @@ -1549,9 +1657,23 @@ process.disconnect();
yield* fs.readFileString(`${plan.baseDir}/runtime/${SERVICE_RESTART_PENDING_FILE}`),
).toBe("1.2.4\n");

expect(markersAtStop).toEqual([true]);
expect(yield* fs.exists(relayMarker)).toBe(true);
yield* fs.remove(relayMarker);

commands.length = 0;
expect(yield* inside.restart).toBe("queued");
expect(commands).toEqual(handoff);
expect(markersAtStop).toEqual([true, true]);
expect(yield* fs.exists(relayMarker)).toBe(true);
yield* fs.remove(relayMarker);

expect(markersAtPreparation).not.toContain(true);
control.failCommand = handoff[0];
expect((yield* inside.restart.pipe(Effect.flip))._tag).toBe("BootServiceCommandError");
expect(yield* fs.exists(relayMarker)).toBe(false);
expect(markersAtPreparation.at(-1)).toBe(false);
expect(markersAtStop).toEqual([true, true]);
}),
);

Expand Down
Loading
Loading