Skip to content

Commit 8ad3a4b

Browse files
authored
test(util): migrate log cleanup test to Effect (#27357)
1 parent 533495a commit 8ad3a4b

1 file changed

Lines changed: 38 additions & 33 deletions

File tree

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,49 @@
1-
import { afterEach, expect, test } from "bun:test"
1+
import { expect } from "bun:test"
2+
import { Effect } from "effect"
23
import fs from "fs/promises"
34
import path from "path"
5+
import { CrossSpawnSpawner } from "@opencode-ai/core/cross-spawn-spawner"
46
import { Global } from "@opencode-ai/core/global"
57
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+
})
2829
}
2930

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
3337

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`)
3539

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))))
3741

38-
await Log.init({ print: false, dev: false })
42+
yield* Effect.promise(() => Log.init({ print: false, dev: false }))
3943

40-
const next = await files(tmp.path)
44+
const next = yield* files(dir)
4145

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

Comments
 (0)