|
| 1 | +/* |
| 2 | +* file-information-cache.test.ts |
| 3 | +* |
| 4 | +* Copyright (C) 2026 Posit Software, PBC |
| 5 | +* |
| 6 | +* Tests for ensureFileInformationCache path normalization. |
| 7 | +* These tests verify that the cache works correctly with different |
| 8 | +* path separator styles on both Windows and Linux. |
| 9 | +*/ |
| 10 | + |
| 11 | +import { unitTest } from "../../test.ts"; |
| 12 | +import { assert, assertEquals } from "testing/asserts"; |
| 13 | +import { ensureFileInformationCache } from "../../../src/project/project-shared.ts"; |
| 14 | +import { ProjectContext } from "../../../src/project/types.ts"; |
| 15 | +import { normalizePath } from "../../../src/core/path.ts"; |
| 16 | +import { join } from "../../../src/deno_ral/path.ts"; |
| 17 | + |
| 18 | +// Test 1: Real OS paths work correctly (runs on Windows + Linux) |
| 19 | +// This uses actual temp directory paths from the OS to verify |
| 20 | +// that the normalization works with real native paths. |
| 21 | +// deno-lint-ignore require-await |
| 22 | +unitTest("ensureFileInformationCache - works with OS-native paths", async () => { |
| 23 | + const tempDir = Deno.makeTempDirSync({ prefix: "quarto-cache-test-" }); |
| 24 | + try { |
| 25 | + const project = { fileInformationCache: new Map() } as unknown as ProjectContext; |
| 26 | + |
| 27 | + // OS gives us native path (backslashes on Windows, forward slashes on Linux) |
| 28 | + const osPath = join(tempDir, "test.qmd"); |
| 29 | + |
| 30 | + // Cache entry created with OS path |
| 31 | + const cache1 = ensureFileInformationCache(project, osPath); |
| 32 | + cache1.metadata = { title: "Test" }; |
| 33 | + |
| 34 | + // Delete with normalized path (what renderForPreview does) |
| 35 | + const normalizedPath = normalizePath(osPath); |
| 36 | + project.fileInformationCache.delete(normalizedPath); |
| 37 | + |
| 38 | + // Entry should be gone |
| 39 | + assertEquals(project.fileInformationCache.size, 0, "Cache should be empty after delete with normalized path"); |
| 40 | + } finally { |
| 41 | + Deno.removeSync(tempDir, { recursive: true }); |
| 42 | + } |
| 43 | +}); |
| 44 | + |
| 45 | +// Test 2: Different separators normalize to same key |
| 46 | +// This verifies that paths with forward and backslashes both |
| 47 | +// map to the same cache entry. |
| 48 | +// deno-lint-ignore require-await |
| 49 | +unitTest("ensureFileInformationCache - normalizes path separators", async () => { |
| 50 | + const project = { fileInformationCache: new Map() } as unknown as ProjectContext; |
| 51 | + |
| 52 | + const cache1 = ensureFileInformationCache(project, "/test/project/test.qmd"); |
| 53 | + const cache2 = ensureFileInformationCache(project, "\\test\\project\\test.qmd"); |
| 54 | + |
| 55 | + assert(cache1 === cache2, "Same file with different separators should return same cache"); |
| 56 | + assertEquals(project.fileInformationCache.size, 1, "Cache should have only one entry"); |
| 57 | +}); |
| 58 | + |
| 59 | +// Test 3: Cache invalidation matches cache creation |
| 60 | +// This simulates the exact cache create/delete flow used in preview.ts |
| 61 | +// to ensure that the normalization is consistent for both operations. |
| 62 | +// deno-lint-ignore require-await |
| 63 | +unitTest("ensureFileInformationCache - invalidation works with normalized paths", async () => { |
| 64 | + const project = { fileInformationCache: new Map() } as unknown as ProjectContext; |
| 65 | + |
| 66 | + // Create cache with backslash path (like Windows might provide) |
| 67 | + ensureFileInformationCache(project, "C:\\project\\test.qmd"); |
| 68 | + assertEquals(project.fileInformationCache.size, 1, "Cache should have one entry after creation"); |
| 69 | + |
| 70 | + // Delete with forward slash path (normalized) |
| 71 | + project.fileInformationCache.delete(normalizePath("C:\\project\\test.qmd")); |
| 72 | + assertEquals(project.fileInformationCache.size, 0, "Cache should be empty after deletion with normalized path"); |
| 73 | +}); |
0 commit comments