|
| 1 | +/* |
| 2 | + * file-information-cache.test.ts |
| 3 | + * |
| 4 | + * Tests for fileInformationCache path normalization |
| 5 | + * Related to issue #13955 |
| 6 | + * |
| 7 | + * Copyright (C) 2026 Posit Software, PBC |
| 8 | + */ |
| 9 | + |
| 10 | +import { unitTest } from "../../test.ts"; |
| 11 | +import { assert } from "testing/asserts"; |
| 12 | +import { join } from "../../../src/deno_ral/path.ts"; |
| 13 | +import { |
| 14 | + ensureFileInformationCache, |
| 15 | + FileInformationCacheMap, |
| 16 | +} from "../../../src/project/project-shared.ts"; |
| 17 | +import { createMockProjectContext } from "./utils.ts"; |
| 18 | + |
| 19 | +// deno-lint-ignore require-await |
| 20 | +unitTest( |
| 21 | + "fileInformationCache - same path returns same entry", |
| 22 | + async () => { |
| 23 | + const project = createMockProjectContext(); |
| 24 | + |
| 25 | + // Use cross-platform absolute path (backslashes on Windows, forward on Linux) |
| 26 | + const path1 = join(project.dir, "doc.qmd"); |
| 27 | + const path2 = join(project.dir, "doc.qmd"); |
| 28 | + |
| 29 | + const entry1 = ensureFileInformationCache(project, path1); |
| 30 | + const entry2 = ensureFileInformationCache(project, path2); |
| 31 | + |
| 32 | + assert( |
| 33 | + entry1 === entry2, |
| 34 | + "Same path should return same cache entry", |
| 35 | + ); |
| 36 | + assert( |
| 37 | + project.fileInformationCache.size === 1, |
| 38 | + "Should have exactly one cache entry", |
| 39 | + ); |
| 40 | + }, |
| 41 | +); |
| 42 | + |
| 43 | +// deno-lint-ignore require-await |
| 44 | +unitTest( |
| 45 | + "fileInformationCache - different paths create different entries", |
| 46 | + async () => { |
| 47 | + const project = createMockProjectContext(); |
| 48 | + |
| 49 | + const path1 = join(project.dir, "doc1.qmd"); |
| 50 | + const path2 = join(project.dir, "doc2.qmd"); |
| 51 | + |
| 52 | + const entry1 = ensureFileInformationCache(project, path1); |
| 53 | + const entry2 = ensureFileInformationCache(project, path2); |
| 54 | + |
| 55 | + assert( |
| 56 | + entry1 !== entry2, |
| 57 | + "Different paths should return different cache entries", |
| 58 | + ); |
| 59 | + assert( |
| 60 | + project.fileInformationCache.size === 2, |
| 61 | + "Should have two cache entries for different paths", |
| 62 | + ); |
| 63 | + }, |
| 64 | +); |
| 65 | + |
| 66 | +// deno-lint-ignore require-await |
| 67 | +unitTest( |
| 68 | + "fileInformationCache - cache entry persists across calls", |
| 69 | + async () => { |
| 70 | + const project = createMockProjectContext(); |
| 71 | + |
| 72 | + const path = join(project.dir, "doc.qmd"); |
| 73 | + |
| 74 | + // First call creates entry |
| 75 | + const entry1 = ensureFileInformationCache(project, path); |
| 76 | + // Modify the entry |
| 77 | + entry1.metadata = { title: "Test" }; |
| 78 | + |
| 79 | + // Second call should return same entry with our modification |
| 80 | + const entry2 = ensureFileInformationCache(project, path); |
| 81 | + |
| 82 | + assert( |
| 83 | + entry2.metadata?.title === "Test", |
| 84 | + "Cache entry should persist modifications", |
| 85 | + ); |
| 86 | + assert( |
| 87 | + entry1 === entry2, |
| 88 | + "Should return same cache entry object", |
| 89 | + ); |
| 90 | + }, |
| 91 | +); |
| 92 | + |
| 93 | +// deno-lint-ignore require-await |
| 94 | +unitTest( |
| 95 | + "ensureFileInformationCache - creates FileInformationCacheMap when cache is missing", |
| 96 | + async () => { |
| 97 | + const project = createMockProjectContext(); |
| 98 | + // Simulate minimal ProjectContext without cache (as in command-utils.ts) |
| 99 | + // deno-lint-ignore no-explicit-any |
| 100 | + (project as any).fileInformationCache = undefined; |
| 101 | + |
| 102 | + ensureFileInformationCache(project, join(project.dir, "doc.qmd")); |
| 103 | + |
| 104 | + assert( |
| 105 | + project.fileInformationCache instanceof FileInformationCacheMap, |
| 106 | + "Should create FileInformationCacheMap, not plain Map", |
| 107 | + ); |
| 108 | + }, |
| 109 | +); |
0 commit comments