|
| 1 | +import { describe, expect, it, vi } from "vitest" |
| 2 | + |
| 3 | +import { |
| 4 | + makeDisposable, |
| 5 | + makeEventEmitter, |
| 6 | + makeExtensionContext, |
| 7 | + makePosition, |
| 8 | + makeRange, |
| 9 | + makeSelection, |
| 10 | + makeTextDocument, |
| 11 | + makeTextEditor, |
| 12 | + makeUri, |
| 13 | + makeWorkspaceConfiguration, |
| 14 | +} from "../vscode" |
| 15 | + |
| 16 | +describe("VS Code test utilities", () => { |
| 17 | + it("creates the common VS Code value shapes", async () => { |
| 18 | + expect(makePosition(2, 3)).toEqual({ line: 2, character: 3 }) |
| 19 | + expect(makeRange(1, 2, 3, 4)).toEqual({ |
| 20 | + start: { line: 1, character: 2 }, |
| 21 | + end: { line: 3, character: 4 }, |
| 22 | + }) |
| 23 | + expect(makeSelection(4, 5)).toEqual({ |
| 24 | + anchor: { line: 4, character: 5 }, |
| 25 | + active: { line: 4, character: 5 }, |
| 26 | + }) |
| 27 | + |
| 28 | + const uri = makeUri("/tmp/test.ts", { scheme: "untitled" }) |
| 29 | + const document = makeTextDocument({ |
| 30 | + uri, |
| 31 | + getText: vi.fn().mockReturnValue("first\nsecond"), |
| 32 | + }) |
| 33 | + const editor = makeTextEditor({ document }) |
| 34 | + |
| 35 | + expect(uri).toMatchObject({ fsPath: "/tmp/test.ts", scheme: "untitled" }) |
| 36 | + expect(uri.toString()).toBe("/tmp/test.ts") |
| 37 | + expect(uri.toJSON()).toEqual({ fsPath: "/tmp/test.ts" }) |
| 38 | + expect(document.lineCount).toBe(2) |
| 39 | + expect(document.lineAt(1).text).toBe("second") |
| 40 | + expect(document.getText()).toBe("first\nsecond") |
| 41 | + expect(document.getWordRangeAtPosition(makePosition())).toBeUndefined() |
| 42 | + expect(document.offsetAt(makePosition())).toBeUndefined() |
| 43 | + expect(document.positionAt(0)).toBeUndefined() |
| 44 | + expect(document.validateRange(makeRange())).toEqual(makeRange()) |
| 45 | + expect(document.validatePosition(makePosition())).toEqual(makePosition()) |
| 46 | + expect(makeTextDocument().getText()).toBe("") |
| 47 | + expect(editor.document).toBe(document) |
| 48 | + expect(await editor.edit(() => undefined)).toBe(true) |
| 49 | + |
| 50 | + const disposable = makeDisposable() |
| 51 | + disposable.dispose() |
| 52 | + expect(disposable.dispose).toHaveBeenCalledOnce() |
| 53 | + }) |
| 54 | + |
| 55 | + it("supports event subscriptions and cleanup", () => { |
| 56 | + const emitter = makeEventEmitter<number>() |
| 57 | + const listener = vi.fn() |
| 58 | + const subscription = emitter.event(listener) |
| 59 | + |
| 60 | + emitter.fire(1) |
| 61 | + expect(listener).toHaveBeenCalledWith(1) |
| 62 | + |
| 63 | + subscription.dispose() |
| 64 | + emitter.fire(2) |
| 65 | + expect(listener).toHaveBeenCalledOnce() |
| 66 | + |
| 67 | + emitter.dispose() |
| 68 | + }) |
| 69 | + |
| 70 | + it("creates configurable workspace settings", async () => { |
| 71 | + const configuration = makeWorkspaceConfiguration({ enabled: true }) |
| 72 | + |
| 73 | + expect(configuration.get("enabled")).toBe(true) |
| 74 | + expect(configuration.get("missing", "fallback")).toBe("fallback") |
| 75 | + expect(configuration.has("enabled")).toBe(true) |
| 76 | + expect(configuration.has("missing")).toBe(false) |
| 77 | + |
| 78 | + await configuration.update("enabled", false) |
| 79 | + expect(configuration.update).toHaveBeenCalledWith("enabled", false) |
| 80 | + }) |
| 81 | + |
| 82 | + it("creates an extension context with fresh state containers", async () => { |
| 83 | + const context = makeExtensionContext({ extensionPath: "/custom/extension" }) |
| 84 | + |
| 85 | + expect(context.extensionPath).toBe("/custom/extension") |
| 86 | + expect(context.asAbsolutePath("dist")).toBe("/mock/extension/dist") |
| 87 | + expect(context.workspaceState.keys()).toEqual([]) |
| 88 | + await context.workspaceState.update("key", "value") |
| 89 | + await context.secrets.store("key", "value") |
| 90 | + expect(context.workspaceState.update).toHaveBeenCalledWith("key", "value") |
| 91 | + expect(context.secrets.store).toHaveBeenCalledWith("key", "value") |
| 92 | + }) |
| 93 | +}) |
0 commit comments