From 8c84549876c733119ff8f016fb92622c7495a252 Mon Sep 17 00:00:00 2001 From: Quorafind Date: Sun, 29 Nov 2026 11:32:12 +0800 Subject: [PATCH] test(ics): check the parse cache directly instead of timing it "should benefit from caching on repeated parsing" required the cached parse to take under half the time of the first one. Both take about a millisecond, and the cache hit still hashes the whole content, so a GC pause on CI flips the result (it failed PR #725's CI with 1.3 ms vs 2.8 ms). Assert the cache behaviour instead: the second parse of the same content doesn't parse again (unfoldLines runs once), the cache holds one entry, and the result is a copy. --- src/__tests__/ics-parser-performance.test.ts | 43 +++++++++++--------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/src/__tests__/ics-parser-performance.test.ts b/src/__tests__/ics-parser-performance.test.ts index fd10beac..f22c6f1b 100644 --- a/src/__tests__/ics-parser-performance.test.ts +++ b/src/__tests__/ics-parser-performance.test.ts @@ -106,27 +106,30 @@ END:VCALENDAR`; }); describe("Caching Performance", () => { - test("should benefit from caching on repeated parsing", () => { + test("should serve repeated parsing from the cache", () => { const content = createMockIcsContent(100); - - // First parse (no cache) - const startTime1 = performance.now(); - const result1 = IcsParser.parse(content, testSource); - const endTime1 = performance.now(); - const firstParseTime = endTime1 - startTime1; - - // Second parse (with cache) - const startTime2 = performance.now(); - const result2 = IcsParser.parse(content, testSource); - const endTime2 = performance.now(); - const secondParseTime = endTime2 - startTime2; - - expect(result1.events).toHaveLength(100); - expect(result2.events).toHaveLength(100); - expect(secondParseTime).toBeLessThan(firstParseTime * 0.5); // Cache should be at least 50% faster - - console.log(`First parse: ${firstParseTime.toFixed(2)}ms, Cached parse: ${secondParseTime.toFixed(2)}ms`); - console.log(`Cache speedup: ${(firstParseTime / secondParseTime).toFixed(2)}x`); + // Check the cache directly: timing a ~1 ms parse against a cache hit + // is dominated by CI noise (GC pauses), and the hit still hashes + // the whole content. + const unfoldLines = jest.spyOn( + IcsParser as unknown as { unfoldLines: (lines: string[]) => string[] }, + "unfoldLines", + ); + try { + const result1 = IcsParser.parse(content, testSource); + const result2 = IcsParser.parse(content, testSource); + + expect(result1.events).toHaveLength(100); + expect(result2.events).toHaveLength(100); + // Only the first call parsed the content. + expect(unfoldLines).toHaveBeenCalledTimes(1); + expect(IcsParser.getCacheStats().size).toBe(1); + // The cached result is copied, so callers can't mutate the cache. + expect(result2.events).toEqual(result1.events); + expect(result2.events[0]).not.toBe(result1.events[0]); + } finally { + unfoldLines.mockRestore(); + } }); test("should manage cache size properly", () => {