@koinzhang/paseo-plugin-activity 0.4.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,62 +0,0 @@
1
- import assert from "node:assert/strict";
2
- import { test } from "node:test";
3
- import { QueryClient } from "@tanstack/react-query";
4
- import { updateWorkspaceStatusCache } from "./status-cache.ts";
5
- import { agentStatusInfo, loadWorkspaceAgentStatuses, type HostAgentUpdate } from "./list-host-agents.ts";
6
- import { agentUpdatedAt } from "./constants.ts";
7
- import type { AgentUsageItem } from "../../shared/usage.ts";
8
-
9
- test("late poll cannot erase live permission or closed-on-remove; cancellation preserves earlier pushes", async () => {
10
- const client = new QueryClient();
11
- const key = ["activity", "workspace-agent-status", "w"];
12
- const stale = { a: agentStatusInfo({ id: "a", workspaceId: "w" }), b: agentStatusInfo({ id: "b", workspaceId: "w", status: "idle" }) };
13
- client.setQueryData(key, stale);
14
- let finish!: (value: typeof stale) => void;
15
- const pending = client.fetchQuery({ queryKey: key, queryFn: () => new Promise<typeof stale>(resolve => { finish = resolve; }) });
16
- updateWorkspaceStatusCache(client, key, "w", { kind: "upsert", agent: { id: "a", workspaceId: "w", pendingPermissions: [{}] } });
17
- updateWorkspaceStatusCache(client, key, "w", { kind: "remove", agentId: "b" });
18
- finish(stale);
19
- await pending;
20
- await Promise.resolve();
21
- const current = client.getQueryData<typeof stale>(key)!;
22
- assert.equal(current.a.permissionCount, 1);
23
- assert.equal(current.b?.status, "closed");
24
- assert.equal(client.getQueryState(key)?.status, "success");
25
- assert.equal(client.getQueryState(key)?.fetchStatus, "idle");
26
- client.clear();
27
- });
28
-
29
- test("first paginated load replays upserts and removes and releases its listener", async () => {
30
- let listener!: (update: HostAgentUpdate) => void;
31
- let unsubscribed = false;
32
- let pages = 0;
33
- const map = await loadWorkspaceAgentStatuses({ agents: {
34
- subscribe: (handler) => { listener = handler; return () => { unsubscribed = true; }; },
35
- list: async () => {
36
- pages++;
37
- if (pages === 1) return { entries: [{ agent: { id: "a", workspaceId: "w" } }], pageInfo: { hasMore: true, nextCursor: "next" } };
38
- listener({ kind: "upsert", agent: { id: "a", workspaceId: "w", pendingPermissions: [{}] } });
39
- listener({ kind: "remove", agentId: "b" });
40
- return { entries: [{ agent: { id: "b", workspaceId: "w", status: "idle" } }], pageInfo: { hasMore: false, nextCursor: null } };
41
- },
42
- } }, "w");
43
- assert.equal(map.a?.permissionCount, 1);
44
- assert.equal(map.b?.status, "closed");
45
- assert.equal(unsubscribed, true);
46
- });
47
-
48
- test("failed load releases its listener", async () => {
49
- let unsubscribed = false;
50
- await assert.rejects(loadWorkspaceAgentStatuses({ agents: {
51
- subscribe: () => () => { unsubscribed = true; },
52
- list: async () => { throw new Error("offline"); },
53
- } }, "w"), /offline/);
54
- assert.equal(unsubscribed, true);
55
- });
56
-
57
- test("live updatedAt takes precedence over old database sync timestamps", () => {
58
- const item = { agentId: "a", updatedAt: "2026-09-19T12:00:00Z", lastActivityAt: "2026-01-01T00:00:00Z" } as AgentUsageItem;
59
- const live = { a: agentStatusInfo({ id: "a", updatedAt: "2026-01-02T00:00:00Z" }) };
60
- assert.equal(agentUpdatedAt(item, live), "2026-01-02T00:00:00Z");
61
- assert.equal(agentUpdatedAt(item, undefined), item.updatedAt);
62
- });
@@ -1,23 +0,0 @@
1
- import type { QueryClient, QueryKey } from "@tanstack/react-query";
2
- import type { AgentStatusInfo } from "./constants.ts";
3
- import { applyAgentStatusUpdate, type HostAgentUpdate } from "./list-host-agents.ts";
4
-
5
- /** Cancel stale reads before publishing live state; the loader buffers first-load events. */
6
- export function updateWorkspaceStatusCache(
7
- queryClient: QueryClient, queryKey: QueryKey, workspaceId: string, update: HostAgentUpdate,
8
- ): void {
9
- const previous = queryClient.getQueryData<Record<string, AgentStatusInfo>>(queryKey);
10
- if (!previous) return;
11
- const id = update.kind === "remove" ? update.agentId : update.agent.id;
12
- if (!(id in previous)) {
13
- if (update.kind === "remove") return;
14
- if (update.agent.workspaceId != null && update.agent.workspaceId !== workspaceId) return;
15
- if (update.agent.workspaceId == null) return;
16
- }
17
- // cancelQueries cancels synchronously, even when the SDK ignores AbortSignal.
18
- // Restore our captured latest cache after cancellation reverts the old fetch.
19
- void queryClient.cancelQueries({ queryKey, exact: true });
20
- const next = { ...previous };
21
- applyAgentStatusUpdate(next, workspaceId, update);
22
- queryClient.setQueryData(queryKey, next);
23
- }