|
| 1 | +import { expect } from "bun:test" |
| 2 | +import { Effect } from "effect" |
| 3 | +import { CrossSpawnSpawner } from "@opencode-ai/core/cross-spawn-spawner" |
| 4 | +import { LayerNode } from "@opencode-ai/core/effect/layer-node" |
| 5 | +import { FSUtil } from "@opencode-ai/core/fs-util" |
| 6 | +import fs from "fs/promises" |
| 7 | +import path from "path" |
| 8 | +import { ConfigFingerprint } from "@/config/fingerprint" |
| 9 | +import { tmpdirScoped } from "../fixture/fixture" |
| 10 | +import { testEffect } from "../lib/effect" |
| 11 | + |
| 12 | +const it = testEffect(LayerNode.compile(LayerNode.group([CrossSpawnSpawner.node, FSUtil.node]))) |
| 13 | + |
| 14 | +const hash = (dir: string) => ConfigFingerprint.hashInstanceInputs(dir, dir) |
| 15 | + |
| 16 | +const write = (file: string, text: string) => Effect.promise(() => fs.writeFile(file, text)) |
| 17 | + |
| 18 | +// Point Global.Path.home at the temp project so the home-level .opencode |
| 19 | +// lookup stays hermetic. |
| 20 | +const withHome = <A, E, R>(dir: string, effect: Effect.Effect<A, E, R>) => |
| 21 | + Effect.acquireUseRelease( |
| 22 | + Effect.sync(() => { |
| 23 | + const previous = process.env.OPENCODE_TEST_HOME |
| 24 | + process.env.OPENCODE_TEST_HOME = dir |
| 25 | + return previous |
| 26 | + }), |
| 27 | + () => effect, |
| 28 | + (previous) => |
| 29 | + Effect.sync(() => { |
| 30 | + if (previous === undefined) delete process.env.OPENCODE_TEST_HOME |
| 31 | + else process.env.OPENCODE_TEST_HOME = previous |
| 32 | + }), |
| 33 | + ) |
| 34 | + |
| 35 | +const inTmpProject = <A, E, R>(fn: (dir: string) => Effect.Effect<A, E, R>) => |
| 36 | + Effect.gen(function* () { |
| 37 | + const dir = yield* tmpdirScoped() |
| 38 | + return yield* withHome(dir, fn(dir)) |
| 39 | + }) |
| 40 | + |
| 41 | +it.effect("is stable across calls when nothing changed", () => |
| 42 | + inTmpProject((dir) => |
| 43 | + Effect.gen(function* () { |
| 44 | + expect(yield* hash(dir)).toBe(yield* hash(dir)) |
| 45 | + }), |
| 46 | + ), |
| 47 | +) |
| 48 | + |
| 49 | +it.effect("ignores key reordering in the project config", () => |
| 50 | + inTmpProject((dir) => |
| 51 | + Effect.gen(function* () { |
| 52 | + const file = path.join(dir, "opencode.json") |
| 53 | + yield* write(file, JSON.stringify({ username: "alice", logLevel: "DEBUG" })) |
| 54 | + const before = yield* hash(dir) |
| 55 | + yield* write(file, JSON.stringify({ logLevel: "DEBUG", username: "alice" })) |
| 56 | + expect(yield* hash(dir)).toBe(before) |
| 57 | + }), |
| 58 | + ), |
| 59 | +) |
| 60 | + |
| 61 | +it.effect("detects project config edits", () => |
| 62 | + inTmpProject((dir) => |
| 63 | + Effect.gen(function* () { |
| 64 | + const file = path.join(dir, "opencode.json") |
| 65 | + yield* write(file, JSON.stringify({ username: "alice" })) |
| 66 | + const before = yield* hash(dir) |
| 67 | + yield* write(file, JSON.stringify({ username: "bob" })) |
| 68 | + expect(yield* hash(dir)).not.toBe(before) |
| 69 | + }), |
| 70 | + ), |
| 71 | +) |
| 72 | + |
| 73 | +it.effect("detects added agent files", () => |
| 74 | + inTmpProject((dir) => |
| 75 | + Effect.gen(function* () { |
| 76 | + const before = yield* hash(dir) |
| 77 | + yield* Effect.promise(() => fs.mkdir(path.join(dir, ".opencode", "agent"), { recursive: true })) |
| 78 | + yield* write(path.join(dir, ".opencode", "agent", "reviewer.md"), "review things") |
| 79 | + expect(yield* hash(dir)).not.toBe(before) |
| 80 | + }), |
| 81 | + ), |
| 82 | +) |
| 83 | + |
| 84 | +it.effect("ignores theme files, which are rendering-only", () => |
| 85 | + inTmpProject((dir) => |
| 86 | + Effect.gen(function* () { |
| 87 | + const before = yield* hash(dir) |
| 88 | + yield* Effect.promise(() => fs.mkdir(path.join(dir, ".opencode", "themes"), { recursive: true })) |
| 89 | + yield* write(path.join(dir, ".opencode", "themes", "night.json"), JSON.stringify({ theme: {} })) |
| 90 | + expect(yield* hash(dir)).toBe(before) |
| 91 | + }), |
| 92 | + ), |
| 93 | +) |
| 94 | + |
| 95 | +it.effect("detects plugin file changes", () => |
| 96 | + inTmpProject((dir) => |
| 97 | + Effect.gen(function* () { |
| 98 | + const plugin = path.join(dir, ".opencode", "plugin") |
| 99 | + yield* Effect.promise(() => fs.mkdir(plugin, { recursive: true })) |
| 100 | + const file = path.join(plugin, "x.ts") |
| 101 | + yield* write(file, "export const a = 1") |
| 102 | + const before = yield* hash(dir) |
| 103 | + yield* write(file, "export const a = 2") |
| 104 | + expect(yield* hash(dir)).not.toBe(before) |
| 105 | + }), |
| 106 | + ), |
| 107 | +) |
| 108 | + |
| 109 | +it.effect("treats a malformed config as a state and detects fixing it", () => |
| 110 | + inTmpProject((dir) => |
| 111 | + Effect.gen(function* () { |
| 112 | + const file = path.join(dir, "opencode.json") |
| 113 | + yield* write(file, "{ not json") |
| 114 | + const broken = yield* hash(dir) |
| 115 | + yield* write(file, JSON.stringify({ username: "alice" })) |
| 116 | + expect(yield* hash(dir)).not.toBe(broken) |
| 117 | + }), |
| 118 | + ), |
| 119 | +) |
0 commit comments