|
| 1 | +import { assertEquals } from "@std/assert/equals"; |
| 2 | +import { assertRejects } from "@std/assert/rejects"; |
| 3 | +import { fromFileUrl } from "@std/path/from-file-url"; |
| 4 | +import { toFileUrl } from "@std/path/to-file-url"; |
| 5 | +import { join } from "@std/path/join"; |
| 6 | +import { Loader } from "../loader.ts"; |
| 7 | + |
| 8 | +const TEST_DATA_DIR = join( |
| 9 | + fromFileUrl(new URL(import.meta.url)), |
| 10 | + "..", |
| 11 | + "testdata", |
| 12 | +); |
| 13 | + |
| 14 | +function fixturePath(name: string): string { |
| 15 | + return join(TEST_DATA_DIR, `${name}.ts`); |
| 16 | +} |
| 17 | + |
| 18 | +Deno.test("registerPath: registers a single ext path without error", async () => { |
| 19 | + const loader = new Loader(); |
| 20 | + const path = fixturePath("ext_alpha"); |
| 21 | + await loader.registerPath("ext", path); |
| 22 | +}); |
| 23 | + |
| 24 | +Deno.test( |
| 25 | + "registerPath: concurrent registration of the same path is idempotent", |
| 26 | + async () => { |
| 27 | + const loader = new Loader(); |
| 28 | + const path = fixturePath("ext_alpha"); |
| 29 | + |
| 30 | + // Fire 8 concurrent registrations for the same path. |
| 31 | + const results = await Promise.allSettled( |
| 32 | + Array.from({ length: 8 }, () => loader.registerPath("ext", path)), |
| 33 | + ); |
| 34 | + |
| 35 | + // All should resolve (no rejections despite concurrent calls). |
| 36 | + const rejected = results.filter((r) => r.status === "rejected"); |
| 37 | + assertEquals(rejected.length, 0, "No registration should fail"); |
| 38 | + }, |
| 39 | +); |
| 40 | + |
| 41 | +Deno.test( |
| 42 | + "registerPath: concurrent registration of different paths succeeds", |
| 43 | + async () => { |
| 44 | + const loader = new Loader(); |
| 45 | + const paths = [ |
| 46 | + fixturePath("ext_alpha"), |
| 47 | + fixturePath("ext_beta"), |
| 48 | + fixturePath("ext_gamma"), |
| 49 | + ]; |
| 50 | + |
| 51 | + const results = await Promise.allSettled( |
| 52 | + paths.map((p) => loader.registerPath("ext", p)), |
| 53 | + ); |
| 54 | + |
| 55 | + const rejected = results.filter((r) => r.status === "rejected"); |
| 56 | + assertEquals( |
| 57 | + rejected.length, |
| 58 | + 0, |
| 59 | + "All different-path registrations should succeed", |
| 60 | + ); |
| 61 | + }, |
| 62 | +); |
| 63 | + |
| 64 | +Deno.test( |
| 65 | + "registerPath: registering a non-existent path throws", |
| 66 | + async () => { |
| 67 | + const loader = new Loader(); |
| 68 | + const badPath = join(TEST_DATA_DIR, "nonexistent.ts"); |
| 69 | + |
| 70 | + await assertRejects( |
| 71 | + () => loader.registerPath("ext", badPath), |
| 72 | + Error, |
| 73 | + ); |
| 74 | + }, |
| 75 | +); |
| 76 | + |
| 77 | +Deno.test("registerPaths: registers multiple paths concurrently", async () => { |
| 78 | + const loader = new Loader(); |
| 79 | + const paths = [ |
| 80 | + fixturePath("ext_alpha"), |
| 81 | + fixturePath("ext_beta"), |
| 82 | + fixturePath("ext_gamma"), |
| 83 | + ]; |
| 84 | + |
| 85 | + // registerPaths should not throw even on success. |
| 86 | + await loader.registerPaths("ext", paths); |
| 87 | +}); |
| 88 | + |
| 89 | +Deno.test( |
| 90 | + "registerPaths: does not throw when some paths are invalid", |
| 91 | + async () => { |
| 92 | + const loader = new Loader(); |
| 93 | + const paths = [ |
| 94 | + fixturePath("ext_alpha"), |
| 95 | + join(TEST_DATA_DIR, "nonexistent.ts"), |
| 96 | + fixturePath("ext_beta"), |
| 97 | + ]; |
| 98 | + |
| 99 | + // registerPaths absorbs errors internally and must not throw. |
| 100 | + await loader.registerPaths("ext", paths); |
| 101 | + }, |
| 102 | +); |
| 103 | + |
| 104 | +Deno.test( |
| 105 | + "registerPaths: concurrent calls for the same paths are idempotent", |
| 106 | + async () => { |
| 107 | + const loader = new Loader(); |
| 108 | + const paths = [fixturePath("ext_alpha"), fixturePath("ext_beta")]; |
| 109 | + |
| 110 | + // Two concurrent registerPaths calls with overlapping paths. |
| 111 | + const [r1, r2] = await Promise.allSettled([ |
| 112 | + loader.registerPaths("ext", paths), |
| 113 | + loader.registerPaths("ext", paths), |
| 114 | + ]); |
| 115 | + |
| 116 | + assertEquals(r1.status, "fulfilled"); |
| 117 | + assertEquals(r2.status, "fulfilled"); |
| 118 | + }, |
| 119 | +); |
| 120 | + |
| 121 | +Deno.test( |
| 122 | + "registerPath: re-registration of the same path after initial success is a no-op", |
| 123 | + async () => { |
| 124 | + const loader = new Loader(); |
| 125 | + const path = fixturePath("ext_alpha"); |
| 126 | + |
| 127 | + await loader.registerPath("ext", path); |
| 128 | + // Second call must succeed without error (fast-path check). |
| 129 | + await loader.registerPath("ext", path); |
| 130 | + }, |
| 131 | +); |
| 132 | + |
| 133 | +// Verify that the module URL used in import.meta.url resolves to a real file. |
| 134 | +Deno.test("testdata fixtures are accessible", async () => { |
| 135 | + for (const name of ["ext_alpha", "ext_beta", "ext_gamma"]) { |
| 136 | + const p = fixturePath(name); |
| 137 | + const stat = await Deno.stat(p); |
| 138 | + assertEquals(stat.isFile, true, `${name}.ts should be a file`); |
| 139 | + } |
| 140 | +}); |
| 141 | + |
| 142 | +// Smoke-test: ensure toFileUrl/fromFileUrl round-trips work correctly for |
| 143 | +// fixture paths (importPlugin relies on toFileUrl internally). |
| 144 | +Deno.test("fixture paths survive toFileUrl/fromFileUrl round-trip", () => { |
| 145 | + const path = fixturePath("ext_alpha"); |
| 146 | + const url = toFileUrl(path).href; |
| 147 | + const back = fromFileUrl(url); |
| 148 | + assertEquals(back, path); |
| 149 | +}); |
0 commit comments