test(util): migrate log cleanup test to Effect (#27357)
This commit is contained in:
@@ -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 fs from "fs/promises"
|
||||||
import path from "path"
|
import path from "path"
|
||||||
|
import { CrossSpawnSpawner } from "@opencode-ai/core/cross-spawn-spawner"
|
||||||
import { Global } from "@opencode-ai/core/global"
|
import { Global } from "@opencode-ai/core/global"
|
||||||
import * as Log from "@opencode-ai/core/util/log"
|
import * as Log from "@opencode-ai/core/util/log"
|
||||||
import { tmpdir } from "../fixture/fixture"
|
import { tmpdirScoped } from "../fixture/fixture"
|
||||||
|
import { testEffect } from "../lib/effect"
|
||||||
|
|
||||||
const log = Global.Path.log
|
const it = testEffect(CrossSpawnSpawner.defaultLayer)
|
||||||
|
|
||||||
afterEach(() => {
|
function files(dir: string) {
|
||||||
Global.Path.log = log
|
return Effect.gen(function* () {
|
||||||
})
|
let last = ""
|
||||||
|
let same = 0
|
||||||
|
|
||||||
async function files(dir: string) {
|
for (let i = 0; i < 50; i++) {
|
||||||
let last = ""
|
const list = yield* Effect.promise(() => fs.readdir(dir).then((files) => files.sort()))
|
||||||
let same = 0
|
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")
|
||||||
|
}
|
||||||
|
|
||||||
for (let i = 0; i < 50; i++) {
|
return yield* Effect.promise(() => fs.readdir(dir).then((files) => files.sort()))
|
||||||
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()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
test("init cleanup keeps the newest timestamped logs", async () => {
|
it.live("init cleanup keeps the newest timestamped logs", () =>
|
||||||
await using tmp = await tmpdir()
|
Effect.gen(function* () {
|
||||||
Global.Path.log = tmp.path
|
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).not.toContain(list[0]!)
|
||||||
expect(next).toContain(list.at(-1)!)
|
expect(next).toContain(list.at(-1)!)
|
||||||
})
|
}),
|
||||||
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user