forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopilot-chat-input.component.spec.ts
More file actions
82 lines (69 loc) · 2.59 KB
/
Copy pathcopilot-chat-input.component.spec.ts
File metadata and controls
82 lines (69 loc) · 2.59 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
import {
EnvironmentInjector,
Injectable,
runInInjectionContext,
signal,
} from "@angular/core";
import { TestBed } from "@angular/core/testing";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { CopilotChatInput } from "../copilot-chat-input";
import { ChatState } from "../../../chat-state";
@Injectable()
class ChatStateStub extends ChatState {
inputValue = signal("");
submitInput = vi.fn((value: string) => this.inputValue.set(value));
changeInput = vi.fn((value: string) => this.inputValue.set(value));
}
describe("CopilotChatInput", () => {
let injector: EnvironmentInjector;
let component: CopilotChatInput;
let chatState: ChatStateStub;
beforeEach(() => {
TestBed.resetTestingModule();
TestBed.configureTestingModule({
providers: [{ provide: ChatState, useClass: ChatStateStub }],
});
injector = TestBed.inject(EnvironmentInjector);
chatState = TestBed.inject(ChatState) as ChatStateStub;
component = runInInjectionContext(injector, () => new CopilotChatInput());
component.textAreaRef = {
setValue: vi.fn(),
focus: vi.fn(),
} as any;
component.audioRecorderRef = {
start: vi.fn().mockResolvedValue(undefined),
stop: vi.fn().mockResolvedValue(undefined),
getState: () => "idle",
} as any;
});
it("switches between input and transcribe modes", () => {
expect(component.computedMode()).toBe("input");
component.handleStartTranscribe();
expect(component.computedMode()).toBe("transcribe");
component.handleCancelTranscribe();
expect(component.computedMode()).toBe("input");
});
it("emits value changes and updates chat state", () => {
const valueSpy = vi.fn();
component.valueChange.subscribe(valueSpy);
component.handleValueChange("Hello world");
expect(valueSpy).toHaveBeenCalledWith("Hello world");
expect(chatState.changeInput).toHaveBeenCalledWith("Hello world");
});
it("submits trimmed messages and clears input", () => {
const submitSpy = vi.fn();
component.submitMessage.subscribe(submitSpy);
component.handleValueChange(" Do it ");
component.send();
expect(submitSpy).toHaveBeenCalledWith("Do it");
expect(chatState.submitInput).toHaveBeenCalledWith("Do it");
expect(chatState.changeInput).toHaveBeenLastCalledWith("");
expect(component.textAreaRef?.setValue).toHaveBeenCalledWith("");
});
it("exposes tools menu through computed signal", () => {
(component as any).toolsMenu = () => [
{ label: "Example", onSelect: vi.fn() },
];
expect(component.computedToolsMenu()).toHaveLength(1);
});
});