|
| 1 | +/** |
| 2 | + * Mock ORPC client factory for Storybook stories. |
| 3 | + * |
| 4 | + * Creates a client that matches the AppRouter interface with configurable mock data. |
| 5 | + */ |
| 6 | +import type { APIClient } from "@/browser/contexts/API"; |
| 7 | +import type { FrontendWorkspaceMetadata } from "@/common/types/workspace"; |
| 8 | +import type { ProjectConfig } from "@/node/config"; |
| 9 | +import type { WorkspaceChatMessage } from "@/common/orpc/types"; |
| 10 | +import type { ChatStats } from "@/common/types/chatStats"; |
| 11 | +import { DEFAULT_RUNTIME_CONFIG } from "@/common/constants/workspace"; |
| 12 | +import { createAsyncMessageQueue } from "@/common/utils/asyncMessageQueue"; |
| 13 | + |
| 14 | +export interface MockORPCClientOptions { |
| 15 | + projects?: Map<string, ProjectConfig>; |
| 16 | + workspaces?: FrontendWorkspaceMetadata[]; |
| 17 | + /** Per-workspace chat callback. Return messages to emit, or use the callback for streaming. */ |
| 18 | + onChat?: (workspaceId: string, emit: (msg: WorkspaceChatMessage) => void) => (() => void) | void; |
| 19 | + /** Mock for executeBash per workspace */ |
| 20 | + executeBash?: ( |
| 21 | + workspaceId: string, |
| 22 | + script: string |
| 23 | + ) => Promise<{ success: true; output: string; exitCode: number; wall_duration_ms: number }>; |
| 24 | +} |
| 25 | + |
| 26 | +/** |
| 27 | + * Creates a mock ORPC client for Storybook. |
| 28 | + * |
| 29 | + * Usage: |
| 30 | + * ```tsx |
| 31 | + * const client = createMockORPCClient({ |
| 32 | + * projects: new Map([...]), |
| 33 | + * workspaces: [...], |
| 34 | + * onChat: (wsId, emit) => { |
| 35 | + * emit({ type: "caught-up" }); |
| 36 | + * // optionally return cleanup function |
| 37 | + * }, |
| 38 | + * }); |
| 39 | + * |
| 40 | + * return <AppLoader client={client} />; |
| 41 | + * ``` |
| 42 | + */ |
| 43 | +export function createMockORPCClient(options: MockORPCClientOptions = {}): APIClient { |
| 44 | + const { projects = new Map(), workspaces = [], onChat, executeBash } = options; |
| 45 | + |
| 46 | + const workspaceMap = new Map(workspaces.map((w) => [w.id, w])); |
| 47 | + |
| 48 | + const mockStats: ChatStats = { |
| 49 | + consumers: [], |
| 50 | + totalTokens: 0, |
| 51 | + model: "mock-model", |
| 52 | + tokenizerName: "mock-tokenizer", |
| 53 | + usageHistory: [], |
| 54 | + }; |
| 55 | + |
| 56 | + // Cast to ORPCClient - TypeScript can't fully validate the proxy structure |
| 57 | + return { |
| 58 | + tokenizer: { |
| 59 | + countTokens: async () => 0, |
| 60 | + countTokensBatch: async (_input: { model: string; texts: string[] }) => |
| 61 | + _input.texts.map(() => 0), |
| 62 | + calculateStats: async () => mockStats, |
| 63 | + }, |
| 64 | + server: { |
| 65 | + getLaunchProject: async () => null, |
| 66 | + }, |
| 67 | + providers: { |
| 68 | + list: async () => [], |
| 69 | + getConfig: async () => ({}), |
| 70 | + setProviderConfig: async () => ({ success: true, data: undefined }), |
| 71 | + setModels: async () => ({ success: true, data: undefined }), |
| 72 | + }, |
| 73 | + general: { |
| 74 | + listDirectory: async () => ({ entries: [], hasMore: false }), |
| 75 | + ping: async (input: string) => `Pong: ${input}`, |
| 76 | + tick: async function* () { |
| 77 | + // No-op generator |
| 78 | + }, |
| 79 | + }, |
| 80 | + projects: { |
| 81 | + list: async () => Array.from(projects.entries()), |
| 82 | + create: async () => ({ |
| 83 | + success: true, |
| 84 | + data: { projectConfig: { workspaces: [] }, normalizedPath: "/mock/project" }, |
| 85 | + }), |
| 86 | + pickDirectory: async () => null, |
| 87 | + listBranches: async () => ({ |
| 88 | + branches: ["main", "develop", "feature/new-feature"], |
| 89 | + recommendedTrunk: "main", |
| 90 | + }), |
| 91 | + remove: async () => ({ success: true, data: undefined }), |
| 92 | + secrets: { |
| 93 | + get: async () => [], |
| 94 | + update: async () => ({ success: true, data: undefined }), |
| 95 | + }, |
| 96 | + }, |
| 97 | + workspace: { |
| 98 | + list: async () => workspaces, |
| 99 | + create: async (input: { projectPath: string; branchName: string }) => ({ |
| 100 | + success: true, |
| 101 | + metadata: { |
| 102 | + id: Math.random().toString(36).substring(2, 12), |
| 103 | + name: input.branchName, |
| 104 | + projectPath: input.projectPath, |
| 105 | + projectName: input.projectPath.split("/").pop() ?? "project", |
| 106 | + namedWorkspacePath: `/mock/workspace/${input.branchName}`, |
| 107 | + runtimeConfig: DEFAULT_RUNTIME_CONFIG, |
| 108 | + }, |
| 109 | + }), |
| 110 | + remove: async () => ({ success: true }), |
| 111 | + rename: async (input: { workspaceId: string }) => ({ |
| 112 | + success: true, |
| 113 | + data: { newWorkspaceId: input.workspaceId }, |
| 114 | + }), |
| 115 | + fork: async () => ({ success: false, error: "Not implemented in mock" }), |
| 116 | + sendMessage: async () => ({ success: true, data: undefined }), |
| 117 | + resumeStream: async () => ({ success: true, data: undefined }), |
| 118 | + interruptStream: async () => ({ success: true, data: undefined }), |
| 119 | + clearQueue: async () => ({ success: true, data: undefined }), |
| 120 | + truncateHistory: async () => ({ success: true, data: undefined }), |
| 121 | + replaceChatHistory: async () => ({ success: true, data: undefined }), |
| 122 | + getInfo: async (input: { workspaceId: string }) => |
| 123 | + workspaceMap.get(input.workspaceId) ?? null, |
| 124 | + executeBash: async (input: { workspaceId: string; script: string }) => { |
| 125 | + if (executeBash) { |
| 126 | + const result = await executeBash(input.workspaceId, input.script); |
| 127 | + return { success: true, data: result }; |
| 128 | + } |
| 129 | + return { |
| 130 | + success: true, |
| 131 | + data: { success: true, output: "", exitCode: 0, wall_duration_ms: 0 }, |
| 132 | + }; |
| 133 | + }, |
| 134 | + onChat: async function* (input: { workspaceId: string }) { |
| 135 | + if (!onChat) { |
| 136 | + yield { type: "caught-up" } as WorkspaceChatMessage; |
| 137 | + return; |
| 138 | + } |
| 139 | + |
| 140 | + const { push, iterate, end } = createAsyncMessageQueue<WorkspaceChatMessage>(); |
| 141 | + |
| 142 | + // Call the user's onChat handler |
| 143 | + const cleanup = onChat(input.workspaceId, push); |
| 144 | + |
| 145 | + try { |
| 146 | + yield* iterate(); |
| 147 | + } finally { |
| 148 | + end(); |
| 149 | + cleanup?.(); |
| 150 | + } |
| 151 | + }, |
| 152 | + onMetadata: async function* () { |
| 153 | + // Empty generator - no metadata updates in mock |
| 154 | + await new Promise(() => {}); // Never resolves, keeps stream open |
| 155 | + }, |
| 156 | + activity: { |
| 157 | + list: async () => ({}), |
| 158 | + subscribe: async function* () { |
| 159 | + await new Promise(() => {}); // Never resolves |
| 160 | + }, |
| 161 | + }, |
| 162 | + }, |
| 163 | + window: { |
| 164 | + setTitle: async () => undefined, |
| 165 | + }, |
| 166 | + terminal: { |
| 167 | + create: async () => ({ |
| 168 | + sessionId: "mock-session", |
| 169 | + workspaceId: "mock-workspace", |
| 170 | + cols: 80, |
| 171 | + rows: 24, |
| 172 | + }), |
| 173 | + close: async () => undefined, |
| 174 | + resize: async () => undefined, |
| 175 | + sendInput: () => undefined, |
| 176 | + onOutput: async function* () { |
| 177 | + await new Promise(() => {}); |
| 178 | + }, |
| 179 | + onExit: async function* () { |
| 180 | + await new Promise(() => {}); |
| 181 | + }, |
| 182 | + openWindow: async () => undefined, |
| 183 | + closeWindow: async () => undefined, |
| 184 | + openNative: async () => undefined, |
| 185 | + }, |
| 186 | + update: { |
| 187 | + check: async () => undefined, |
| 188 | + download: async () => undefined, |
| 189 | + install: () => undefined, |
| 190 | + onStatus: async function* () { |
| 191 | + await new Promise(() => {}); |
| 192 | + }, |
| 193 | + }, |
| 194 | + } as unknown as APIClient; |
| 195 | +} |
0 commit comments