|
1 | 1 | import { mkdtemp, rm } from "node:fs/promises"; |
2 | 2 | import { tmpdir } from "node:os"; |
3 | 3 | import { join } from "node:path"; |
4 | | -import { expect, mock, test } from "bun:test"; |
| 4 | +import { expect, test } from "bun:test"; |
5 | 5 |
|
6 | | -let nodeEvaluated = false; |
| 6 | +import { withMockedModuleDuring } from "../../testkit/mock-module.js"; |
7 | 7 |
|
8 | | -mock.module("@intx/storage-isogit/node", () => { |
9 | | - nodeEvaluated = true; |
10 | | - return { |
11 | | - createIsogitStore: async () => { |
12 | | - throw new Error("session open reached isogit"); |
13 | | - }, |
14 | | - }; |
15 | | -}); |
| 8 | +const isogitNode = import.meta.resolve("@intx/storage-isogit/node"); |
16 | 9 |
|
17 | 10 | test("importing the session store does not construct isogit storage", async () => { |
18 | | - await import("./optimized-context-store.js"); |
19 | | - expect(nodeEvaluated).toBe(false); |
| 11 | + let loads = 0; |
| 12 | + await withMockedModuleDuring( |
| 13 | + isogitNode, |
| 14 | + () => { |
| 15 | + loads += 1; |
| 16 | + return { |
| 17 | + createIsogitStore: async () => { |
| 18 | + throw new Error("session open reached isogit"); |
| 19 | + }, |
| 20 | + }; |
| 21 | + }, |
| 22 | + async () => { |
| 23 | + const before = loads; |
| 24 | + await import("./optimized-context-store.js"); |
| 25 | + expect(loads).toBe(before); |
| 26 | + }, |
| 27 | + ); |
20 | 28 | }); |
21 | 29 |
|
22 | 30 | test("opening a session constructs isogit storage", async () => { |
23 | | - const { createSessionStores } = await import("./optimized-context-store.js"); |
24 | 31 | const dir = await mkdtemp(join(tmpdir(), "isogit-boot-")); |
25 | 32 | try { |
26 | | - await expect(createSessionStores(dir)).rejects.toThrow( |
27 | | - "session open reached isogit", |
| 33 | + await withMockedModuleDuring( |
| 34 | + isogitNode, |
| 35 | + () => ({ |
| 36 | + createIsogitStore: async () => { |
| 37 | + throw new Error("session open reached isogit"); |
| 38 | + }, |
| 39 | + }), |
| 40 | + async () => { |
| 41 | + const { createSessionStores } = |
| 42 | + await import("./optimized-context-store.js"); |
| 43 | + await expect(createSessionStores(dir)).rejects.toThrow( |
| 44 | + "session open reached isogit", |
| 45 | + ); |
| 46 | + }, |
28 | 47 | ); |
29 | | - expect(nodeEvaluated).toBe(true); |
30 | 48 | } finally { |
31 | 49 | await rm(dir, { recursive: true, force: true }); |
32 | 50 | } |
|
0 commit comments