|
| 1 | +import { describe, expect, test, mock, beforeEach } from "bun:test"; |
| 2 | +import { WorkspaceService } from "./workspaceService"; |
| 3 | +import type { Config } from "@/node/config"; |
| 4 | +import type { HistoryService } from "./historyService"; |
| 5 | +import type { PartialService } from "./partialService"; |
| 6 | +import type { AIService } from "./aiService"; |
| 7 | +import type { InitStateManager } from "./initStateManager"; |
| 8 | +import type { ExtensionMetadataService } from "./ExtensionMetadataService"; |
| 9 | + |
| 10 | +describe("WorkspaceService rename lock", () => { |
| 11 | + let workspaceService: WorkspaceService; |
| 12 | + let mockAIService: AIService; |
| 13 | + |
| 14 | + beforeEach(() => { |
| 15 | + // Create minimal mocks for the services |
| 16 | + mockAIService = { |
| 17 | + isStreaming: mock(() => false), |
| 18 | + getWorkspaceMetadata: mock(() => Promise.resolve({ success: false, error: "not found" })), |
| 19 | + on: mock(() => {}), |
| 20 | + off: mock(() => {}), |
| 21 | + } as unknown as AIService; |
| 22 | + |
| 23 | + const mockHistoryService = { |
| 24 | + getHistory: mock(() => Promise.resolve({ success: true, data: [] })), |
| 25 | + appendToHistory: mock(() => Promise.resolve({ success: true })), |
| 26 | + } as unknown as HistoryService; |
| 27 | + |
| 28 | + const mockConfig = { |
| 29 | + srcDir: "/tmp/test", |
| 30 | + getSessionDir: mock(() => "/tmp/test/sessions"), |
| 31 | + generateStableId: mock(() => "test-id"), |
| 32 | + findWorkspace: mock(() => null), |
| 33 | + } as unknown as Config; |
| 34 | + |
| 35 | + const mockPartialService = { |
| 36 | + commitToHistory: mock(() => Promise.resolve({ success: true })), |
| 37 | + } as unknown as PartialService; |
| 38 | + |
| 39 | + const mockInitStateManager = {} as InitStateManager; |
| 40 | + const mockExtensionMetadataService = {} as ExtensionMetadataService; |
| 41 | + |
| 42 | + workspaceService = new WorkspaceService( |
| 43 | + mockConfig, |
| 44 | + mockHistoryService, |
| 45 | + mockPartialService, |
| 46 | + mockAIService, |
| 47 | + mockInitStateManager, |
| 48 | + mockExtensionMetadataService |
| 49 | + ); |
| 50 | + }); |
| 51 | + |
| 52 | + test("sendMessage returns error when workspace is being renamed", async () => { |
| 53 | + const workspaceId = "test-workspace"; |
| 54 | + |
| 55 | + // Access private renamingWorkspaces set via type assertion |
| 56 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 57 | + (workspaceService as any).renamingWorkspaces.add(workspaceId); |
| 58 | + |
| 59 | + const result = await workspaceService.sendMessage(workspaceId, "test message", { |
| 60 | + model: "test-model", |
| 61 | + }); |
| 62 | + |
| 63 | + expect(result.success).toBe(false); |
| 64 | + if (!result.success) { |
| 65 | + const error = result.error; |
| 66 | + // Error is SendMessageError which has a discriminated union |
| 67 | + expect(typeof error === "object" && error.type === "unknown").toBe(true); |
| 68 | + if (typeof error === "object" && error.type === "unknown") { |
| 69 | + expect(error.raw).toContain("being renamed"); |
| 70 | + } |
| 71 | + } |
| 72 | + }); |
| 73 | + |
| 74 | + test("resumeStream returns error when workspace is being renamed", async () => { |
| 75 | + const workspaceId = "test-workspace"; |
| 76 | + |
| 77 | + // Access private renamingWorkspaces set via type assertion |
| 78 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 79 | + (workspaceService as any).renamingWorkspaces.add(workspaceId); |
| 80 | + |
| 81 | + const result = await workspaceService.resumeStream(workspaceId, { |
| 82 | + model: "test-model", |
| 83 | + }); |
| 84 | + |
| 85 | + expect(result.success).toBe(false); |
| 86 | + if (!result.success) { |
| 87 | + const error = result.error; |
| 88 | + // Error is SendMessageError which has a discriminated union |
| 89 | + expect(typeof error === "object" && error.type === "unknown").toBe(true); |
| 90 | + if (typeof error === "object" && error.type === "unknown") { |
| 91 | + expect(error.raw).toContain("being renamed"); |
| 92 | + } |
| 93 | + } |
| 94 | + }); |
| 95 | + |
| 96 | + test("rename returns error when workspace is streaming", async () => { |
| 97 | + const workspaceId = "test-workspace"; |
| 98 | + |
| 99 | + // Mock isStreaming to return true |
| 100 | + (mockAIService.isStreaming as ReturnType<typeof mock>).mockReturnValue(true); |
| 101 | + |
| 102 | + const result = await workspaceService.rename(workspaceId, "new-name"); |
| 103 | + |
| 104 | + expect(result.success).toBe(false); |
| 105 | + if (!result.success) { |
| 106 | + expect(result.error).toContain("stream is active"); |
| 107 | + } |
| 108 | + }); |
| 109 | +}); |
0 commit comments