forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent-context.spec.ts
More file actions
81 lines (67 loc) · 2.17 KB
/
Copy pathagent-context.spec.ts
File metadata and controls
81 lines (67 loc) · 2.17 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
import { Component, signal } from "@angular/core";
import { TestBed } from "@angular/core/testing";
import { beforeEach, describe, expect, it, vi } from "vitest";
import type { Context } from "@ag-ui/client";
import { connectAgentContext } from "./agent-context";
import { CopilotKit } from "./copilotkit";
class CopilotKitCoreStub {
addContext = vi.fn<(context: Context) => string>();
removeContext = vi.fn<(id: string) => void>();
constructor() {
this.addContext.mockImplementation(
() => `ctx-${this.addContext.mock.calls.length}`,
);
}
}
class CopilotKitStub {
core = new CopilotKitCoreStub();
}
describe("connectAgentContext", () => {
let core: CopilotKitCoreStub;
beforeEach(() => {
TestBed.resetTestingModule();
TestBed.configureTestingModule({
providers: [{ provide: CopilotKit, useClass: CopilotKitStub }],
});
core = TestBed.inject(CopilotKit).core as unknown as CopilotKitCoreStub;
core.addContext.mockClear();
core.removeContext.mockClear();
});
it("registers context values and cleans up when the signal changes", async () => {
@Component({
standalone: true,
template: "",
})
class HostComponent {
context = signal<Context>({ description: "Initial", value: "1" });
constructor() {
connectAgentContext(this.context);
}
}
const fixture = TestBed.createComponent(HostComponent);
fixture.detectChanges();
await fixture.whenStable();
expect(core.addContext).toHaveBeenNthCalledWith(1, {
description: "Initial",
value: "1",
});
fixture.componentInstance.context.set({
description: "Updated",
value: "2",
});
fixture.detectChanges();
await fixture.whenStable();
expect(core.removeContext).toHaveBeenNthCalledWith(1, "ctx-1");
expect(core.addContext).toHaveBeenNthCalledWith(2, {
description: "Updated",
value: "2",
});
fixture.destroy();
expect(core.removeContext).toHaveBeenNthCalledWith(2, "ctx-2");
});
it("throws when used outside of an injection context", () => {
expect(() =>
connectAgentContext({ description: "missing", value: "0" }),
).toThrow(/NG0203/);
});
});