Repository navigation
Phase 3: Testing wiring (CLI process, ACP mock-runtime, hooks) #37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| import type { AgentEvent } from "@cyrus/schemas/rtc/chat"; | ||
|
|
||
| export type MockPromptOptions = { | ||
| message?: string; | ||
| messageId?: string; | ||
| failAfterToken?: boolean; | ||
| }; | ||
|
|
||
| export function* createMockPromptStream( | ||
| options: MockPromptOptions = {} | ||
| ): Generator<AgentEvent> { | ||
| const messageId = options.messageId ?? "mock-message-1"; | ||
| yield { | ||
| type: "token", | ||
| text: options.message ?? "mock response", | ||
| messageId, | ||
| }; | ||
| if (options.failAfterToken) { | ||
| throw new Error("mock runtime failed"); | ||
| } | ||
| yield { | ||
| type: "message_completed", | ||
| text: options.message ?? "mock response", | ||
| messageId, | ||
| }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| import { describe, expect, test } from "bun:test"; | ||
| import { mkdtemp, rm } from "node:fs/promises"; | ||
| import { tmpdir } from "node:os"; | ||
| import { join } from "node:path"; | ||
| import type { AgentEvent } from "@cyrus/schemas/rtc/chat"; | ||
| import { runTurn } from "../../src/utils/run-turn"; | ||
| import { createMockPromptStream } from "../helpers/acp-runtime"; | ||
|
|
||
| describe("acp mock runtime", () => { | ||
| test("streams token and completion events", () => { | ||
| const events: AgentEvent[] = []; | ||
| for (const event of createMockPromptStream({ message: "hello" })) { | ||
| events.push(event); | ||
| } | ||
|
|
||
| expect(events).toEqual([ | ||
| { type: "token", text: "hello", messageId: "mock-message-1" }, | ||
| { type: "message_completed", text: "hello", messageId: "mock-message-1" }, | ||
| ]); | ||
| }); | ||
|
|
||
| test("drives runTurn through a mock coordinator", async () => { | ||
| const emitted: Array<{ type: string }> = []; | ||
|
|
||
| const result = await runTurn({ | ||
| agentName: "mock-agent", | ||
| threadId: "thread-1", | ||
| projectId: "project-1", | ||
| message: "ping", | ||
| emit: (event) => { | ||
| emitted.push({ type: event.type }); | ||
| return Promise.resolve(); | ||
| }, | ||
| emitTerminal: () => Promise.resolve(), | ||
| runtime: { | ||
| threadCoordinator: { | ||
| prompt: () => createMockPromptStream({ message: "pong" }), | ||
| }, | ||
| } as never, | ||
| }); | ||
|
|
||
| expect(result.isOk()).toBe(true); | ||
| expect(emitted.map((event) => event.type)).toEqual([ | ||
| "user_message", | ||
| "thread_started", | ||
| "token", | ||
| "message_completed", | ||
| ]); | ||
| }); | ||
|
|
||
| test("emits turn_interrupted when the mock stream fails", async () => { | ||
| const terminal: Array<{ type: string }> = []; | ||
|
|
||
| const result = await runTurn({ | ||
| agentName: "mock-agent", | ||
| threadId: "thread-1", | ||
| projectId: "project-1", | ||
| message: "ping", | ||
| emit: () => Promise.resolve(), | ||
| emitTerminal: (event) => { | ||
| terminal.push({ type: event.type }); | ||
| return Promise.resolve(); | ||
| }, | ||
| runtime: { | ||
| threadCoordinator: { | ||
| prompt: () => | ||
| createMockPromptStream({ | ||
| message: "pong", | ||
| failAfterToken: true, | ||
| }), | ||
| }, | ||
| } as never, | ||
| }); | ||
|
|
||
| expect(result.isErr()).toBe(true); | ||
| expect(terminal).toEqual([{ type: "turn_interrupted" }]); | ||
| }); | ||
| }); | ||
|
|
||
| describe("cli process integration", () => { | ||
| test("exits when start is invoked without a login token", async () => { | ||
| const home = await mkdtemp(join(tmpdir(), "cyrus-cli-test-")); | ||
|
|
||
| try { | ||
| const proc = Bun.spawn(["bun", "src/cli.ts", "start"], { | ||
| cwd: join(import.meta.dir, "../.."), | ||
| env: { | ||
| ...process.env, | ||
| CYRUS_HOME: home, | ||
| CYRUS_DAEMON: "1", | ||
| }, | ||
| stdout: "ignore", | ||
| stderr: "ignore", | ||
| }); | ||
|
|
||
| const exitCode = await proc.exited; | ||
| expect(exitCode).toBe(1); | ||
| } finally { | ||
| await rm(home, { recursive: true, force: true }); | ||
| } | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
shared/hooks/src/connection/use-controller-threads.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| import { describe, expect, test } from "bun:test"; | ||
| import { RTC_OPERATION_KEYS } from "@cyrus/constants/operation-keys"; | ||
| import { appendOptimisticUserMessage } from "@cyrus/utils/conversations/cache"; | ||
| import { QueryClient } from "@tanstack/react-query"; | ||
|
|
||
| describe("controller thread cache integration", () => { | ||
| test("appends an optimistic user message to the conversations cache", () => { | ||
| const queryClient = new QueryClient(); | ||
| const threadId = "thread-1"; | ||
| const turnId = "turn-1"; | ||
|
|
||
| appendOptimisticUserMessage(queryClient, threadId, turnId, "hello"); | ||
|
|
||
| const cached = queryClient.getQueryData<{ | ||
| conversations: Array<{ | ||
| chunk: { event: { type: string; content: string } }; | ||
| }>; | ||
| }>(RTC_OPERATION_KEYS.getConversations(threadId)); | ||
|
|
||
| expect(cached?.conversations).toHaveLength(1); | ||
| expect(cached?.conversations[0]?.chunk.event).toEqual({ | ||
| type: "user_message", | ||
| content: "hello", | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| import { describe, expect, test } from "bun:test"; | ||
|
|
||
| type MockChannelState = "connecting" | "open" | "closing" | "closed"; | ||
|
|
||
| type MockChannel = { | ||
| readyState: MockChannelState; | ||
| listeners: Map<string, Set<() => void>>; | ||
| }; | ||
|
|
||
| export function createMockDataChannel( | ||
| initialState: MockChannelState = "connecting" | ||
| ): MockChannel { | ||
| return { | ||
| readyState: initialState, | ||
| listeners: new Map(), | ||
| }; | ||
| } | ||
|
|
||
| export function openMockDataChannel(channel: MockChannel): void { | ||
| channel.readyState = "open"; | ||
| for (const listener of channel.listeners.get("open") ?? []) { | ||
| listener(); | ||
| } | ||
| } | ||
|
|
||
| export function attachMockChannelListeners( | ||
| channel: MockChannel, | ||
| handlers: { | ||
| onOpen?: () => void; | ||
| onError?: () => void; | ||
| onClose?: () => void; | ||
| } | ||
| ): void { | ||
| const register = (type: string, handler?: () => void) => { | ||
| if (!handler) return; | ||
| const listeners = channel.listeners.get(type) ?? new Set(); | ||
| listeners.add(handler); | ||
| channel.listeners.set(type, listeners); | ||
| }; | ||
|
|
||
| register("open", handlers.onOpen); | ||
| register("error", handlers.onError); | ||
| register("close", handlers.onClose); | ||
| } | ||
|
|
||
| describe("mock data channel", () => { | ||
| test("opens and notifies listeners", () => { | ||
| const channel = createMockDataChannel(); | ||
| let opened = false; | ||
|
|
||
| attachMockChannelListeners(channel, { | ||
| onOpen: () => { | ||
| opened = true; | ||
| }, | ||
| }); | ||
| openMockDataChannel(channel); | ||
|
|
||
| expect(channel.readyState).toBe("open"); | ||
| expect(opened).toBe(true); | ||
| }); | ||
| }); | ||
|
soorya-u marked this conversation as resolved.
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.