Path: blob/main/tests/unit/project/file-information-cache.test.ts
6451 views
/*1* file-information-cache.test.ts2*3* Tests for fileInformationCache path normalization4* Related to issue #139555*6* Copyright (C) 2026 Posit Software, PBC7*/89import { unitTest } from "../../test.ts";10import { assert } from "testing/asserts";11import { join } from "../../../src/deno_ral/path.ts";12import {13ensureFileInformationCache,14FileInformationCacheMap,15} from "../../../src/project/project-shared.ts";16import { createMockProjectContext } from "./utils.ts";1718// deno-lint-ignore require-await19unitTest(20"fileInformationCache - same path returns same entry",21async () => {22const project = createMockProjectContext();2324// Use cross-platform absolute path (backslashes on Windows, forward on Linux)25const path1 = join(project.dir, "doc.qmd");26const path2 = join(project.dir, "doc.qmd");2728const entry1 = ensureFileInformationCache(project, path1);29const entry2 = ensureFileInformationCache(project, path2);3031assert(32entry1 === entry2,33"Same path should return same cache entry",34);35assert(36project.fileInformationCache.size === 1,37"Should have exactly one cache entry",38);39},40);4142// deno-lint-ignore require-await43unitTest(44"fileInformationCache - different paths create different entries",45async () => {46const project = createMockProjectContext();4748const path1 = join(project.dir, "doc1.qmd");49const path2 = join(project.dir, "doc2.qmd");5051const entry1 = ensureFileInformationCache(project, path1);52const entry2 = ensureFileInformationCache(project, path2);5354assert(55entry1 !== entry2,56"Different paths should return different cache entries",57);58assert(59project.fileInformationCache.size === 2,60"Should have two cache entries for different paths",61);62},63);6465// deno-lint-ignore require-await66unitTest(67"fileInformationCache - cache entry persists across calls",68async () => {69const project = createMockProjectContext();7071const path = join(project.dir, "doc.qmd");7273// First call creates entry74const entry1 = ensureFileInformationCache(project, path);75// Modify the entry76entry1.metadata = { title: "Test" };7778// Second call should return same entry with our modification79const entry2 = ensureFileInformationCache(project, path);8081assert(82entry2.metadata?.title === "Test",83"Cache entry should persist modifications",84);85assert(86entry1 === entry2,87"Should return same cache entry object",88);89},90);9192// deno-lint-ignore require-await93unitTest(94"ensureFileInformationCache - creates FileInformationCacheMap when cache is missing",95async () => {96const project = createMockProjectContext();97// Simulate minimal ProjectContext without cache (as in command-utils.ts)98// deno-lint-ignore no-explicit-any99(project as any).fileInformationCache = undefined;100101ensureFileInformationCache(project, join(project.dir, "doc.qmd"));102103assert(104project.fileInformationCache instanceof FileInformationCacheMap,105"Should create FileInformationCacheMap, not plain Map",106);107},108);109110111