|
9 | 9 |
|
10 | 10 | import { unitTest } from "../../test.ts"; |
11 | 11 | import { assert } from "testing/asserts"; |
| 12 | +import { asMappedString } from "../../../src/core/lib/mapped-text.ts"; |
| 13 | +import { existsSync } from "../../../src/deno_ral/fs.ts"; |
12 | 14 | import { join, relative } from "../../../src/deno_ral/path.ts"; |
13 | 15 | import { |
14 | 16 | ensureFileInformationCache, |
@@ -130,3 +132,78 @@ unitTest( |
130 | 132 | ); |
131 | 133 | }, |
132 | 134 | ); |
| 135 | + |
| 136 | +// deno-lint-ignore require-await |
| 137 | +unitTest( |
| 138 | + "fileInformationCache - invalidateForFile deletes transient notebook file", |
| 139 | + async () => { |
| 140 | + const project = createMockProjectContext(); |
| 141 | + const sourcePath = join(project.dir, "doc.qmd"); |
| 142 | + |
| 143 | + // Create a real temp file simulating a transient .quarto_ipynb |
| 144 | + const notebookPath = join(project.dir, "doc.quarto_ipynb"); |
| 145 | + Deno.writeTextFileSync(notebookPath, '{"cells": []}'); |
| 146 | + assert(existsSync(notebookPath), "Temp notebook file should exist"); |
| 147 | + |
| 148 | + // Populate cache entry with a transient target pointing to the file |
| 149 | + const entry = ensureFileInformationCache(project, sourcePath); |
| 150 | + entry.target = { |
| 151 | + source: sourcePath, |
| 152 | + input: notebookPath, |
| 153 | + markdown: asMappedString(""), |
| 154 | + metadata: {}, |
| 155 | + data: { transient: true, kernelspec: {} }, |
| 156 | + }; |
| 157 | + |
| 158 | + // Invalidate the cache entry for this file |
| 159 | + project.fileInformationCache.invalidateForFile(sourcePath); |
| 160 | + |
| 161 | + // The transient file should be deleted from disk |
| 162 | + assert( |
| 163 | + !existsSync(notebookPath), |
| 164 | + "Transient notebook file should be deleted on invalidation", |
| 165 | + ); |
| 166 | + // The cache entry should be removed |
| 167 | + assert( |
| 168 | + !project.fileInformationCache.has(sourcePath), |
| 169 | + "Cache entry should be removed after invalidation", |
| 170 | + ); |
| 171 | + }, |
| 172 | +); |
| 173 | + |
| 174 | +// deno-lint-ignore require-await |
| 175 | +unitTest( |
| 176 | + "fileInformationCache - invalidateForFile preserves non-transient files", |
| 177 | + async () => { |
| 178 | + const project = createMockProjectContext(); |
| 179 | + const sourcePath = join(project.dir, "notebook.ipynb"); |
| 180 | + |
| 181 | + // Create a real file simulating a user's .ipynb (non-transient) |
| 182 | + const notebookPath = join(project.dir, "notebook.ipynb"); |
| 183 | + Deno.writeTextFileSync(notebookPath, '{"cells": []}'); |
| 184 | + |
| 185 | + // Populate cache entry with a non-transient target |
| 186 | + const entry = ensureFileInformationCache(project, sourcePath); |
| 187 | + entry.target = { |
| 188 | + source: sourcePath, |
| 189 | + input: notebookPath, |
| 190 | + markdown: asMappedString(""), |
| 191 | + metadata: {}, |
| 192 | + data: { transient: false, kernelspec: {} }, |
| 193 | + }; |
| 194 | + |
| 195 | + // Invalidate the cache entry |
| 196 | + project.fileInformationCache.invalidateForFile(sourcePath); |
| 197 | + |
| 198 | + // The non-transient file should NOT be deleted |
| 199 | + assert( |
| 200 | + existsSync(notebookPath), |
| 201 | + "Non-transient file should be preserved on invalidation", |
| 202 | + ); |
| 203 | + // But the cache entry should still be removed |
| 204 | + assert( |
| 205 | + !project.fileInformationCache.has(sourcePath), |
| 206 | + "Cache entry should be removed after invalidation", |
| 207 | + ); |
| 208 | + }, |
| 209 | +); |
0 commit comments