|
1 | | -import { afterEach, expect, test } from "bun:test" |
| 1 | +import { expect } from "bun:test" |
| 2 | +import { Effect } from "effect" |
2 | 3 | import fs from "fs/promises" |
3 | 4 | import path from "path" |
| 5 | +import { CrossSpawnSpawner } from "@opencode-ai/core/cross-spawn-spawner" |
4 | 6 | import { Global } from "@opencode-ai/core/global" |
5 | 7 | import * as Log from "@opencode-ai/core/util/log" |
6 | | -import { tmpdir } from "../fixture/fixture" |
7 | | - |
8 | | -const log = Global.Path.log |
9 | | - |
10 | | -afterEach(() => { |
11 | | - Global.Path.log = log |
12 | | -}) |
13 | | - |
14 | | -async function files(dir: string) { |
15 | | - let last = "" |
16 | | - let same = 0 |
17 | | - |
18 | | - for (let i = 0; i < 50; i++) { |
19 | | - const list = (await fs.readdir(dir)).sort() |
20 | | - const next = JSON.stringify(list) |
21 | | - same = next === last ? same + 1 : 0 |
22 | | - if (same >= 2 && list.length === 11) return list |
23 | | - last = next |
24 | | - await Bun.sleep(10) |
25 | | - } |
26 | | - |
27 | | - return (await fs.readdir(dir)).sort() |
| 8 | +import { tmpdirScoped } from "../fixture/fixture" |
| 9 | +import { testEffect } from "../lib/effect" |
| 10 | + |
| 11 | +const it = testEffect(CrossSpawnSpawner.defaultLayer) |
| 12 | + |
| 13 | +function files(dir: string) { |
| 14 | + return Effect.gen(function* () { |
| 15 | + let last = "" |
| 16 | + let same = 0 |
| 17 | + |
| 18 | + for (let i = 0; i < 50; i++) { |
| 19 | + const list = yield* Effect.promise(() => fs.readdir(dir).then((files) => files.sort())) |
| 20 | + const next = JSON.stringify(list) |
| 21 | + same = next === last ? same + 1 : 0 |
| 22 | + if (same >= 2 && list.length === 11) return list |
| 23 | + last = next |
| 24 | + yield* Effect.sleep("10 millis") |
| 25 | + } |
| 26 | + |
| 27 | + return yield* Effect.promise(() => fs.readdir(dir).then((files) => files.sort())) |
| 28 | + }) |
28 | 29 | } |
29 | 30 |
|
30 | | -test("init cleanup keeps the newest timestamped logs", async () => { |
31 | | - await using tmp = await tmpdir() |
32 | | - Global.Path.log = tmp.path |
| 31 | +it.live("init cleanup keeps the newest timestamped logs", () => |
| 32 | + Effect.gen(function* () { |
| 33 | + const log = Global.Path.log |
| 34 | + yield* Effect.addFinalizer(() => Effect.sync(() => (Global.Path.log = log))) |
| 35 | + const dir = yield* tmpdirScoped() |
| 36 | + Global.Path.log = dir |
33 | 37 |
|
34 | | - const list = Array.from({ length: 12 }, (_, i) => `2000-01-${String(i + 1).padStart(2, "0")}T000000.log`) |
| 38 | + const list = Array.from({ length: 12 }, (_, i) => `2000-01-${String(i + 1).padStart(2, "0")}T000000.log`) |
35 | 39 |
|
36 | | - await Promise.all(list.map((file) => fs.writeFile(path.join(tmp.path, file), file))) |
| 40 | + yield* Effect.all(list.map((file) => Effect.promise(() => fs.writeFile(path.join(dir, file), file)))) |
37 | 41 |
|
38 | | - await Log.init({ print: false, dev: false }) |
| 42 | + yield* Effect.promise(() => Log.init({ print: false, dev: false })) |
39 | 43 |
|
40 | | - const next = await files(tmp.path) |
| 44 | + const next = yield* files(dir) |
41 | 45 |
|
42 | | - expect(next).not.toContain(list[0]!) |
43 | | - expect(next).toContain(list.at(-1)!) |
44 | | -}) |
| 46 | + expect(next).not.toContain(list[0]!) |
| 47 | + expect(next).toContain(list.at(-1)!) |
| 48 | + }), |
| 49 | +) |
0 commit comments