Repository navigation
Expand file tree
/
Copy pathplugin.test.ts
More file actions
85 lines (82 loc) · 5.22 KB
/
Copy pathplugin.test.ts
File metadata and controls
85 lines (82 loc) · 5.22 KB
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
import { afterEach, expect, test } from "bun:test"
import { PetPlugin } from "./plugin/pet.js"
const originalFetch = globalThis.fetch
afterEach(() => { globalThis.fetch = originalFetch })
test("forwards session activity and permission states, but no conversation content", async () => {
const sent: unknown[] = []
let relayUrl = ""
globalThis.fetch = (async (url, options) => {
if (String(url).endsWith("/register")) {
const registration = JSON.parse(options.body as string)
relayUrl = registration.url
sent.push({ directory: registration.directory, url: "local-relay" })
} else {
sent.push(JSON.parse(options.body as string))
}
return new Response(null, { status: 204 })
}) as typeof fetch
const calls: unknown[] = []
const manyModels = Object.fromEntries(Array.from({ length: 250 }, (_, index) => [
`model-${index}`, { id: `model-${index}`, name: `Model ${index}`, status: "active" },
]))
const client = { session: {
list: async () => ({ data: [{ id: "s-1", title: "Test" }] }),
create: async () => ({ data: { id: "s-2" } }),
messages: async () => ({ data: [{ info: { role: "assistant" }, parts: [{ type: "text", text: "hola" }] }] }),
promptAsync: async (request: unknown) => { calls.push(request); return { data: undefined } },
}, provider: {
list: async () => ({ data: { connected: ["own", "local", "opencode", "opencode-go"], all: [
{ id: "own", name: "My provider", key: "secret-not-for-the-pet", models: {
custom: { id: "custom", name: "My model", status: "active" },
retired: { id: "retired", name: "Retired", status: "deprecated" },
...manyModels,
} },
{ id: "local", name: "Local", models: { llama: { id: "llama", name: "Llama", status: "active" } } },
{ id: "opencode", name: "OpenCode Zen", models: { "gpt-5.4": { id: "gpt-5.4", name: "GPT-5.4" } } },
{ id: "opencode-go", name: "OpenCode Go", models: { "kimi-k3": { id: "kimi-k3", name: "Kimi K3" } } },
{ id: "other", name: "Other", models: { hidden: { id: "hidden", name: "Hidden" } } },
] } }),
} }
const plugin = await PetPlugin({ directory: "/work", serverUrl: new URL("http://localhost:4096"), client } as never)
await Bun.sleep(1)
expect(relayUrl).toStartWith("http://127.0.0.1:")
expect((await (await originalFetch(`${relayUrl}/session?directory=%2Fwork`)).json())[0].title).toBe("Test")
const catalog = await (await originalFetch(`${relayUrl}/models?directory=%2Fwork`)).json()
expect(catalog).toHaveLength(254)
expect(catalog.find((item: { id: string }) => item.id === "own/custom")).toEqual({
id: "own/custom", label: "My provider · My model", providerID: "own",
providerName: "My provider", modelName: "My model",
})
expect(catalog.some((item: { id: string }) => item.id === "own/model-249")).toBe(true)
expect(catalog.some((item: { id: string }) => item.id === "local/llama")).toBe(true)
expect(catalog.some((item: { id: string }) => item.id === "opencode/gpt-5.4")).toBe(true)
expect(catalog.some((item: { id: string }) => item.id === "opencode-go/kimi-k3")).toBe(true)
expect(catalog.some((item: { id: string }) => item.id === "other/hidden" || item.id === "own/retired")).toBe(false)
expect(JSON.stringify(catalog)).not.toContain("secret-not-for-the-pet")
expect((await (await originalFetch(`${relayUrl}/session?directory=%2Fwork`, { method: "POST", body: "{}" })).json()).id).toBe("s-2")
expect((await (await originalFetch(`${relayUrl}/session/s-1/message?directory=%2Fwork`)).json())[0].parts[0].text).toBe("hola")
const prompt = await originalFetch(`${relayUrl}/session/s-1/prompt_async?directory=%2Fwork`, {
method: "POST", body: JSON.stringify({ parts: [{ type: "text", text: "hola" }], model: { providerID: "own", modelID: "custom" } }),
})
expect(prompt.status).toBe(204)
expect(calls).toEqual([{ path: { id: "s-1" }, query: { directory: "/work" }, body: { parts: [{ type: "text", text: "hola" }], model: { providerID: "own", modelID: "custom" } } }])
expect((await originalFetch(`${relayUrl}/session?directory=%2Fother`)).status).toBe(403)
const event = plugin.event!
await event({ event: { type: "session.status", properties: {
sessionID: "session-1", status: { type: "busy" }, prompt: "private"
} } } as never)
await event({ event: { type: "permission.asked", properties: { sessionID: "session-1" } } } as never)
await event({ event: { type: "permission.replied", properties: { sessionID: "session-1" } } } as never)
await event({ event: { type: "message.updated", properties: { sessionID: "session-1", text: "private" } } } as never)
await event({ event: { type: "session.idle", properties: { sessionID: "session-1" } } } as never)
await event({ event: { type: "command.executed", properties: { sessionID: "session-1", name: "review" } } } as never)
expect(sent).toEqual([
{ directory: "/work", url: "local-relay" },
{ type: "session.busy", sessionID: "session-1", directory: "/work" },
{ type: "session.waiting", sessionID: "session-1", directory: "/work" },
{ type: "session.busy", sessionID: "session-1", directory: "/work" },
{ type: "session.idle", sessionID: "session-1", directory: "/work" },
{ type: "session.review", sessionID: "session-1", directory: "/work" },
])
await plugin.dispose?.()
})