-
Notifications
You must be signed in to change notification settings - Fork 432
Expand file tree
/
Copy pathfile-information-cache.test.ts
More file actions
209 lines (178 loc) · 6.31 KB
/
file-information-cache.test.ts
File metadata and controls
209 lines (178 loc) · 6.31 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
* 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 { asMappedString } from "../../../src/core/lib/mapped-text.ts";
import { existsSync } from "../../../src/deno_ral/fs.ts";
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",
);
},
);
// deno-lint-ignore require-await
unitTest(
"fileInformationCache - invalidateForFile deletes transient notebook file",
async () => {
const project = createMockProjectContext();
const sourcePath = join(project.dir, "doc.qmd");
// Create a real temp file simulating a transient .quarto_ipynb
const notebookPath = join(project.dir, "doc.quarto_ipynb");
Deno.writeTextFileSync(notebookPath, '{"cells": []}');
assert(existsSync(notebookPath), "Temp notebook file should exist");
// Populate cache entry with a transient target pointing to the file
const entry = ensureFileInformationCache(project, sourcePath);
entry.target = {
source: sourcePath,
input: notebookPath,
markdown: asMappedString(""),
metadata: {},
data: { transient: true, kernelspec: {} },
};
// Invalidate the cache entry for this file
project.fileInformationCache.invalidateForFile(sourcePath);
// The transient file should be deleted from disk
assert(
!existsSync(notebookPath),
"Transient notebook file should be deleted on invalidation",
);
// The cache entry should be removed
assert(
!project.fileInformationCache.has(sourcePath),
"Cache entry should be removed after invalidation",
);
},
);
// deno-lint-ignore require-await
unitTest(
"fileInformationCache - invalidateForFile preserves non-transient files",
async () => {
const project = createMockProjectContext();
const sourcePath = join(project.dir, "notebook.ipynb");
// Create a real file simulating a user's .ipynb (non-transient)
const notebookPath = join(project.dir, "notebook.ipynb");
Deno.writeTextFileSync(notebookPath, '{"cells": []}');
// Populate cache entry with a non-transient target
const entry = ensureFileInformationCache(project, sourcePath);
entry.target = {
source: sourcePath,
input: notebookPath,
markdown: asMappedString(""),
metadata: {},
data: { transient: false, kernelspec: {} },
};
// Invalidate the cache entry
project.fileInformationCache.invalidateForFile(sourcePath);
// The non-transient file should NOT be deleted
assert(
existsSync(notebookPath),
"Non-transient file should be preserved on invalidation",
);
// But the cache entry should still be removed
assert(
!project.fileInformationCache.has(sourcePath),
"Cache entry should be removed after invalidation",
);
},
);