|
| 1 | +import { describe, it, expect } from "vitest"; |
| 2 | +import createMdxLockedPatternsLoader from "./locked-patterns"; |
| 3 | +import dedent from "dedent"; |
| 4 | + |
| 5 | +describe("MDX Locked Patterns Loader", () => { |
| 6 | + describe("Basic functionality", () => { |
| 7 | + it("should do nothing when no patterns are provided", async () => { |
| 8 | + const loader = createMdxLockedPatternsLoader(); |
| 9 | + loader.setDefaultLocale("en"); |
| 10 | + |
| 11 | + const md = dedent` |
| 12 | + # Title |
| 13 | + |
| 14 | + Some content. |
| 15 | + |
| 16 | + !params |
| 17 | + |
| 18 | + !! parameter_name |
| 19 | + |
| 20 | + !type string |
| 21 | + `; |
| 22 | + |
| 23 | + const result = await loader.pull("en", md); |
| 24 | + |
| 25 | + const placeholderRegex = /---LOCKED-PATTERN-[0-9a-f]+---/g; |
| 26 | + const placeholders = result.match(placeholderRegex) || []; |
| 27 | + expect(placeholders.length).toBe(0); // No patterns should be replaced |
| 28 | + |
| 29 | + expect(result).toBe(md); |
| 30 | + |
| 31 | + const pushed = await loader.push("es", result); |
| 32 | + expect(pushed).toBe(md); |
| 33 | + }); |
| 34 | + |
| 35 | + it("should preserve content matching patterns", async () => { |
| 36 | + const loader = createMdxLockedPatternsLoader([ |
| 37 | + "!params", |
| 38 | + "!! [\\w_]+", |
| 39 | + "!type [\\w<>\\[\\]\"',]+" |
| 40 | + ]); |
| 41 | + loader.setDefaultLocale("en"); |
| 42 | + |
| 43 | + const md = dedent` |
| 44 | + # Title |
| 45 | + |
| 46 | + Some content. |
| 47 | + |
| 48 | + !params |
| 49 | + |
| 50 | + !! parameter_name |
| 51 | + |
| 52 | + !type string |
| 53 | + `; |
| 54 | + |
| 55 | + const result = await loader.pull("en", md); |
| 56 | + |
| 57 | + const placeholderRegex = /---LOCKED-PATTERN-[0-9a-f]+---/g; |
| 58 | + const placeholders = result.match(placeholderRegex) || []; |
| 59 | + expect(placeholders.length).toBe(3); // Three patterns should be replaced |
| 60 | + |
| 61 | + const sanitizedContent = result |
| 62 | + .replace(placeholderRegex, "---PLACEHOLDER---"); |
| 63 | + |
| 64 | + const expectedSanitized = dedent` |
| 65 | + # Title |
| 66 | + |
| 67 | + Some content. |
| 68 | + |
| 69 | + ---PLACEHOLDER--- |
| 70 | + |
| 71 | + ---PLACEHOLDER--- |
| 72 | + |
| 73 | + ---PLACEHOLDER--- |
| 74 | + `; |
| 75 | + |
| 76 | + expect(sanitizedContent).toBe(expectedSanitized); |
| 77 | + |
| 78 | + const translated = result |
| 79 | + .replace("# Title", "# Título") |
| 80 | + .replace("Some content.", "Algún contenido."); |
| 81 | + |
| 82 | + const pushed = await loader.push("es", translated); |
| 83 | + |
| 84 | + const expectedPushed = dedent` |
| 85 | + # Título |
| 86 | + |
| 87 | + Algún contenido. |
| 88 | + |
| 89 | + !params |
| 90 | + |
| 91 | + !! parameter_name |
| 92 | + |
| 93 | + !type string |
| 94 | + `; |
| 95 | + |
| 96 | + expect(pushed).toBe(expectedPushed); |
| 97 | + }); |
| 98 | + }); |
| 99 | + |
| 100 | + describe("Real-world patterns", () => { |
| 101 | + it("should handle !hover syntax in code blocks", async () => { |
| 102 | + const loader = createMdxLockedPatternsLoader([ |
| 103 | + "// !hover[\\s\\S]*?(?=\\n|$)", |
| 104 | + "// !hover\\([\\d:]+\\)[\\s\\S]*?(?=\\n|$)" |
| 105 | + ]); |
| 106 | + loader.setDefaultLocale("en"); |
| 107 | + |
| 108 | + const md = dedent` |
| 109 | + \`\`\`js |
| 110 | + const x = 1; |
| 111 | + const pubkey = "vines1vzrYbzLMRdu58ou5XTby4qAqVRLmqo36NKPTg"; |
| 112 | + \`\`\` |
| 113 | + `; |
| 114 | + |
| 115 | + const result = await loader.pull("en", md); |
| 116 | + |
| 117 | + const placeholderRegex = /---LOCKED-PATTERN-[0-9a-f]+---/g; |
| 118 | + const placeholders = result.match(placeholderRegex) || []; |
| 119 | + expect(placeholders.length).toBe(0); // No patterns should be replaced |
| 120 | + |
| 121 | + const pushed = await loader.push("es", result); |
| 122 | + |
| 123 | + expect(pushed).toBe(md); |
| 124 | + }); |
| 125 | + |
| 126 | + it("should handle !! parameter headings", async () => { |
| 127 | + const loader = createMdxLockedPatternsLoader([ |
| 128 | + "!! [\\w_]+" |
| 129 | + ]); |
| 130 | + loader.setDefaultLocale("en"); |
| 131 | + |
| 132 | + const md = dedent` |
| 133 | + # Parameters |
| 134 | + |
| 135 | + !! pubkey |
| 136 | + |
| 137 | + The public key of the account to query. |
| 138 | + |
| 139 | + !! encoding |
| 140 | + |
| 141 | + Encoding format for the returned Account data. |
| 142 | + `; |
| 143 | + |
| 144 | + const result = await loader.pull("en", md); |
| 145 | + |
| 146 | + const placeholderRegex = /---LOCKED-PATTERN-[0-9a-f]+---/g; |
| 147 | + const placeholders = result.match(placeholderRegex) || []; |
| 148 | + expect(placeholders.length).toBe(2); // Two patterns should be replaced |
| 149 | + |
| 150 | + const sanitizedContent = result |
| 151 | + .replace(placeholderRegex, "---PLACEHOLDER---"); |
| 152 | + |
| 153 | + const expectedSanitized = dedent` |
| 154 | + # Parameters |
| 155 | + |
| 156 | + ---PLACEHOLDER--- |
| 157 | + |
| 158 | + The public key of the account to query. |
| 159 | + |
| 160 | + ---PLACEHOLDER--- |
| 161 | + |
| 162 | + Encoding format for the returned Account data. |
| 163 | + `; |
| 164 | + |
| 165 | + expect(sanitizedContent).toBe(expectedSanitized); |
| 166 | + |
| 167 | + const translated = result |
| 168 | + .replace("# Parameters", "# Parámetros") |
| 169 | + .replace("The public key of the account to query.", "La clave pública de la cuenta a consultar.") |
| 170 | + .replace("Encoding format for the returned Account data.", "Formato de codificación para los datos de la cuenta devueltos."); |
| 171 | + |
| 172 | + const pushed = await loader.push("es", translated); |
| 173 | + |
| 174 | + const expectedPushed = dedent` |
| 175 | + # Parámetros |
| 176 | + |
| 177 | + !! pubkey |
| 178 | + |
| 179 | + La clave pública de la cuenta a consultar. |
| 180 | + |
| 181 | + !! encoding |
| 182 | + |
| 183 | + Formato de codificación para los datos de la cuenta devueltos. |
| 184 | + `; |
| 185 | + |
| 186 | + expect(pushed).toBe(expectedPushed); |
| 187 | + }); |
| 188 | + |
| 189 | + it("should handle !type, !required, and !values declarations", async () => { |
| 190 | + const loader = createMdxLockedPatternsLoader([ |
| 191 | + "!! [\\w_]+", |
| 192 | + "!type [\\w<>\\[\\]\"',]+", |
| 193 | + "!required", |
| 194 | + "!values [\\s\\S]*?(?=\\n\\n|$)" |
| 195 | + ]); |
| 196 | + loader.setDefaultLocale("en"); |
| 197 | + |
| 198 | + const md = dedent` |
| 199 | + !! pubkey |
| 200 | + |
| 201 | + !type string |
| 202 | + !required |
| 203 | + |
| 204 | + The public key of the account to query. |
| 205 | + |
| 206 | + !! encoding |
| 207 | + |
| 208 | + !type string |
| 209 | + !values "base58" (default), "base64", "jsonParsed" |
| 210 | + |
| 211 | + Encoding format for the returned Account data. |
| 212 | + `; |
| 213 | + |
| 214 | + const result = await loader.pull("en", md); |
| 215 | + |
| 216 | + const placeholderRegex = /---LOCKED-PATTERN-[0-9a-f]+---/g; |
| 217 | + const placeholders = result.match(placeholderRegex) || []; |
| 218 | + expect(placeholders.length).toBe(6); // Six patterns should be replaced |
| 219 | + |
| 220 | + const sanitizedContent = result |
| 221 | + .replace(placeholderRegex, "---PLACEHOLDER---"); |
| 222 | + |
| 223 | + const expectedSanitized = dedent` |
| 224 | + ---PLACEHOLDER--- |
| 225 | + |
| 226 | + ---PLACEHOLDER--- |
| 227 | + ---PLACEHOLDER--- |
| 228 | + |
| 229 | + The public key of the account to query. |
| 230 | + |
| 231 | + ---PLACEHOLDER--- |
| 232 | + |
| 233 | + ---PLACEHOLDER--- |
| 234 | + ---PLACEHOLDER--- |
| 235 | + |
| 236 | + Encoding format for the returned Account data. |
| 237 | + `; |
| 238 | + |
| 239 | + expect(sanitizedContent).toBe(expectedSanitized); |
| 240 | + |
| 241 | + const translated = result |
| 242 | + .replace("The public key of the account to query.", "La clave pública de la cuenta a consultar.") |
| 243 | + .replace("Encoding format for the returned Account data.", "Formato de codificación para los datos de la cuenta devueltos."); |
| 244 | + |
| 245 | + const pushed = await loader.push("es", translated); |
| 246 | + |
| 247 | + const expectedPushed = dedent` |
| 248 | + !! pubkey |
| 249 | + |
| 250 | + !type string |
| 251 | + !required |
| 252 | + |
| 253 | + La clave pública de la cuenta a consultar. |
| 254 | + |
| 255 | + !! encoding |
| 256 | + |
| 257 | + !type string |
| 258 | + !values "base58" (default), "base64", "jsonParsed" |
| 259 | + |
| 260 | + Formato de codificación para los datos de la cuenta devueltos. |
| 261 | + `; |
| 262 | + |
| 263 | + expect(pushed).toBe(expectedPushed); |
| 264 | + }); |
| 265 | + }); |
| 266 | +}); |
0 commit comments