|
| 1 | +import assert from "node:assert"; |
| 2 | +import {FileAttachment, registerFile} from "../../../src/client/stdlib/fileAttachment.js"; |
| 3 | + |
| 4 | +describe("FileAttachment(name)", () => { |
| 5 | + before(() => { |
| 6 | + globalThis.location = new URL("http://localhost:3000/test") as any; |
| 7 | + registerFile("./test.csv", {"name":"./test.csv","mimeType":"text/csv","path":"./_file/test.csv?sha=edd06c0d902e9ce083a9a5d8d0e655732c72e8129d3c60bfe69d228265e892d6"}); // prettier-ignore |
| 8 | + }); |
| 9 | + after(() => { |
| 10 | + delete (globalThis as any).location; |
| 11 | + }); |
| 12 | + it("returns the file at the specified path", async () => { |
| 13 | + const f = FileAttachment("./test.csv"); |
| 14 | + assert.strictEqual(f.name, "test.csv"); |
| 15 | + assert.strictEqual(f.mimeType, "text/csv"); |
| 16 | + assert.strictEqual(await f.url(), "http://localhost:3000/_file/test.csv?sha=edd06c0d902e9ce083a9a5d8d0e655732c72e8129d3c60bfe69d228265e892d6"); // prettier-ignore |
| 17 | + }); |
| 18 | + it("normalizes the specified file path", async () => { |
| 19 | + const f = FileAttachment("test.csv"); |
| 20 | + assert.strictEqual(f.name, "test.csv"); |
| 21 | + assert.strictEqual(f.mimeType, "text/csv"); |
| 22 | + assert.strictEqual(await f.url(), "http://localhost:3000/_file/test.csv?sha=edd06c0d902e9ce083a9a5d8d0e655732c72e8129d3c60bfe69d228265e892d6"); // prettier-ignore |
| 23 | + }); |
| 24 | + it("resolves the specified path relative to the current location", async () => { |
| 25 | + const f = FileAttachment("/test.csv"); |
| 26 | + assert.strictEqual(f.name, "test.csv"); |
| 27 | + assert.strictEqual(f.mimeType, "text/csv"); |
| 28 | + assert.strictEqual(await f.url(), "http://localhost:3000/_file/test.csv?sha=edd06c0d902e9ce083a9a5d8d0e655732c72e8129d3c60bfe69d228265e892d6"); // prettier-ignore |
| 29 | + }); |
| 30 | + it("resolves the specified path relative to the specified location (1)", async () => { |
| 31 | + const f = FileAttachment("/test.csv", "http://localhost:3000/sub/path"); |
| 32 | + assert.strictEqual(f.name, "test.csv"); |
| 33 | + assert.strictEqual(f.mimeType, "text/csv"); |
| 34 | + assert.strictEqual(await f.url(), "http://localhost:3000/_file/test.csv?sha=edd06c0d902e9ce083a9a5d8d0e655732c72e8129d3c60bfe69d228265e892d6"); // prettier-ignore |
| 35 | + }); |
| 36 | + it("resolves the specified path relative to the specified location (2)", async () => { |
| 37 | + const f = FileAttachment("../test.csv", "http://localhost:3000/sub/path"); |
| 38 | + assert.strictEqual(f.name, "test.csv"); |
| 39 | + assert.strictEqual(f.mimeType, "text/csv"); |
| 40 | + assert.strictEqual(await f.url(), "http://localhost:3000/_file/test.csv?sha=edd06c0d902e9ce083a9a5d8d0e655732c72e8129d3c60bfe69d228265e892d6"); // prettier-ignore |
| 41 | + }); |
| 42 | + it("throws an error if the file does not exist", async () => { |
| 43 | + assert.throws(() => FileAttachment("does-not-exist.csv"), /file not found/i); |
| 44 | + assert.throws(() => FileAttachment("test.csv?found=not"), /file not found/i); |
| 45 | + assert.throws(() => FileAttachment("test.csv", "http://localhost:3000/sub/path"), /file not found/i); |
| 46 | + }); |
| 47 | + it("throws an error if used as a constructor", async () => { |
| 48 | + assert.throws(() => new FileAttachment("test.csv"), /not a constructor/i); |
| 49 | + }); |
| 50 | +}); |
0 commit comments