|
| 1 | +import { promises as fs } from "fs" |
| 2 | +import * as path from "path" |
| 3 | +import { setDefaults, parse } from "../../../src/node/cli" |
| 4 | +import { loadCustomStrings } from "../../../src/node/i18n" |
| 5 | +import { tmpdir } from "../../utils/helpers" |
| 6 | + |
| 7 | +// Mock the i18n module |
| 8 | +jest.mock("../../../src/node/i18n", () => ({ |
| 9 | + loadCustomStrings: jest.fn(), |
| 10 | +})) |
| 11 | + |
| 12 | +// Mock logger to avoid console output during tests |
| 13 | +jest.mock("@coder/logger", () => ({ |
| 14 | + logger: { |
| 15 | + info: jest.fn(), |
| 16 | + debug: jest.fn(), |
| 17 | + warn: jest.fn(), |
| 18 | + error: jest.fn(), |
| 19 | + level: 0, |
| 20 | + }, |
| 21 | + field: jest.fn(), |
| 22 | + Level: { |
| 23 | + Trace: 0, |
| 24 | + Debug: 1, |
| 25 | + Info: 2, |
| 26 | + Warn: 3, |
| 27 | + Error: 4, |
| 28 | + }, |
| 29 | +})) |
| 30 | + |
| 31 | +const mockedLoadCustomStrings = loadCustomStrings as jest.MockedFunction<typeof loadCustomStrings> |
| 32 | + |
| 33 | +describe("main", () => { |
| 34 | + let tempDir: string |
| 35 | + let mockServer: any |
| 36 | + |
| 37 | + beforeEach(async () => { |
| 38 | + tempDir = await tmpdir("code-server-main-test") |
| 39 | + |
| 40 | + // Reset mocks |
| 41 | + jest.clearAllMocks() |
| 42 | + |
| 43 | + // Mock the server creation to avoid actually starting a server |
| 44 | + mockServer = { |
| 45 | + server: { |
| 46 | + listen: jest.fn(), |
| 47 | + address: jest.fn(() => ({ address: "127.0.0.1", port: 8080 })), |
| 48 | + close: jest.fn(), |
| 49 | + }, |
| 50 | + editorSessionManagerServer: { |
| 51 | + address: jest.fn(() => null), |
| 52 | + }, |
| 53 | + dispose: jest.fn(), |
| 54 | + } |
| 55 | + }) |
| 56 | + |
| 57 | + afterEach(async () => { |
| 58 | + // Clean up temp directory |
| 59 | + try { |
| 60 | + await fs.rmdir(tempDir, { recursive: true }) |
| 61 | + } catch (error) { |
| 62 | + // Ignore cleanup errors |
| 63 | + } |
| 64 | + }) |
| 65 | + |
| 66 | + describe("runCodeServer", () => { |
| 67 | + it("should load custom strings when i18n flag is provided", async () => { |
| 68 | + // Create a test custom strings file |
| 69 | + const customStringsFile = path.join(tempDir, "custom-strings.json") |
| 70 | + await fs.writeFile( |
| 71 | + customStringsFile, |
| 72 | + JSON.stringify({ |
| 73 | + WELCOME: "Custom Welcome", |
| 74 | + LOGIN_TITLE: "My App", |
| 75 | + }), |
| 76 | + ) |
| 77 | + |
| 78 | + // Create args with i18n flag |
| 79 | + const cliArgs = parse([ |
| 80 | + `--config=${path.join(tempDir, "config.yaml")}`, |
| 81 | + `--user-data-dir=${tempDir}`, |
| 82 | + "--bind-addr=localhost:0", |
| 83 | + "--log=warn", |
| 84 | + "--auth=none", |
| 85 | + `--i18n=${customStringsFile}`, |
| 86 | + ]) |
| 87 | + const args = await setDefaults(cliArgs) |
| 88 | + |
| 89 | + // Mock the app module |
| 90 | + jest.doMock("../../../src/node/app", () => ({ |
| 91 | + createApp: jest.fn().mockResolvedValue(mockServer), |
| 92 | + ensureAddress: jest.fn().mockReturnValue(new URL("http://localhost:8080")), |
| 93 | + })) |
| 94 | + |
| 95 | + // Mock routes module |
| 96 | + jest.doMock("../../../src/node/routes", () => ({ |
| 97 | + register: jest.fn().mockResolvedValue(jest.fn()), |
| 98 | + })) |
| 99 | + |
| 100 | + // Mock loadCustomStrings to succeed |
| 101 | + mockedLoadCustomStrings.mockResolvedValue(undefined) |
| 102 | + |
| 103 | + // Import runCodeServer after mocking |
| 104 | + const mainModule = await import("../../../src/node/main") |
| 105 | + const result = await mainModule.runCodeServer(args) |
| 106 | + |
| 107 | + // Verify that loadCustomStrings was called with the correct file path |
| 108 | + expect(mockedLoadCustomStrings).toHaveBeenCalledWith(customStringsFile) |
| 109 | + expect(mockedLoadCustomStrings).toHaveBeenCalledTimes(1) |
| 110 | + |
| 111 | + // Clean up |
| 112 | + await result.dispose() |
| 113 | + }) |
| 114 | + |
| 115 | + it("should not load custom strings when i18n flag is not provided", async () => { |
| 116 | + // Create args without i18n flag |
| 117 | + const cliArgs = parse([ |
| 118 | + `--config=${path.join(tempDir, "config.yaml")}`, |
| 119 | + `--user-data-dir=${tempDir}`, |
| 120 | + "--bind-addr=localhost:0", |
| 121 | + "--log=warn", |
| 122 | + "--auth=none", |
| 123 | + ]) |
| 124 | + const args = await setDefaults(cliArgs) |
| 125 | + |
| 126 | + // Mock the app module |
| 127 | + jest.doMock("../../../src/node/app", () => ({ |
| 128 | + createApp: jest.fn().mockResolvedValue(mockServer), |
| 129 | + ensureAddress: jest.fn().mockReturnValue(new URL("http://localhost:8080")), |
| 130 | + })) |
| 131 | + |
| 132 | + // Mock routes module |
| 133 | + jest.doMock("../../../src/node/routes", () => ({ |
| 134 | + register: jest.fn().mockResolvedValue(jest.fn()), |
| 135 | + })) |
| 136 | + |
| 137 | + // Import runCodeServer after mocking |
| 138 | + const mainModule = await import("../../../src/node/main") |
| 139 | + const result = await mainModule.runCodeServer(args) |
| 140 | + |
| 141 | + // Verify that loadCustomStrings was NOT called |
| 142 | + expect(mockedLoadCustomStrings).not.toHaveBeenCalled() |
| 143 | + |
| 144 | + // Clean up |
| 145 | + await result.dispose() |
| 146 | + }) |
| 147 | + |
| 148 | + it("should handle errors when loadCustomStrings fails", async () => { |
| 149 | + // Create args with i18n flag pointing to non-existent file |
| 150 | + const nonExistentFile = path.join(tempDir, "does-not-exist.json") |
| 151 | + const cliArgs = parse([ |
| 152 | + `--config=${path.join(tempDir, "config.yaml")}`, |
| 153 | + `--user-data-dir=${tempDir}`, |
| 154 | + "--bind-addr=localhost:0", |
| 155 | + "--log=warn", |
| 156 | + "--auth=none", |
| 157 | + `--i18n=${nonExistentFile}`, |
| 158 | + ]) |
| 159 | + const args = await setDefaults(cliArgs) |
| 160 | + |
| 161 | + // Mock loadCustomStrings to throw an error |
| 162 | + const mockError = new Error("Custom strings file not found") |
| 163 | + mockedLoadCustomStrings.mockRejectedValue(mockError) |
| 164 | + |
| 165 | + // Import runCodeServer after mocking |
| 166 | + const mainModule = await import("../../../src/node/main") |
| 167 | + |
| 168 | + // Verify that runCodeServer throws the error from loadCustomStrings |
| 169 | + await expect(mainModule.runCodeServer(args)).rejects.toThrow("Custom strings file not found") |
| 170 | + |
| 171 | + // Verify that loadCustomStrings was called |
| 172 | + expect(mockedLoadCustomStrings).toHaveBeenCalledWith(nonExistentFile) |
| 173 | + }) |
| 174 | + }) |
| 175 | +}) |
0 commit comments