|
| 1 | +import { beforeEach, describe, expect, it } from "vitest"; |
| 2 | +import type { PromptContext } from "./formatter"; |
| 3 | +import { Formatter } from "./formatter"; |
| 4 | + |
| 5 | +class TextMappingFormatter extends Formatter { |
| 6 | + private nextSuggestionResult = ""; |
| 7 | + private hasExplicitSuggestionResult = false; |
| 8 | + private selectedDisplayValue?: string; |
| 9 | + public lastSuggestCall: |
| 10 | + | { |
| 11 | + suggestedValues: string[]; |
| 12 | + allowCustomInput: boolean; |
| 13 | + displayValues?: string[]; |
| 14 | + } |
| 15 | + | undefined; |
| 16 | + |
| 17 | + constructor() { |
| 18 | + super(); |
| 19 | + } |
| 20 | + |
| 21 | + public setSelectedDisplayValue(value: string): void { |
| 22 | + this.selectedDisplayValue = value; |
| 23 | + } |
| 24 | + |
| 25 | + public setNextSuggestionResult(value: string): void { |
| 26 | + this.nextSuggestionResult = value; |
| 27 | + this.hasExplicitSuggestionResult = true; |
| 28 | + } |
| 29 | + |
| 30 | + public async testFormat(input: string): Promise<string> { |
| 31 | + return await this.format(input); |
| 32 | + } |
| 33 | + |
| 34 | + protected async format(input: string): Promise<string> { |
| 35 | + let output = input; |
| 36 | + output = await this.replaceVariableInString(output); |
| 37 | + return output; |
| 38 | + } |
| 39 | + |
| 40 | + protected promptForValue(): Promise<string> { |
| 41 | + return Promise.resolve(""); |
| 42 | + } |
| 43 | + |
| 44 | + protected getCurrentFileLink(): string | null { |
| 45 | + return null; |
| 46 | + } |
| 47 | + |
| 48 | + protected getCurrentFileName(): string | null { |
| 49 | + return null; |
| 50 | + } |
| 51 | + |
| 52 | + protected getVariableValue(variableName: string): string { |
| 53 | + const value = this.variables.get(variableName); |
| 54 | + return typeof value === "string" ? value : ""; |
| 55 | + } |
| 56 | + |
| 57 | + protected suggestForValue( |
| 58 | + suggestedValues: string[], |
| 59 | + allowCustomInput = false, |
| 60 | + context?: { |
| 61 | + placeholder?: string; |
| 62 | + variableKey?: string; |
| 63 | + displayValues?: string[]; |
| 64 | + }, |
| 65 | + ): string { |
| 66 | + this.lastSuggestCall = { |
| 67 | + suggestedValues, |
| 68 | + allowCustomInput, |
| 69 | + displayValues: context?.displayValues, |
| 70 | + }; |
| 71 | + |
| 72 | + if (this.hasExplicitSuggestionResult) { |
| 73 | + return this.nextSuggestionResult; |
| 74 | + } |
| 75 | + |
| 76 | + if (this.selectedDisplayValue && context?.displayValues) { |
| 77 | + const selectedIndex = context.displayValues.indexOf( |
| 78 | + this.selectedDisplayValue, |
| 79 | + ); |
| 80 | + if (selectedIndex >= 0) { |
| 81 | + return suggestedValues[selectedIndex] ?? ""; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + return suggestedValues[0] ?? ""; |
| 86 | + } |
| 87 | + |
| 88 | + protected suggestForField(_variableName: string): Promise<string> { |
| 89 | + return Promise.resolve(""); |
| 90 | + } |
| 91 | + |
| 92 | + protected promptForMathValue(): Promise<string> { |
| 93 | + return Promise.resolve(""); |
| 94 | + } |
| 95 | + |
| 96 | + protected getMacroValue(_macroName: string): Promise<string> { |
| 97 | + return Promise.resolve(""); |
| 98 | + } |
| 99 | + |
| 100 | + protected promptForVariable( |
| 101 | + _variableName: string, |
| 102 | + _context?: PromptContext, |
| 103 | + ): Promise<string> { |
| 104 | + return Promise.resolve(""); |
| 105 | + } |
| 106 | + |
| 107 | + protected getTemplateContent(_templatePath: string): Promise<string> { |
| 108 | + return Promise.resolve(""); |
| 109 | + } |
| 110 | + |
| 111 | + protected getSelectedText(): Promise<string> { |
| 112 | + return Promise.resolve(""); |
| 113 | + } |
| 114 | + |
| 115 | + protected getClipboardContent(): Promise<string> { |
| 116 | + return Promise.resolve(""); |
| 117 | + } |
| 118 | + |
| 119 | + protected isTemplatePropertyTypesEnabled(): boolean { |
| 120 | + return false; |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +describe("Formatter VALUE text mapping", () => { |
| 125 | + let formatter: TextMappingFormatter; |
| 126 | + |
| 127 | + beforeEach(() => { |
| 128 | + formatter = new TextMappingFormatter(); |
| 129 | + }); |
| 130 | + |
| 131 | + it("passes display mappings to suggesters and inserts mapped item values", async () => { |
| 132 | + formatter.setSelectedDisplayValue("High"); |
| 133 | + const result = await formatter.testFormat( |
| 134 | + "{{VALUE:🔼,⏫|text:Normal,High}}", |
| 135 | + ); |
| 136 | + |
| 137 | + expect(result).toBe("⏫"); |
| 138 | + expect(formatter.lastSuggestCall?.suggestedValues).toEqual(["🔼", "⏫"]); |
| 139 | + expect(formatter.lastSuggestCall?.displayValues).toEqual([ |
| 140 | + "Normal", |
| 141 | + "High", |
| 142 | + ]); |
| 143 | + }); |
| 144 | + |
| 145 | + it("applies default values against inserted item values", async () => { |
| 146 | + formatter.setNextSuggestionResult(""); |
| 147 | + const result = await formatter.testFormat( |
| 148 | + "{{VALUE:🔼,⏫|text:Normal,High|default:⏫}}", |
| 149 | + ); |
| 150 | + |
| 151 | + expect(result).toBe("⏫"); |
| 152 | + }); |
| 153 | + |
| 154 | + it("keeps custom typed values when custom input is enabled", async () => { |
| 155 | + formatter.setNextSuggestionResult("urgent!"); |
| 156 | + const result = await formatter.testFormat( |
| 157 | + "{{VALUE:🔼,⏫|text:Normal,High|custom}}", |
| 158 | + ); |
| 159 | + |
| 160 | + expect(result).toBe("urgent!"); |
| 161 | + expect(formatter.lastSuggestCall?.allowCustomInput).toBe(true); |
| 162 | + }); |
| 163 | + |
| 164 | + it("does not remap typed custom values that equal display labels", async () => { |
| 165 | + formatter.setNextSuggestionResult("High"); |
| 166 | + const result = await formatter.testFormat( |
| 167 | + "{{VALUE:🔼,⏫|text:Normal,High|custom}}", |
| 168 | + ); |
| 169 | + |
| 170 | + expect(result).toBe("High"); |
| 171 | + }); |
| 172 | + |
| 173 | + it("throws for duplicate display labels in text mappings", async () => { |
| 174 | + await expect( |
| 175 | + formatter.testFormat("{{VALUE:a,b|text:Same,Same}}"), |
| 176 | + ).rejects.toThrow(/duplicate text entries/i); |
| 177 | + }); |
| 178 | +}); |
0 commit comments