|
| 1 | +import { expect, test } from "bun:test" |
| 2 | +import { spawnSync } from "node:child_process" |
| 3 | +import { readFile, writeFile } from "node:fs/promises" |
| 4 | +import path from "node:path" |
| 5 | +import { Effect } from "effect" |
| 6 | +import { and, desc, eq } from "drizzle-orm" |
| 7 | +import { Bus } from "@opencode/core/bus" |
| 8 | +import { Database } from "@opencode/core/database/database" |
| 9 | +import { AppNodeBuilder } from "@opencode/core/effect/app-node-builder" |
| 10 | +import { SessionEvent } from "@opencode/core/session/event" |
| 11 | +import { SessionMessage } from "@opencode/core/session/message" |
| 12 | +import { SessionProjector } from "@opencode/core/session/projector" |
| 13 | +import { SessionSchema } from "@opencode/core/session/schema" |
| 14 | +import { SessionStore } from "@opencode/core/session/store" |
| 15 | +import { EventSequenceTable } from "@opencode/core/event/sql" |
| 16 | +import { SessionMessageTable } from "@opencode/core/session/sql" |
| 17 | +import { LayerNode } from "@opencode/util/effect/layer-node" |
| 18 | +import { tmpdir } from "./fixture/tmpdir" |
| 19 | + |
| 20 | +const script = path.resolve(import.meta.dir, "../script/fixture-v2-pagination.ts") |
| 21 | +const sessionID = SessionSchema.ID.make("ses_fixture_001") |
| 22 | + |
| 23 | +const runScript = (filename: string) => spawnSync(process.execPath, ["run", script, filename], { encoding: "utf8" }) |
| 24 | + |
| 25 | +test("fixture data traverses production session pagination and remains appendable", async () => { |
| 26 | + await using directory = await tmpdir("opencode-v2-pagination-") |
| 27 | + const filename = path.join(directory.path, "fixture.sqlite") |
| 28 | + const created = runScript(filename) |
| 29 | + expect(created.status, created.stderr).toBe(0) |
| 30 | + |
| 31 | + const layer = AppNodeBuilder.build( |
| 32 | + LayerNode.group([Database.node, Bus.node, SessionProjector.node, SessionStore.node]), |
| 33 | + [ |
| 34 | + Database.node.replace(Database.configured({ path: filename })), |
| 35 | + Bus.node.replace(Bus.configured({ persist: true })), |
| 36 | + ], |
| 37 | + ) |
| 38 | + const report = await Effect.runPromise( |
| 39 | + Effect.scoped( |
| 40 | + Effect.provide( |
| 41 | + Effect.gen(function* () { |
| 42 | + const db = (yield* Database.Service).db |
| 43 | + const bus = yield* Bus.Service |
| 44 | + const store = yield* SessionStore.Service |
| 45 | + const sessions = yield* store.list({ parentID: null, order: "asc" }) |
| 46 | + const messages = [] as SessionMessage.Info[] |
| 47 | + let cursor: { id: SessionMessage.ID; direction: "next" } | undefined |
| 48 | + while (true) { |
| 49 | + const page = yield* store.messages({ sessionID, limit: 17, order: "asc", cursor }) |
| 50 | + if (page.length === 0) break |
| 51 | + messages.push(...page) |
| 52 | + cursor = { id: page.at(-1)!.id, direction: "next" } |
| 53 | + } |
| 54 | + const terminal = yield* store.messages({ sessionID, limit: 17, order: "asc", cursor }) |
| 55 | + const initialWatermark = yield* db |
| 56 | + .select({ seq: EventSequenceTable.seq }) |
| 57 | + .from(EventSequenceTable) |
| 58 | + .where(eq(EventSequenceTable.aggregate_id, sessionID)) |
| 59 | + .get() |
| 60 | + const maximumMessageSeq = yield* db |
| 61 | + .select({ seq: SessionMessageTable.seq }) |
| 62 | + .from(SessionMessageTable) |
| 63 | + .where(eq(SessionMessageTable.session_id, sessionID)) |
| 64 | + .orderBy(desc(SessionMessageTable.seq)) |
| 65 | + .all() |
| 66 | + const previousWatermark = initialWatermark!.seq |
| 67 | + |
| 68 | + const inboxID = SessionMessage.ID.make("msg_fixture_append_001") |
| 69 | + yield* bus.publish(SessionEvent.InboxEnqueued, { |
| 70 | + sessionID, |
| 71 | + inboxID, |
| 72 | + item: { type: "user", payload: { text: "Message appended" }, delivery: "steer" }, |
| 73 | + }) |
| 74 | + yield* bus.publish(SessionEvent.InboxDelivered, { sessionID, inboxID }) |
| 75 | + const appended = yield* store.messages({ sessionID, limit: 1, order: "desc" }) |
| 76 | + const afterDelivery = yield* db |
| 77 | + .select({ seq: EventSequenceTable.seq }) |
| 78 | + .from(EventSequenceTable) |
| 79 | + .where(eq(EventSequenceTable.aggregate_id, sessionID)) |
| 80 | + .get() |
| 81 | + yield* bus.publish(SessionEvent.Renamed, { sessionID, title: "Fixture session 001" }) |
| 82 | + const finalWatermark = yield* db |
| 83 | + .select({ seq: EventSequenceTable.seq }) |
| 84 | + .from(EventSequenceTable) |
| 85 | + .where(eq(EventSequenceTable.aggregate_id, sessionID)) |
| 86 | + .get() |
| 87 | + const finalMaximum = yield* db |
| 88 | + .select({ seq: SessionMessageTable.seq }) |
| 89 | + .from(SessionMessageTable) |
| 90 | + .where(and(eq(SessionMessageTable.session_id, sessionID), eq(SessionMessageTable.id, appended[0]!.id))) |
| 91 | + .get() |
| 92 | + |
| 93 | + return { |
| 94 | + sessions, |
| 95 | + messages, |
| 96 | + terminal, |
| 97 | + previousWatermark, |
| 98 | + maximumMessageSeq: maximumMessageSeq[0]!.seq, |
| 99 | + afterDelivery: afterDelivery!.seq, |
| 100 | + finalWatermark: finalWatermark!.seq, |
| 101 | + finalMaximumSeq: finalMaximum!.seq, |
| 102 | + appended, |
| 103 | + } |
| 104 | + }), |
| 105 | + layer, |
| 106 | + ), |
| 107 | + ), |
| 108 | + ) |
| 109 | + |
| 110 | + expect(report.sessions).toHaveLength(60) |
| 111 | + expect(report.sessions.every((session) => session.parentID === undefined)).toBe(true) |
| 112 | + expect(report.sessions.map((session) => session.title).toSorted()).toEqual( |
| 113 | + Array.from({ length: 60 }, (_, index) => `Fixture session ${String(index + 1).padStart(3, "0")}`), |
| 114 | + ) |
| 115 | + expect(report.messages).toHaveLength(250) |
| 116 | + expect(new Set(report.messages.map((message) => message.id)).size).toBe(250) |
| 117 | + expect(report.messages.map((message) => (message.type === "user" ? message.text : message.type))).toEqual( |
| 118 | + Array.from({ length: 250 }, (_, index) => `Message ${String(index + 1).padStart(3, "0")}`), |
| 119 | + ) |
| 120 | + expect(report.terminal).toEqual([]) |
| 121 | + expect(report.previousWatermark).toBeGreaterThanOrEqual(report.maximumMessageSeq) |
| 122 | + expect(report.afterDelivery).toBeGreaterThan(report.previousWatermark) |
| 123 | + expect(report.appended[0]?.type === "user" && report.appended[0].text).toBe("Message appended") |
| 124 | + expect(report.finalMaximumSeq).toBe(report.afterDelivery) |
| 125 | + expect(report.finalWatermark).toBeGreaterThan(report.finalMaximumSeq) |
| 126 | +}) |
| 127 | + |
| 128 | +test("fixture refuses an existing destination and SQLite sidecars without changing bytes", async () => { |
| 129 | + await using directory = await tmpdir("opencode-v2-pagination-refusal-") |
| 130 | + const existing = path.join(directory.path, "existing.sqlite") |
| 131 | + const existingBytes = Buffer.from("keep existing database bytes") |
| 132 | + await writeFile(existing, existingBytes) |
| 133 | + const existingResult = runScript(existing) |
| 134 | + expect(existingResult.status).not.toBe(0) |
| 135 | + expect(await readFile(existing)).toEqual(existingBytes) |
| 136 | + |
| 137 | + for (const suffix of ["-wal", "-shm"]) { |
| 138 | + const filename = path.join(directory.path, `sidecar${suffix}.sqlite`) |
| 139 | + const sidecar = `${filename}${suffix}` |
| 140 | + const sidecarBytes = Buffer.from(`keep ${suffix} bytes`) |
| 141 | + await writeFile(sidecar, sidecarBytes) |
| 142 | + const result = runScript(filename) |
| 143 | + expect(result.status).not.toBe(0) |
| 144 | + expect(await readFile(sidecar)).toEqual(sidecarBytes) |
| 145 | + } |
| 146 | +}) |
0 commit comments