|
| 1 | +import assert from "node:assert"; |
| 2 | +import {parseMarkdown} from "../src/markdown.js"; |
| 3 | +import {getResolvers} from "../src/resolvers.js"; |
| 4 | + |
| 5 | +describe("getResolvers(page, {root, path})", () => { |
| 6 | + const builtins = ["npm:@observablehq/runtime", "npm:@observablehq/stdlib", "observablehq:client"]; |
| 7 | + it("resolves directly-attached files", async () => { |
| 8 | + const options = {root: "test/input", path: "attached.md"}; |
| 9 | + const page = parseMarkdown("${FileAttachment('foo.csv')}", options); |
| 10 | + const resolvers = await getResolvers(page, options); |
| 11 | + assert.deepStrictEqual(resolvers.files, new Set(["./foo.csv"])); |
| 12 | + }); |
| 13 | + it("ignores files that are outside of the source root", async () => { |
| 14 | + const options = {root: "test/input", path: "attached.md"}; |
| 15 | + const page = parseMarkdown("${FileAttachment('../foo.csv')}", options); |
| 16 | + const resolvers = await getResolvers(page, options); |
| 17 | + assert.deepStrictEqual(resolvers.files, new Set([])); |
| 18 | + }); |
| 19 | + it("detects file methods", async () => { |
| 20 | + const options = {root: "test/input", path: "attached.md"}; |
| 21 | + const page = parseMarkdown("${FileAttachment('foo.csv').csv}", options); |
| 22 | + const resolvers = await getResolvers(page, options); |
| 23 | + assert.deepStrictEqual(resolvers.staticImports, new Set(["npm:d3-dsv", ...builtins])); |
| 24 | + }); |
| 25 | + it("detects local static imports", async () => { |
| 26 | + const options = {root: "test/input/imports", path: "attached.md"}; |
| 27 | + const page = parseMarkdown("```js\nimport './bar.js';\n```", options); |
| 28 | + const resolvers = await getResolvers(page, options); |
| 29 | + assert.deepStrictEqual(resolvers.staticImports, new Set(["./bar.js", ...builtins])); |
| 30 | + assert.deepStrictEqual(resolvers.localImports, new Set(["./bar.js"])); |
| 31 | + }); |
| 32 | + it("detects local transitive static imports", async () => { |
| 33 | + const options = {root: "test/input/imports", path: "attached.md"}; |
| 34 | + const page = parseMarkdown("```js\nimport './other/foo.js';\n```", options); |
| 35 | + const resolvers = await getResolvers(page, options); |
| 36 | + assert.deepStrictEqual(resolvers.staticImports, new Set(["./other/foo.js", "./bar.js", ...builtins])); |
| 37 | + assert.deepStrictEqual(resolvers.localImports, new Set(["./other/foo.js", "./bar.js"])); |
| 38 | + }); |
| 39 | + it("detects local transitive static imports (2)", async () => { |
| 40 | + const options = {root: "test/input/imports", path: "attached.md"}; |
| 41 | + const page = parseMarkdown("```js\nimport './transitive-static-import.js';\n```", options); |
| 42 | + const resolvers = await getResolvers(page, options); |
| 43 | + assert.deepStrictEqual(resolvers.staticImports, new Set(["./transitive-static-import.js", "./other/foo.js", "./bar.js", ...builtins])); // prettier-ignore |
| 44 | + assert.deepStrictEqual(resolvers.localImports, new Set(["./transitive-static-import.js", "./other/foo.js", "./bar.js"])); // prettier-ignore |
| 45 | + }); |
| 46 | + it("detects local transitive dynamic imports", async () => { |
| 47 | + const options = {root: "test/input/imports", path: "attached.md"}; |
| 48 | + const page = parseMarkdown("```js\nimport './dynamic-import.js';\n```", options); |
| 49 | + const resolvers = await getResolvers(page, options); |
| 50 | + assert.deepStrictEqual(resolvers.staticImports, new Set(["./dynamic-import.js", ...builtins])); |
| 51 | + assert.deepStrictEqual(resolvers.localImports, new Set(["./dynamic-import.js", "./bar.js"])); |
| 52 | + }); |
| 53 | + it("detects local transitive dynamic imports (2)", async () => { |
| 54 | + const options = {root: "test/input/imports", path: "attached.md"}; |
| 55 | + const page = parseMarkdown("```js\nimport('./dynamic-import.js');\n```", options); |
| 56 | + const resolvers = await getResolvers(page, options); |
| 57 | + assert.deepStrictEqual(resolvers.staticImports, new Set(builtins)); |
| 58 | + assert.deepStrictEqual(resolvers.localImports, new Set(["./dynamic-import.js", "./bar.js"])); |
| 59 | + }); |
| 60 | + it("detects local transitive dynamic imports (3)", async () => { |
| 61 | + const options = {root: "test/input/imports", path: "attached.md"}; |
| 62 | + const page = parseMarkdown("```js\nimport('./transitive-dynamic-import.js');\n```", options); |
| 63 | + const resolvers = await getResolvers(page, options); |
| 64 | + assert.deepStrictEqual(resolvers.staticImports, new Set(builtins)); |
| 65 | + assert.deepStrictEqual(resolvers.localImports, new Set(["./transitive-dynamic-import.js", "./other/foo.js", "./bar.js"])); // prettier-ignore |
| 66 | + }); |
| 67 | + it("detects local transitive dynamic imports (4)", async () => { |
| 68 | + const options = {root: "test/input/imports", path: "attached.md"}; |
| 69 | + const page = parseMarkdown("```js\nimport('./transitive-static-import.js');\n```", options); |
| 70 | + const resolvers = await getResolvers(page, options); |
| 71 | + assert.deepStrictEqual(resolvers.staticImports, new Set(builtins)); |
| 72 | + assert.deepStrictEqual(resolvers.localImports, new Set(["./transitive-static-import.js", "./other/foo.js", "./bar.js"])); // prettier-ignore |
| 73 | + }); |
| 74 | + it("detects local dynamic imports", async () => { |
| 75 | + const options = {root: "test/input", path: "attached.md"}; |
| 76 | + const page = parseMarkdown("${import('./foo.js')}", options); |
| 77 | + const resolvers = await getResolvers(page, options); |
| 78 | + assert.deepStrictEqual(resolvers.staticImports, new Set(builtins)); |
| 79 | + assert.deepStrictEqual(resolvers.localImports, new Set(["./foo.js"])); |
| 80 | + }); |
| 81 | +}); |
0 commit comments