persistence.test.ts 1011 B

1234567891011121314151617181920212223
  1. import { expect, test } from "bun:test"
  2. import path from "path"
  3. import { mkdtemp, rm } from "fs/promises"
  4. import { tmpdir } from "os"
  5. import { appendText, readJson, readText, writeJsonAtomic, writeText } from "../../src/util/persistence"
  6. test("persistence creates parent directories and supports text, append, and JSON", async () => {
  7. const root = await mkdtemp(path.join(tmpdir(), "opencode-tui-persistence-"))
  8. try {
  9. const textPath = path.join(root, "nested", "state.jsonl")
  10. await writeText(textPath, "one\n")
  11. await appendText(textPath, "two\n")
  12. expect(await readText(textPath)).toBe("one\ntwo\n")
  13. const jsonPath = path.join(root, "other", "state.json")
  14. await writeJsonAtomic(jsonPath, { value: 1 })
  15. expect(await readJson<{ value: number }>(jsonPath)).toEqual({ value: 1 })
  16. await writeJsonAtomic(jsonPath, { value: 2 })
  17. expect(await readJson<{ value: number }>(jsonPath)).toEqual({ value: 2 })
  18. } finally {
  19. await rm(root, { recursive: true, force: true })
  20. }
  21. })