|
1 | 1 | // @vitest-environment node |
2 | | -import { describe, it, expect } from "vitest"; |
| 2 | +import { describe, it, expect, vi } from "vitest"; |
3 | 3 | import { extractTranscript, buildCaptionModel, TranscriptWord } from "./parser"; |
4 | 4 | import { DEFAULT_STYLE, DEFAULT_CONTAINER, DEFAULT_ANIMATION_SET } from "./types"; |
5 | 5 |
|
@@ -156,6 +156,76 @@ describe("extractTranscript", () => { |
156 | 156 | }); |
157 | 157 | }); |
158 | 158 |
|
| 159 | + it.each([ |
| 160 | + ["array delimiter", "'x ]; y'", "x ]; y"], |
| 161 | + ["trailing-comma text", "'literal ,] and ,}'", "literal ,] and ,}"], |
| 162 | + ["object-key text", "'{ text: example }'", "{ text: example }"], |
| 163 | + ["escaped quotes", String.raw`'It\'s "quoted"'`, `It's "quoted"`], |
| 164 | + ["escaped double quotes", String.raw`'Say \"hello\"'`, 'Say "hello"'], |
| 165 | + ["escaped backslash", String.raw`'C:\\captions\\'`, "C:\\captions\\"], |
| 166 | + ["hex and Unicode escapes", String.raw`'\x41\u0042\u{1D11E}'`, "AB\u{1D11E}"], |
| 167 | + ["control escapes", String.raw`'one\ntwo\tthree'`, "one\ntwo\tthree"], |
| 168 | + ])("preserves %s in JavaScript strings", (_name, literal, text) => { |
| 169 | + const source = `<script>const TRANSCRIPT = [{ id: 'word-a', text: ${literal}, start: 0.25, end: 1.5 }];</script>`; |
| 170 | + expect(extractTranscript(source)).toEqual([{ id: "word-a", text, start: 0.25, end: 1.5 }]); |
| 171 | + }); |
| 172 | + |
| 173 | + it("preserves JSON values and filters malformed word entries", () => { |
| 174 | + const source = `const TRANSCRIPT = [ |
| 175 | + null, 42, {"text":"missing timing"}, {"text":"bad timing","start":"0","end":1}, |
| 176 | + {"id":7,"text":"valid","start":-0.25,"end":1e1,"extra":{"items":[true,false,null]}} |
| 177 | + ];`; |
| 178 | + expect(extractTranscript(source)).toEqual([{ text: "valid", start: -0.25, end: 10 }]); |
| 179 | + }); |
| 180 | + |
| 181 | + it("accepts comments around static values", () => { |
| 182 | + expect( |
| 183 | + extractTranscript(`const TRANSCRIPT = [ |
| 184 | + // Delimiter in a comment: ]; |
| 185 | + { text: 'hello', start: 0, end: 1, }, |
| 186 | + ] /* trailing comment */;`), |
| 187 | + ).toEqual([{ text: "hello", start: 0, end: 1 }]); |
| 188 | + }); |
| 189 | + |
| 190 | + it.each([ |
| 191 | + "[{ text: 'unterminated, start: 0, end: 1 }];", |
| 192 | + "[{ text: 'hello', start: 0, end: 1 };", |
| 193 | + String.raw`[{ text: '\xZZ', start: 0, end: 1 }];`, |
| 194 | + "[{ text: 'hello', start: 0, end: 1 }] garbage;", |
| 195 | + "[{ text: 'hello', start: 0, end: 1 },,];", |
| 196 | + "[{ text: getText(), start: 0, end: 1 }];", |
| 197 | + "[{ text: 'hello', start: offset, end: 1 }];", |
| 198 | + "[{ text: 'hello', start: 1 + 2, end: 4 }];", |
| 199 | + "[{ text: 'hello', start: -getTime(), end: 1 }];", |
| 200 | + "[{ text: 'hello', start: 0, end: 1, extra: /pattern/ }];", |
| 201 | + "[{ text: 'hello', start: 0, end: 1, extra: 1n }];", |
| 202 | + "[{ get text() { return 'hello'; }, start: 0, end: 1 }];", |
| 203 | + "[{ text: 'hello', start: 0, end: 1, extra() {} }];", |
| 204 | + "[{ ['text']: 'hello', start: 0, end: 1 }];", |
| 205 | + "[{ text, start: 0, end: 1 }];", |
| 206 | + "[{ ...word, text: 'hello', start: 0, end: 1 }];", |
| 207 | + "[...words];", |
| 208 | + "[{ text: `hello ${getText()}`, start: 0, end: 1 }];", |
| 209 | + "[{ text: 'hello', start: 0, end: 1, extra: { run: getText() } }];", |
| 210 | + "[{ text: 'hello', start: 0, end: 1 }].map(transform);", |
| 211 | + "[{ text: 'hello', start: 0, end: 1 }], run();", |
| 212 | + ])("rejects malformed or executable initializers: %s", (initializer) => { |
| 213 | + expect(extractTranscript(`const TRANSCRIPT = ${initializer}`)).toEqual([]); |
| 214 | + }); |
| 215 | + |
| 216 | + it("never runs code while extracting a transcript", () => { |
| 217 | + const getCaptionText = vi.fn(() => "hello"); |
| 218 | + vi.stubGlobal("getCaptionText", getCaptionText); |
| 219 | + try { |
| 220 | + expect( |
| 221 | + extractTranscript("const TRANSCRIPT = [{ text: getCaptionText(), start: 0, end: 1 }];"), |
| 222 | + ).toEqual([]); |
| 223 | + expect(getCaptionText).not.toHaveBeenCalled(); |
| 224 | + } finally { |
| 225 | + vi.unstubAllGlobals(); |
| 226 | + } |
| 227 | + }); |
| 228 | + |
159 | 229 | describe("real-world source samples", () => { |
160 | 230 | it("handles a realistic production-style TRANSCRIPT block with many words", () => { |
161 | 231 | const source = ` |
|
0 commit comments