|
| 1 | +import { afterEach, describe, expect, it } from "bun:test"; |
| 2 | +import { mkdtemp, rm, writeFile } from "node:fs/promises"; |
| 3 | +import { tmpdir } from "node:os"; |
| 4 | +import { join } from "node:path"; |
| 5 | +import { listInstalledServices, restartCommand, type InstalledService } from "./service-restart.ts"; |
| 6 | + |
| 7 | +const cleanups: Array<() => Promise<void>> = []; |
| 8 | + |
| 9 | +afterEach(async () => { |
| 10 | + while (cleanups.length > 0) { |
| 11 | + const fn = cleanups.pop(); |
| 12 | + if (fn) await fn(); |
| 13 | + } |
| 14 | +}); |
| 15 | + |
| 16 | +async function makeTempDir(): Promise<string> { |
| 17 | + const dir = await mkdtemp(join(tmpdir(), "webmux-service-restart-")); |
| 18 | + cleanups.push(async () => rm(dir, { recursive: true, force: true })); |
| 19 | + return dir; |
| 20 | +} |
| 21 | + |
| 22 | +describe("listInstalledServices", () => { |
| 23 | + it("picks up systemd units and strips the .service suffix", async () => { |
| 24 | + const systemdDir = await makeTempDir(); |
| 25 | + await writeFile(join(systemdDir, "webmux-alpha.service"), "[Service]\nExecStart=/bin/x\n"); |
| 26 | + await writeFile(join(systemdDir, "webmux-beta.service"), "[Service]\nExecStart=/bin/x\n"); |
| 27 | + await writeFile(join(systemdDir, "unrelated.service"), "[Service]\nExecStart=/bin/x\n"); |
| 28 | + |
| 29 | + const services = listInstalledServices({ |
| 30 | + systemdDir, |
| 31 | + launchdDir: "/no/such/dir", |
| 32 | + }); |
| 33 | + |
| 34 | + expect(services.map((s) => s.name).sort()).toEqual(["webmux-alpha", "webmux-beta"]); |
| 35 | + for (const svc of services) expect(svc.platform).toBe("linux"); |
| 36 | + }); |
| 37 | + |
| 38 | + it("picks up launchd plists and keeps the full label", async () => { |
| 39 | + const launchdDir = await makeTempDir(); |
| 40 | + await writeFile(join(launchdDir, "com.webmux.alpha.plist"), "<plist></plist>"); |
| 41 | + await writeFile(join(launchdDir, "com.other.thing.plist"), "<plist></plist>"); |
| 42 | + |
| 43 | + const services = listInstalledServices({ |
| 44 | + systemdDir: "/no/such/dir", |
| 45 | + launchdDir, |
| 46 | + }); |
| 47 | + |
| 48 | + expect(services.map((s) => s.name)).toEqual(["com.webmux.alpha"]); |
| 49 | + expect(services[0].platform).toBe("darwin"); |
| 50 | + }); |
| 51 | + |
| 52 | + it("returns empty when neither directory exists", () => { |
| 53 | + const services = listInstalledServices({ |
| 54 | + systemdDir: "/no/such/systemd", |
| 55 | + launchdDir: "/no/such/launchd", |
| 56 | + }); |
| 57 | + expect(services).toEqual([]); |
| 58 | + }); |
| 59 | +}); |
| 60 | + |
| 61 | +describe("restartCommand", () => { |
| 62 | + it("builds the systemctl --user restart command for linux", () => { |
| 63 | + const svc: InstalledService = { |
| 64 | + name: "webmux-foo", |
| 65 | + filePath: "/x/webmux-foo.service", |
| 66 | + platform: "linux", |
| 67 | + }; |
| 68 | + expect(restartCommand(svc, 1000)).toEqual({ |
| 69 | + bin: "systemctl", |
| 70 | + args: ["--user", "restart", "webmux-foo"], |
| 71 | + }); |
| 72 | + }); |
| 73 | + |
| 74 | + it("builds the launchctl kickstart command for darwin", () => { |
| 75 | + const svc: InstalledService = { |
| 76 | + name: "com.webmux.foo", |
| 77 | + filePath: "/x/com.webmux.foo.plist", |
| 78 | + platform: "darwin", |
| 79 | + }; |
| 80 | + expect(restartCommand(svc, 501)).toEqual({ |
| 81 | + bin: "launchctl", |
| 82 | + args: ["kickstart", "-k", "gui/501/com.webmux.foo"], |
| 83 | + }); |
| 84 | + }); |
| 85 | +}); |
0 commit comments