-
Notifications
You must be signed in to change notification settings - Fork 434
Expand file tree
/
Copy pathfile-information-cache.test.ts
More file actions
132 lines (111 loc) · 3.74 KB
/
file-information-cache.test.ts
File metadata and controls
132 lines (111 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
* file-information-cache.test.ts
*
* Tests for fileInformationCache path normalization
* Related to issue #13955
*
* Copyright (C) 2026 Posit Software, PBC
*/
import { unitTest } from "../../test.ts";
import { assert } from "testing/asserts";
import { join, relative } from "../../../src/deno_ral/path.ts";
import {
ensureFileInformationCache,
FileInformationCacheMap,
} from "../../../src/project/project-shared.ts";
import { createMockProjectContext } from "./utils.ts";
// deno-lint-ignore require-await
unitTest(
"fileInformationCache - same path returns same entry",
async () => {
const project = createMockProjectContext();
// Use cross-platform absolute path (backslashes on Windows, forward on Linux)
const path1 = join(project.dir, "doc.qmd");
const path2 = join(project.dir, "doc.qmd");
const entry1 = ensureFileInformationCache(project, path1);
const entry2 = ensureFileInformationCache(project, path2);
assert(
entry1 === entry2,
"Same path should return same cache entry",
);
assert(
project.fileInformationCache.size === 1,
"Should have exactly one cache entry",
);
},
);
// deno-lint-ignore require-await
unitTest(
"fileInformationCache - different paths create different entries",
async () => {
const project = createMockProjectContext();
const path1 = join(project.dir, "doc1.qmd");
const path2 = join(project.dir, "doc2.qmd");
const entry1 = ensureFileInformationCache(project, path1);
const entry2 = ensureFileInformationCache(project, path2);
assert(
entry1 !== entry2,
"Different paths should return different cache entries",
);
assert(
project.fileInformationCache.size === 2,
"Should have two cache entries for different paths",
);
},
);
// deno-lint-ignore require-await
unitTest(
"fileInformationCache - cache entry persists across calls",
async () => {
const project = createMockProjectContext();
const path = join(project.dir, "doc.qmd");
// First call creates entry
const entry1 = ensureFileInformationCache(project, path);
// Modify the entry
entry1.metadata = { title: "Test" };
// Second call should return same entry with our modification
const entry2 = ensureFileInformationCache(project, path);
assert(
entry2.metadata?.title === "Test",
"Cache entry should persist modifications",
);
assert(
entry1 === entry2,
"Should return same cache entry object",
);
},
);
// deno-lint-ignore require-await
unitTest(
"ensureFileInformationCache - creates FileInformationCacheMap when cache is missing",
async () => {
const project = createMockProjectContext();
// Simulate minimal ProjectContext without cache (as in command-utils.ts)
// deno-lint-ignore no-explicit-any
(project as any).fileInformationCache = undefined;
ensureFileInformationCache(project, join(project.dir, "doc.qmd"));
assert(
project.fileInformationCache instanceof FileInformationCacheMap,
"Should create FileInformationCacheMap, not plain Map",
);
},
);
// deno-lint-ignore require-await
unitTest(
"fileInformationCache - relative and absolute paths share same entry",
async () => {
const project = createMockProjectContext();
const absolutePath = join(project.dir, "subdir", "page.qmd");
const relativePath = relative(Deno.cwd(), absolutePath);
const entry1 = ensureFileInformationCache(project, relativePath);
const entry2 = ensureFileInformationCache(project, absolutePath);
assert(
entry1 === entry2,
"Relative and absolute paths to same file should share a cache entry",
);
assert(
project.fileInformationCache.size === 1,
"Should have exactly one cache entry",
);
},
);