File size: 3,078 Bytes
fc93158
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { afterEach, describe, expect, it, vi } from "vitest";
import {
  checkInteractionLock,
  createInteractionLock,
  refreshInteractionLock,
  sleep,
  startAutonomousDaemon,
} from "./daemon-cooperative.js";

const tmpDirs: string[] = [];

async function createWorkspaceRoot() {
  const root = await fs.mkdtemp(path.join(os.tmpdir(), "openskynet-omega-daemon-"));
  tmpDirs.push(root);
  return root;
}

afterEach(async () => {
  vi.restoreAllMocks();
  await Promise.all(
    tmpDirs.splice(0, tmpDirs.length).map((dir) => fs.rm(dir, { recursive: true, force: true })),
  );
});

describe("omega daemon cooperative", () => {
  it("writes structured interaction lock payloads", async () => {
    const workspaceRoot = await createWorkspaceRoot();

    await createInteractionLock(workspaceRoot);

    const raw = await fs.readFile(path.join(workspaceRoot, ".interaction-lock"), "utf-8");
    const parsed = JSON.parse(raw) as { owner: string; pid: number; refreshedAt: number };
    expect(parsed).toMatchObject({
      owner: "tui",
      pid: process.pid,
      refreshedAt: expect.any(Number),
    });
  });

  it("refreshInteractionLock updates the lock timestamp until cleanup", async () => {
    const workspaceRoot = await createWorkspaceRoot();

    const stop = await refreshInteractionLock(workspaceRoot, 20);
    const lockPath = path.join(workspaceRoot, ".interaction-lock");
    const first = JSON.parse(await fs.readFile(lockPath, "utf-8")) as { refreshedAt: number };

    await sleep(40);
    const second = JSON.parse(await fs.readFile(lockPath, "utf-8")) as { refreshedAt: number };
    expect(second.refreshedAt).toBeGreaterThanOrEqual(first.refreshedAt);

    await stop();
    await expect(fs.stat(lockPath)).rejects.toThrow();
  });

  it("treats stale locks as inactive and deletes them", async () => {
    const workspaceRoot = await createWorkspaceRoot();
    const lockPath = path.join(workspaceRoot, ".interaction-lock");
    await fs.writeFile(lockPath, "{}\n", "utf-8");
    const staleSecondsAgo = Date.now() - 24 * 60 * 60 * 1000;
    await fs.utimes(lockPath, staleSecondsAgo / 1000, staleSecondsAgo / 1000);

    await expect(checkInteractionLock(lockPath)).resolves.toBe(false);
    await expect(fs.stat(lockPath)).rejects.toThrow();
  });

  it("supports cooperative shutdown via AbortSignal", async () => {
    const workspaceRoot = await createWorkspaceRoot();
    const controller = new AbortController();
    const runHeartbeatCycle = vi.fn(async () => {
      controller.abort(new Error("stop"));
    });

    await startAutonomousDaemon(
      { workspaceRoot, sessionKey: "main", signal: controller.signal },
      {
        checkInteractionLock: vi.fn(async () => false),
        runHeartbeatCycle,
        sleep: vi.fn(async (_ms, signal) => {
          if (signal?.aborted) throw signal.reason;
        }),
        log: vi.fn(),
        error: vi.fn(),
      },
    );

    expect(runHeartbeatCycle).toHaveBeenCalledTimes(1);
  });
});