Просмотр исходного кода

test(util): migrate log cleanup test to Effect (#27357)

Kit Langton 3 месяцев назад
Родитель
Сommit
8ad3a4b217
1 измененных файлов с 38 добавлено и 33 удалено
  1. 38 33
      packages/opencode/test/util/log.test.ts

+ 38 - 33
packages/opencode/test/util/log.test.ts

@@ -1,44 +1,49 @@
-import { afterEach, expect, test } from "bun:test"
+import { expect } from "bun:test"
+import { Effect } from "effect"
 import fs from "fs/promises"
 import path from "path"
+import { CrossSpawnSpawner } from "@opencode-ai/core/cross-spawn-spawner"
 import { Global } from "@opencode-ai/core/global"
 import * as Log from "@opencode-ai/core/util/log"
-import { tmpdir } from "../fixture/fixture"
-
-const log = Global.Path.log
-
-afterEach(() => {
-  Global.Path.log = log
-})
-
-async function files(dir: string) {
-  let last = ""
-  let same = 0
-
-  for (let i = 0; i < 50; i++) {
-    const list = (await fs.readdir(dir)).sort()
-    const next = JSON.stringify(list)
-    same = next === last ? same + 1 : 0
-    if (same >= 2 && list.length === 11) return list
-    last = next
-    await Bun.sleep(10)
-  }
-
-  return (await fs.readdir(dir)).sort()
+import { tmpdirScoped } from "../fixture/fixture"
+import { testEffect } from "../lib/effect"
+
+const it = testEffect(CrossSpawnSpawner.defaultLayer)
+
+function files(dir: string) {
+  return Effect.gen(function* () {
+    let last = ""
+    let same = 0
+
+    for (let i = 0; i < 50; i++) {
+      const list = yield* Effect.promise(() => fs.readdir(dir).then((files) => files.sort()))
+      const next = JSON.stringify(list)
+      same = next === last ? same + 1 : 0
+      if (same >= 2 && list.length === 11) return list
+      last = next
+      yield* Effect.sleep("10 millis")
+    }
+
+    return yield* Effect.promise(() => fs.readdir(dir).then((files) => files.sort()))
+  })
 }
 
-test("init cleanup keeps the newest timestamped logs", async () => {
-  await using tmp = await tmpdir()
-  Global.Path.log = tmp.path
+it.live("init cleanup keeps the newest timestamped logs", () =>
+  Effect.gen(function* () {
+    const log = Global.Path.log
+    yield* Effect.addFinalizer(() => Effect.sync(() => (Global.Path.log = log)))
+    const dir = yield* tmpdirScoped()
+    Global.Path.log = dir
 
-  const list = Array.from({ length: 12 }, (_, i) => `2000-01-${String(i + 1).padStart(2, "0")}T000000.log`)
+    const list = Array.from({ length: 12 }, (_, i) => `2000-01-${String(i + 1).padStart(2, "0")}T000000.log`)
 
-  await Promise.all(list.map((file) => fs.writeFile(path.join(tmp.path, file), file)))
+    yield* Effect.all(list.map((file) => Effect.promise(() => fs.writeFile(path.join(dir, file), file))))
 
-  await Log.init({ print: false, dev: false })
+    yield* Effect.promise(() => Log.init({ print: false, dev: false }))
 
-  const next = await files(tmp.path)
+    const next = yield* files(dir)
 
-  expect(next).not.toContain(list[0]!)
-  expect(next).toContain(list.at(-1)!)
-})
+    expect(next).not.toContain(list[0]!)
+    expect(next).toContain(list.at(-1)!)
+  }),
+)