|
| 1 | +/* |
| 2 | + * preview-initial-path.test.ts |
| 3 | + * |
| 4 | + * Tests that previewInitialPath computes the correct browse URL path |
| 5 | + * for different project types. Regression test for #14298. |
| 6 | + * |
| 7 | + * Copyright (C) 2026 Posit Software, PBC |
| 8 | + */ |
| 9 | + |
| 10 | +import { unitTest } from "../test.ts"; |
| 11 | +import { assertEquals } from "testing/asserts"; |
| 12 | +import { join } from "../../src/deno_ral/path.ts"; |
| 13 | +import { normalizePath } from "../../src/core/path.ts"; |
| 14 | +import { previewInitialPath } from "../../src/command/preview/preview.ts"; |
| 15 | +import { ProjectContext } from "../../src/project/types.ts"; |
| 16 | + |
| 17 | +function mockProjectContext( |
| 18 | + dir: string, |
| 19 | + isSingleFile: boolean, |
| 20 | +): ProjectContext { |
| 21 | + return { |
| 22 | + dir: normalizePath(dir), |
| 23 | + isSingleFile, |
| 24 | + engines: [], |
| 25 | + files: { input: [], resources: [], config: [], configResources: [] }, |
| 26 | + config: { project: {} }, |
| 27 | + notebookContext: () => ({ resolve: () => undefined, get: () => undefined }), |
| 28 | + resolveFullMarkdownForFile: () => Promise.resolve(undefined), |
| 29 | + cleanup: () => {}, |
| 30 | + } as unknown as ProjectContext; |
| 31 | +} |
| 32 | + |
| 33 | +// deno-lint-ignore require-await |
| 34 | +unitTest("previewInitialPath - single file returns empty path (#14298)", async () => { |
| 35 | + const dir = normalizePath(Deno.makeTempDirSync({ prefix: "quarto-test" })); |
| 36 | + const outputFile = join(dir, "hello.html"); |
| 37 | + const project = mockProjectContext(dir, true); |
| 38 | + |
| 39 | + const result = previewInitialPath(outputFile, project); |
| 40 | + assertEquals(result, "", "Single-file preview should use root path, not filename"); |
| 41 | + |
| 42 | + Deno.removeSync(dir, { recursive: true }); |
| 43 | +}); |
| 44 | + |
| 45 | +// deno-lint-ignore require-await |
| 46 | +unitTest("previewInitialPath - project file returns relative path", async () => { |
| 47 | + const dir = normalizePath(Deno.makeTempDirSync({ prefix: "quarto-test" })); |
| 48 | + const outputFile = join(dir, "chapter.html"); |
| 49 | + const project = mockProjectContext(dir, false); |
| 50 | + |
| 51 | + const result = previewInitialPath(outputFile, project); |
| 52 | + assertEquals(result, "chapter.html", "Project preview should include relative path"); |
| 53 | + |
| 54 | + Deno.removeSync(dir, { recursive: true }); |
| 55 | +}); |
| 56 | + |
| 57 | +// deno-lint-ignore require-await |
| 58 | +unitTest("previewInitialPath - project subdir returns relative path", async () => { |
| 59 | + const dir = normalizePath(Deno.makeTempDirSync({ prefix: "quarto-test" })); |
| 60 | + const outputFile = join(dir, "pages", "about.html"); |
| 61 | + const project = mockProjectContext(dir, false); |
| 62 | + |
| 63 | + const result = previewInitialPath(outputFile, project); |
| 64 | + assertEquals(result, "pages/about.html", "Project preview should include subdirectory path"); |
| 65 | + |
| 66 | + Deno.removeSync(dir, { recursive: true }); |
| 67 | +}); |
| 68 | + |
| 69 | +// deno-lint-ignore require-await |
| 70 | +unitTest("previewInitialPath - undefined project returns empty path", async () => { |
| 71 | + const result = previewInitialPath("/tmp/hello.html", undefined); |
| 72 | + assertEquals(result, "", "No project should use root path"); |
| 73 | +}); |
0 commit comments