|
| 1 | +import * as vscode from "vscode" |
| 2 | +import * as os from "os" |
| 3 | +import { ClineProviderStatic, FormatLanguageUtil, PromptVariables } from "./test-types" |
| 4 | + |
| 5 | +// Mock dependencies |
| 6 | +jest.mock("vscode", () => ({ |
| 7 | + env: { |
| 8 | + language: "en", |
| 9 | + shell: "/bin/bash", |
| 10 | + }, |
| 11 | +})) |
| 12 | + |
| 13 | +jest.mock("os", () => ({ |
| 14 | + type: jest.fn(), |
| 15 | +})) |
| 16 | + |
| 17 | +// Create mock ClineProvider |
| 18 | +const MockClineProvider = { |
| 19 | + getInstance: jest.fn(), |
| 20 | +} as unknown as ClineProviderStatic |
| 21 | + |
| 22 | +// Create mock formatLanguage utility |
| 23 | +const formatLanguage = jest.fn() as FormatLanguageUtil |
| 24 | + |
| 25 | +describe("System Prompt Variables", () => { |
| 26 | + const mockCwd = "/test/workspace" |
| 27 | + const mockMode = "test-mode" |
| 28 | + const mockGetState = jest.fn() |
| 29 | + const mockProvider = { |
| 30 | + getState: mockGetState, |
| 31 | + } |
| 32 | + |
| 33 | + beforeEach(() => { |
| 34 | + jest.clearAllMocks() |
| 35 | + jest.resetModules() |
| 36 | + ;(os.type as jest.Mock).mockReturnValue("Linux") |
| 37 | + ;(formatLanguage as jest.Mock).mockReturnValue("en-US") |
| 38 | + ;(MockClineProvider.getInstance as jest.Mock).mockResolvedValue(mockProvider) |
| 39 | + |
| 40 | + // Set up global variables that would normally be in scope |
| 41 | + globalThis.cwd = mockCwd |
| 42 | + globalThis.mode = mockMode |
| 43 | + globalThis.language = undefined |
| 44 | + }) |
| 45 | + |
| 46 | + it("should use maxReadFileLine from provider state when available", async () => { |
| 47 | + // Setup |
| 48 | + const mockState = { maxReadFileLine: 1000 } |
| 49 | + mockGetState.mockResolvedValue(mockState) |
| 50 | + |
| 51 | + // Create test variables |
| 52 | + const provider = await MockClineProvider.getInstance() |
| 53 | + const { maxReadFileLine = 500 } = provider ? await provider.getState() : {} |
| 54 | + const testVariables: PromptVariables = { |
| 55 | + workspace: mockCwd, |
| 56 | + mode: mockMode, |
| 57 | + language: globalThis.language || formatLanguage(vscode.env.language), |
| 58 | + shell: vscode.env.shell, |
| 59 | + operatingSystem: os.type(), |
| 60 | + maxReadFileLine, |
| 61 | + } |
| 62 | + |
| 63 | + // Verify |
| 64 | + expect(testVariables).toEqual({ |
| 65 | + workspace: mockCwd, |
| 66 | + mode: mockMode, |
| 67 | + language: "en-US", |
| 68 | + shell: "/bin/bash", |
| 69 | + operatingSystem: "Linux", |
| 70 | + maxReadFileLine: 1000, |
| 71 | + }) |
| 72 | + }) |
| 73 | + |
| 74 | + it("should default maxReadFileLine to 500 when not in provider state", async () => { |
| 75 | + // Setup |
| 76 | + mockGetState.mockResolvedValue({}) |
| 77 | + |
| 78 | + // Create test variables |
| 79 | + const provider = await MockClineProvider.getInstance() |
| 80 | + const { maxReadFileLine = 500 } = provider ? await provider.getState() : {} |
| 81 | + const testVariables: PromptVariables = { |
| 82 | + workspace: mockCwd, |
| 83 | + mode: mockMode, |
| 84 | + language: globalThis.language || formatLanguage(vscode.env.language), |
| 85 | + shell: vscode.env.shell, |
| 86 | + operatingSystem: os.type(), |
| 87 | + maxReadFileLine, |
| 88 | + } |
| 89 | + |
| 90 | + // Verify |
| 91 | + expect(testVariables.maxReadFileLine).toBe(500) |
| 92 | + }) |
| 93 | + |
| 94 | + it("should default maxReadFileLine to 500 when provider is null", async () => { |
| 95 | + // Setup |
| 96 | + ;(MockClineProvider.getInstance as jest.Mock).mockResolvedValue(null) |
| 97 | + |
| 98 | + // Create test variables |
| 99 | + const provider = await MockClineProvider.getInstance() |
| 100 | + const { maxReadFileLine = 500 } = provider ? await provider.getState() : {} |
| 101 | + const testVariables: PromptVariables = { |
| 102 | + workspace: mockCwd, |
| 103 | + mode: mockMode, |
| 104 | + language: globalThis.language || formatLanguage(vscode.env.language), |
| 105 | + shell: vscode.env.shell, |
| 106 | + operatingSystem: os.type(), |
| 107 | + maxReadFileLine, |
| 108 | + } |
| 109 | + |
| 110 | + // Verify |
| 111 | + expect(testVariables.maxReadFileLine).toBe(500) |
| 112 | + }) |
| 113 | + |
| 114 | + it("should use provided language when available", async () => { |
| 115 | + // Setup |
| 116 | + const providedLanguage = "fr" |
| 117 | + mockGetState.mockResolvedValue({}) |
| 118 | + globalThis.language = providedLanguage |
| 119 | + |
| 120 | + // Create test variables |
| 121 | + const provider = await MockClineProvider.getInstance() |
| 122 | + const { maxReadFileLine = 500 } = provider ? await provider.getState() : {} |
| 123 | + const testVariables: PromptVariables = { |
| 124 | + workspace: mockCwd, |
| 125 | + mode: mockMode, |
| 126 | + language: globalThis.language || formatLanguage(vscode.env.language), |
| 127 | + shell: vscode.env.shell, |
| 128 | + operatingSystem: os.type(), |
| 129 | + maxReadFileLine, |
| 130 | + } |
| 131 | + |
| 132 | + // Verify |
| 133 | + expect(testVariables.language).toBe(providedLanguage) |
| 134 | + expect(formatLanguage).not.toHaveBeenCalled() |
| 135 | + }) |
| 136 | + |
| 137 | + it("should use formatted vscode language when input language is not provided", async () => { |
| 138 | + // Setup |
| 139 | + mockGetState.mockResolvedValue({}) |
| 140 | + globalThis.language = undefined |
| 141 | + |
| 142 | + // Create test variables |
| 143 | + const provider = await MockClineProvider.getInstance() |
| 144 | + const { maxReadFileLine = 500 } = provider ? await provider.getState() : {} |
| 145 | + const testVariables: PromptVariables = { |
| 146 | + workspace: mockCwd, |
| 147 | + mode: mockMode, |
| 148 | + language: globalThis.language || formatLanguage(vscode.env.language), |
| 149 | + shell: vscode.env.shell, |
| 150 | + operatingSystem: os.type(), |
| 151 | + maxReadFileLine, |
| 152 | + } |
| 153 | + |
| 154 | + // Verify |
| 155 | + expect(formatLanguage).toHaveBeenCalledWith("en") |
| 156 | + expect(testVariables.language).toBe("en-US") |
| 157 | + }) |
| 158 | + |
| 159 | + it("should correctly populate all variables with mocked inputs", async () => { |
| 160 | + // Setup |
| 161 | + mockGetState.mockResolvedValue({ maxReadFileLine: 750 }) |
| 162 | + |
| 163 | + // Create test variables |
| 164 | + const provider = await MockClineProvider.getInstance() |
| 165 | + const { maxReadFileLine = 500 } = provider ? await provider.getState() : {} |
| 166 | + const testVariables: PromptVariables = { |
| 167 | + workspace: mockCwd, |
| 168 | + mode: mockMode, |
| 169 | + language: globalThis.language || formatLanguage(vscode.env.language), |
| 170 | + shell: vscode.env.shell, |
| 171 | + operatingSystem: os.type(), |
| 172 | + maxReadFileLine, |
| 173 | + } |
| 174 | + |
| 175 | + // Verify |
| 176 | + expect(testVariables).toEqual({ |
| 177 | + workspace: mockCwd, |
| 178 | + mode: mockMode, |
| 179 | + language: "en-US", |
| 180 | + shell: "/bin/bash", |
| 181 | + operatingSystem: "Linux", |
| 182 | + maxReadFileLine: 750, |
| 183 | + }) |
| 184 | + |
| 185 | + expect(os.type).toHaveBeenCalled() |
| 186 | + expect(MockClineProvider.getInstance).toHaveBeenCalled() |
| 187 | + expect(mockGetState).toHaveBeenCalled() |
| 188 | + }) |
| 189 | +}) |
0 commit comments