summaryrefslogtreecommitdiffhomepage
path: root/packages/heartbeat/src/run-store.test.ts
blob: 8be21f58048a93f2731838ed4e5674642fb28b02 (plain)
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
import type { StorageNamespace } from "@dispatch/kernel";
import type { HeartbeatRun } from "@dispatch/transport-contract";
import { describe, expect, it } from "vitest";
import { createHeartbeatRunStore } from "./run-store.js";

function createMemoryStorage(): StorageNamespace {
  const data = new Map<string, string>();
  return {
    get: async (key) => data.get(key) ?? null,
    set: async (key, value) => {
      data.set(key, value);
    },
    delete: async (key) => {
      data.delete(key);
    },
    has: async (key) => data.has(key),
    keys: async (prefix) => {
      const all = [...data.keys()];
      if (prefix === undefined) return all;
      return all.filter((k) => k.startsWith(prefix));
    },
  };
}

function run(id: string, conversationId: string, triggeredAt: string): HeartbeatRun {
  return { id, conversationId, triggeredAt, status: "running" };
}

describe("createHeartbeatRunStore", () => {
  it("creates and reads back a run", async () => {
    const store = createHeartbeatRunStore(createMemoryStorage());
    const created = await store.create("ws-1", run("r1", "c1", "2026-01-01T00:00:00.000Z"));
    expect(created.status).toBe("running");
    const read = await store.get("ws-1", "r1");
    expect(read).toEqual(created);
  });

  it("returns null for an unknown run", async () => {
    const store = createHeartbeatRunStore(createMemoryStorage());
    expect(await store.get("ws-1", "nope")).toBeNull();
  });

  it("updates the status of a run", async () => {
    const store = createHeartbeatRunStore(createMemoryStorage());
    await store.create("ws-1", run("r1", "c1", "2026-01-01T00:00:00.000Z"));
    const updated = await store.setStatus("ws-1", "r1", "completed");
    expect(updated?.status).toBe("completed");
    expect((await store.get("ws-1", "r1"))?.status).toBe("completed");
  });

  it("setStatus is a no-op (returns null) for an unknown run", async () => {
    const store = createHeartbeatRunStore(createMemoryStorage());
    expect(await store.setStatus("ws-1", "ghost", "stopped")).toBeNull();
  });

  it("lists runs most-recent first by triggeredAt", async () => {
    const store = createHeartbeatRunStore(createMemoryStorage());
    await store.create("ws-1", run("r1", "c1", "2026-01-01T00:00:00.000Z"));
    await store.create("ws-1", run("r2", "c2", "2026-02-01T00:00:00.000Z"));
    await store.create("ws-1", run("r3", "c3", "2026-01-15T00:00:00.000Z"));
    const runs = await store.list("ws-1");
    expect(runs.map((r) => r.id)).toEqual(["r2", "r3", "r1"]);
  });

  it("scopes runs per workspace", async () => {
    const store = createHeartbeatRunStore(createMemoryStorage());
    await store.create("ws-1", run("r1", "c1", "2026-01-01T00:00:00.000Z"));
    await store.create("ws-2", run("r2", "c2", "2026-01-01T00:00:00.000Z"));
    expect((await store.list("ws-1")).map((r) => r.id)).toEqual(["r1"]);
    expect((await store.list("ws-2")).map((r) => r.id)).toEqual(["r2"]);
  });
});