49 lines
2.6 KiB
TypeScript
49 lines
2.6 KiB
TypeScript
import { describe, test, expect } from "bun:test";
|
|
import { chmodSync, mkdtempSync, readFileSync, writeFileSync } from "node:fs";
|
|
import { tmpdir } from "node:os";
|
|
import path from "node:path";
|
|
import { catOwnerLabel, instancesToStop, stopOwnedCatInstances } from "../src/cat_instances";
|
|
|
|
const MINE = catOwnerLabel("bug", "t1");
|
|
const list = (...instances: object[]) => JSON.stringify({ ok: true, count: instances.length, instances });
|
|
|
|
describe("收尾停哪些 App 实例:只认本次运行的 owner", () => {
|
|
test("活着且 owner 是本次运行的 → 停", () => {
|
|
expect(instancesToStop(list(
|
|
{ id: "tiktok-1", app: "/a.app", state: "running", pid: 101, owner: MINE },
|
|
{ id: "tiktok-2", app: "/b.app", state: "starting", pid: 102, owner: MINE },
|
|
), MINE)).toEqual(["tiktok-1", "tiktok-2"]);
|
|
});
|
|
test("别人的(另一张单 / 评估 / 人手起的)→ 不停", () => {
|
|
expect(instancesToStop(list(
|
|
{ id: "tiktok-1", app: "/a.app", state: "running", pid: 101, owner: catOwnerLabel("feature", "f9") },
|
|
{ id: "tiktok-2", app: "/b.app", state: "running", pid: 102, owner: catOwnerLabel("bug", "t1", "eval") },
|
|
{ id: "tiktok-3", app: "/c.app", state: "running", pid: 103, owner: "zsh(4242)" },
|
|
), MINE)).toEqual([]);
|
|
});
|
|
test("已经停了 → 什么都不做", () => {
|
|
expect(instancesToStop(list({ id: "tiktok-1", app: "/a.app", state: "stopped", owner: MINE }), MINE)).toEqual([]);
|
|
});
|
|
test("读不懂 / 空输出 → 什么都不停", () => {
|
|
expect(instancesToStop("not json", MINE)).toBeNull();
|
|
expect(instancesToStop("", MINE)).toBeNull();
|
|
expect(instancesToStop(list(), MINE)).toEqual([]);
|
|
});
|
|
});
|
|
|
|
test("stopOwnedCatInstances:对本次运行的实例逐个 `stop <id>`,不带 --force", async () => {
|
|
const dir = mkdtempSync(path.join(tmpdir(), "catinst-"));
|
|
const calls = path.join(dir, "calls");
|
|
const bin = path.join(dir, "cat-supervisor");
|
|
writeFileSync(path.join(dir, "list.json"), list(
|
|
{ id: "tiktok-1", app: "/a.app", state: "running", pid: 101, owner: MINE },
|
|
{ id: "tiktok-2", app: "/b.app", state: "running", pid: 102, owner: "someone-else" },
|
|
));
|
|
writeFileSync(bin, `#!/bin/sh\necho "$*" >> "${calls}"\nif [ "$1" = list ]; then cat "${dir}/list.json"; else echo '{"ok":true}'; fi\n`);
|
|
chmodSync(bin, 0o755);
|
|
const logs: string[] = [];
|
|
await stopOwnedCatInstances(MINE, (level, msg) => logs.push(`${level} ${msg}`), bin);
|
|
expect(readFileSync(calls, "utf8").trim().split("\n")).toEqual(["list", "stop tiktok-1"]);
|
|
expect(logs).toEqual([expect.stringContaining("info 已停掉本次运行拉起的 App 实例 tiktok-1")]);
|
|
});
|