|
| 1 | +import { type CodeFormatter, FormatterRegistry } from "./code_formatter.js"; |
| 2 | +import { beforeEach, describe, expect, it } from "vitest"; |
| 3 | + |
| 4 | +function makeFormatter( |
| 5 | + name: string, |
| 6 | + supportedLanguages: string[], |
| 7 | +): CodeFormatter { |
| 8 | + return { |
| 9 | + name, |
| 10 | + canFormat(language: string): boolean { |
| 11 | + return supportedLanguages.includes(language); |
| 12 | + }, |
| 13 | + async format(code: string, _language: string): Promise<string> { |
| 14 | + return `[${name}] ${code}`; |
| 15 | + }, |
| 16 | + }; |
| 17 | +} |
| 18 | + |
| 19 | +function makeNeverFormatter(name = "NeverFormatter"): CodeFormatter { |
| 20 | + return makeFormatter(name, []); |
| 21 | +} |
| 22 | + |
| 23 | +describe("FormatterRegistry", () => { |
| 24 | + let registry: FormatterRegistry; |
| 25 | + |
| 26 | + beforeEach(() => { |
| 27 | + registry = new FormatterRegistry(); |
| 28 | + }); |
| 29 | + |
| 30 | + describe("initial state", () => { |
| 31 | + it("should have no formatters registered", () => { |
| 32 | + expect(registry.isLanguageSupported("javascript")).toBe(false); |
| 33 | + }); |
| 34 | + |
| 35 | + it("should return undefined for getFormatterForLanguage when empty", () => { |
| 36 | + expect( |
| 37 | + registry.getFormatterForLanguage("typescript"), |
| 38 | + ).toBeUndefined(); |
| 39 | + }); |
| 40 | + }); |
| 41 | + |
| 42 | + describe("isLanguageSupported", () => { |
| 43 | + it("should return false for an unknown language when formatters are registered", () => { |
| 44 | + registry.register(makeFormatter("A", ["javascript"])); |
| 45 | + |
| 46 | + expect(registry.isLanguageSupported("python")).toBe(false); |
| 47 | + }); |
| 48 | + |
| 49 | + it("should return true for a language handled by a registered formatter", () => { |
| 50 | + registry.register(makeFormatter("A", ["javascript"])); |
| 51 | + |
| 52 | + expect(registry.isLanguageSupported("javascript")).toBe(true); |
| 53 | + }); |
| 54 | + |
| 55 | + it("should return true when only one of many formatters handles the language", () => { |
| 56 | + registry.register(makeFormatter("A", ["css"])); |
| 57 | + registry.register(makeFormatter("B", ["html"])); |
| 58 | + |
| 59 | + expect(registry.isLanguageSupported("html")).toBe(true); |
| 60 | + }); |
| 61 | + |
| 62 | + it("should return false when all registered formatters reject the language", () => { |
| 63 | + registry.register(makeNeverFormatter("X")); |
| 64 | + registry.register(makeNeverFormatter("Y")); |
| 65 | + |
| 66 | + expect(registry.isLanguageSupported("rust")).toBe(false); |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | + describe("getFormatterForLanguage", () => { |
| 71 | + it("should return undefined for an unregistered language", () => { |
| 72 | + registry.register(makeFormatter("A", ["javascript"])); |
| 73 | + |
| 74 | + expect(registry.getFormatterForLanguage("rust")).toBeUndefined(); |
| 75 | + }); |
| 76 | + |
| 77 | + it("should return the matching formatter for a registered language", () => { |
| 78 | + const formatter = makeFormatter("Prettier", ["typescript"]); |
| 79 | + registry.register(formatter); |
| 80 | + |
| 81 | + expect(registry.getFormatterForLanguage("typescript")).toBe( |
| 82 | + formatter, |
| 83 | + ); |
| 84 | + }); |
| 85 | + |
| 86 | + it("should return the first matching formatter when multiple formatters support the language", () => { |
| 87 | + const first = makeFormatter("First", ["javascript"]); |
| 88 | + const second = makeFormatter("Second", ["javascript"]); |
| 89 | + registry.register(first); |
| 90 | + registry.register(second); |
| 91 | + |
| 92 | + expect(registry.getFormatterForLanguage("javascript")).toBe(first); |
| 93 | + }); |
| 94 | + |
| 95 | + it("should return the correct formatter when languages do not overlap", () => { |
| 96 | + const cssFormatter = makeFormatter("CSS", ["css"]); |
| 97 | + const jsFormatter = makeFormatter("JS", ["javascript"]); |
| 98 | + registry.register(cssFormatter); |
| 99 | + registry.register(jsFormatter); |
| 100 | + |
| 101 | + expect(registry.getFormatterForLanguage("css")).toBe(cssFormatter); |
| 102 | + expect(registry.getFormatterForLanguage("javascript")).toBe( |
| 103 | + jsFormatter, |
| 104 | + ); |
| 105 | + }); |
| 106 | + }); |
| 107 | + |
| 108 | + describe("register", () => { |
| 109 | + it("should allow registering a single formatter", () => { |
| 110 | + registry.register(makeFormatter("A", ["json"])); |
| 111 | + |
| 112 | + expect(registry.isLanguageSupported("json")).toBe(true); |
| 113 | + }); |
| 114 | + |
| 115 | + it("should allow registering multiple formatters independently", () => { |
| 116 | + registry.register(makeFormatter("A", ["json"])); |
| 117 | + registry.register(makeFormatter("B", ["yaml"])); |
| 118 | + |
| 119 | + expect(registry.isLanguageSupported("json")).toBe(true); |
| 120 | + expect(registry.isLanguageSupported("yaml")).toBe(true); |
| 121 | + }); |
| 122 | + |
| 123 | + it("should give priority to the first registered formatter when both handle the same language", () => { |
| 124 | + const first = makeFormatter("First", ["scss"]); |
| 125 | + const second = makeFormatter("Second", ["scss"]); |
| 126 | + registry.register(first); |
| 127 | + registry.register(second); |
| 128 | + |
| 129 | + const resolved = registry.getFormatterForLanguage("scss"); |
| 130 | + |
| 131 | + expect(resolved?.name).toBe("First"); |
| 132 | + }); |
| 133 | + |
| 134 | + it("should skip non-matching formatters and reach the one that matches", () => { |
| 135 | + const noMatch = makeNeverFormatter("NoMatch"); |
| 136 | + const match = makeFormatter("Match", ["graphql"]); |
| 137 | + registry.register(noMatch); |
| 138 | + registry.register(match); |
| 139 | + |
| 140 | + expect(registry.getFormatterForLanguage("graphql")).toBe(match); |
| 141 | + }); |
| 142 | + }); |
| 143 | + |
| 144 | + describe("canFormat delegation", () => { |
| 145 | + it("should delegate canFormat to each registered formatter in order", () => { |
| 146 | + const callLog: string[] = []; |
| 147 | + |
| 148 | + const trackingFormatter = ( |
| 149 | + name: string, |
| 150 | + languages: string[], |
| 151 | + ): CodeFormatter => ({ |
| 152 | + name, |
| 153 | + canFormat(language: string): boolean { |
| 154 | + callLog.push(name); |
| 155 | + return languages.includes(language); |
| 156 | + }, |
| 157 | + async format(code: string): Promise<string> { |
| 158 | + return code; |
| 159 | + }, |
| 160 | + }); |
| 161 | + |
| 162 | + const formatterA = trackingFormatter("A", []); |
| 163 | + const formatterB = trackingFormatter("B", ["markdown"]); |
| 164 | + registry.register(formatterA); |
| 165 | + registry.register(formatterB); |
| 166 | + |
| 167 | + registry.getFormatterForLanguage("markdown"); |
| 168 | + |
| 169 | + expect(callLog).toEqual(["A", "B"]); |
| 170 | + }); |
| 171 | + |
| 172 | + it("should stop delegation at the first formatter that handles the language", () => { |
| 173 | + const callLog: string[] = []; |
| 174 | + |
| 175 | + const trackingFormatter = ( |
| 176 | + name: string, |
| 177 | + languages: string[], |
| 178 | + ): CodeFormatter => ({ |
| 179 | + name, |
| 180 | + canFormat(language: string): boolean { |
| 181 | + callLog.push(name); |
| 182 | + return languages.includes(language); |
| 183 | + }, |
| 184 | + async format(code: string): Promise<string> { |
| 185 | + return code; |
| 186 | + }, |
| 187 | + }); |
| 188 | + |
| 189 | + const formatterA = trackingFormatter("A", ["html"]); |
| 190 | + const formatterB = trackingFormatter("B", ["html"]); |
| 191 | + registry.register(formatterA); |
| 192 | + registry.register(formatterB); |
| 193 | + |
| 194 | + registry.getFormatterForLanguage("html"); |
| 195 | + |
| 196 | + // Array.prototype.find stops at the first truthy result, so B |
| 197 | + // should never be consulted. |
| 198 | + expect(callLog).toEqual(["A"]); |
| 199 | + }); |
| 200 | + }); |
| 201 | + |
| 202 | + describe("CodeFormatter interface contract", () => { |
| 203 | + it("should expose a readonly name property", () => { |
| 204 | + const formatter = makeFormatter("TestFormatter", ["javascript"]); |
| 205 | + |
| 206 | + expect(formatter.name).toBe("TestFormatter"); |
| 207 | + }); |
| 208 | + |
| 209 | + it("canFormat should return true for a supported language", () => { |
| 210 | + const formatter = makeFormatter("F", ["css", "scss"]); |
| 211 | + |
| 212 | + expect(formatter.canFormat("css")).toBe(true); |
| 213 | + expect(formatter.canFormat("scss")).toBe(true); |
| 214 | + }); |
| 215 | + |
| 216 | + it("canFormat should return false for an unsupported language", () => { |
| 217 | + const formatter = makeFormatter("F", ["css"]); |
| 218 | + |
| 219 | + expect(formatter.canFormat("rust")).toBe(false); |
| 220 | + }); |
| 221 | + |
| 222 | + it("format should return a promise that resolves to a string", async () => { |
| 223 | + const formatter = makeFormatter("F", ["javascript"]); |
| 224 | + |
| 225 | + const result = await formatter.format("const x = 1", "javascript"); |
| 226 | + |
| 227 | + expect(typeof result).toBe("string"); |
| 228 | + }); |
| 229 | + |
| 230 | + it("format should resolve with the formatted output", async () => { |
| 231 | + const formatter = makeFormatter("F", ["javascript"]); |
| 232 | + |
| 233 | + const result = await formatter.format("const x = 1", "javascript"); |
| 234 | + |
| 235 | + expect(result).toBe("[F] const x = 1"); |
| 236 | + }); |
| 237 | + |
| 238 | + it("format should preserve empty string input", async () => { |
| 239 | + const formatter = makeFormatter("F", ["javascript"]); |
| 240 | + |
| 241 | + const result = await formatter.format("", "javascript"); |
| 242 | + |
| 243 | + expect(result).toBe("[F] "); |
| 244 | + }); |
| 245 | + }); |
| 246 | +}); |
0 commit comments