|
| 1 | +import { describe, expect, test } from "bun:test" |
| 2 | +import { Schema } from "effect" |
| 3 | +import { ConfigLSP } from "../../src/config/lsp" |
| 4 | + |
| 5 | +// The LSP config refinement enforces: any custom (non-builtin) LSP server |
| 6 | +// entry must declare an `extensions` array so the client knows which files |
| 7 | +// the server should attach to. Builtin server IDs and explicitly disabled |
| 8 | +// entries are exempt. |
| 9 | +// |
| 10 | +// Both validation paths must honor this rule: |
| 11 | +// - `Schema.decodeUnknownSync(ConfigLSP.Info)` (Effect layer) |
| 12 | +// - `ConfigLSP.Info.zod.parse(...)` (derived Zod) |
| 13 | +// |
| 14 | +// `typescript` is a builtin server id (see src/lsp/server.ts). |
| 15 | +describe("ConfigLSP.Info refinement", () => { |
| 16 | + const decodeEffect = Schema.decodeUnknownSync(ConfigLSP.Info) |
| 17 | + |
| 18 | + describe("accepted inputs", () => { |
| 19 | + test("true and false pass (top-level toggle)", () => { |
| 20 | + expect(decodeEffect(true)).toBe(true) |
| 21 | + expect(decodeEffect(false)).toBe(false) |
| 22 | + expect(ConfigLSP.Info.zod.parse(true)).toBe(true) |
| 23 | + expect(ConfigLSP.Info.zod.parse(false)).toBe(false) |
| 24 | + }) |
| 25 | + |
| 26 | + test("builtin server with no extensions passes", () => { |
| 27 | + const input = { typescript: { command: ["typescript-language-server", "--stdio"] } } |
| 28 | + expect(decodeEffect(input)).toEqual(input) |
| 29 | + expect(ConfigLSP.Info.zod.parse(input)).toEqual(input) |
| 30 | + }) |
| 31 | + |
| 32 | + test("custom server WITH extensions passes", () => { |
| 33 | + const input = { |
| 34 | + "my-lsp": { command: ["my-lsp-bin"], extensions: [".ml"] }, |
| 35 | + } |
| 36 | + expect(decodeEffect(input)).toEqual(input) |
| 37 | + expect(ConfigLSP.Info.zod.parse(input)).toEqual(input) |
| 38 | + }) |
| 39 | + |
| 40 | + test("disabled custom server passes (no extensions needed)", () => { |
| 41 | + const input = { "my-lsp": { disabled: true as const } } |
| 42 | + expect(decodeEffect(input)).toEqual(input) |
| 43 | + expect(ConfigLSP.Info.zod.parse(input)).toEqual(input) |
| 44 | + }) |
| 45 | + |
| 46 | + test("mix of builtin and custom with extensions passes", () => { |
| 47 | + const input = { |
| 48 | + typescript: { command: ["typescript-language-server", "--stdio"] }, |
| 49 | + "my-lsp": { command: ["my-lsp-bin"], extensions: [".ml"] }, |
| 50 | + } |
| 51 | + expect(decodeEffect(input)).toEqual(input) |
| 52 | + expect(ConfigLSP.Info.zod.parse(input)).toEqual(input) |
| 53 | + }) |
| 54 | + }) |
| 55 | + |
| 56 | + describe("rejected inputs", () => { |
| 57 | + const expectedMessage = "For custom LSP servers, 'extensions' array is required." |
| 58 | + |
| 59 | + test("custom server WITHOUT extensions fails via Effect decode", () => { |
| 60 | + expect(() => decodeEffect({ "my-lsp": { command: ["my-lsp-bin"] } })).toThrow(expectedMessage) |
| 61 | + }) |
| 62 | + |
| 63 | + test("custom server WITHOUT extensions fails via derived Zod", () => { |
| 64 | + const result = ConfigLSP.Info.zod.safeParse({ "my-lsp": { command: ["my-lsp-bin"] } }) |
| 65 | + expect(result.success).toBe(false) |
| 66 | + expect(result.error!.issues.some((i) => i.message === expectedMessage)).toBe(true) |
| 67 | + }) |
| 68 | + |
| 69 | + test("custom server with empty extensions array fails (extensions must be non-empty-truthy)", () => { |
| 70 | + // Boolean(['']) is true, so a non-empty array of strings is fine. |
| 71 | + // Boolean([]) is also true in JS, so empty arrays are accepted by the |
| 72 | + // refinement. This test documents current behavior. |
| 73 | + const input = { "my-lsp": { command: ["my-lsp-bin"], extensions: [] } } |
| 74 | + expect(decodeEffect(input)).toEqual(input) |
| 75 | + expect(ConfigLSP.Info.zod.parse(input)).toEqual(input) |
| 76 | + }) |
| 77 | + |
| 78 | + test("custom server without extensions mixed with a valid builtin still fails", () => { |
| 79 | + const input = { |
| 80 | + typescript: { command: ["typescript-language-server", "--stdio"] }, |
| 81 | + "my-lsp": { command: ["my-lsp-bin"] }, |
| 82 | + } |
| 83 | + expect(() => decodeEffect(input)).toThrow(expectedMessage) |
| 84 | + expect(ConfigLSP.Info.zod.safeParse(input).success).toBe(false) |
| 85 | + }) |
| 86 | + }) |
| 87 | +}) |
0 commit comments