Files
opencode/packages/schema/test/contract-hygiene.test.ts
2026-07-06 18:14:06 -04:00

107 lines
3.8 KiB
TypeScript

import { describe, expect, test } from "bun:test"
import { DateTime, Schema } from "effect"
import { Agent } from "../src/agent.js"
import { FileSystem } from "../src/filesystem.js"
import { Model } from "../src/model.js"
import { Project } from "../src/project.js"
import { Provider } from "../src/provider.js"
import { Pty } from "../src/pty.js"
import { Question } from "../src/question.js"
import { Session } from "../src/session.js"
import { SessionMessage } from "../src/session-message.js"
import { SessionTodo } from "../src/session-todo.js"
import { optional } from "../src/schema.js"
describe("contract hygiene", () => {
test("optional properties preserve transformations and omit undefined while encoding", () => {
const Value = Schema.Struct({ value: optional(Schema.FiniteFromString) })
expect(Schema.decodeUnknownSync(Value)({ value: "1" })).toEqual({ value: 1 })
expect(Schema.encodeSync(Value)({ value: 1 })).toEqual({ value: "1" })
expect(Schema.encodeSync(Value)({ value: undefined })).toEqual({})
})
test("model defaults and provider overlays preserve public invariants", () => {
const id = Model.ID.make("model")
expect(Model.Info.empty(Provider.ID.make("provider"), id)).toMatchObject({ modelID: id, variants: [] })
expect(() =>
Schema.decodeUnknownSync(Provider.Info)({
id: "provider",
name: "Provider",
package: "native",
settings: { invalid: 1n },
}),
).toThrow()
})
test("todo status and priority preserve arbitrary strings", () => {
const decode = Schema.decodeUnknownSync(SessionTodo.Info)
expect(decode({ content: "ship", status: "waiting", priority: "urgent" })).toEqual({
content: "ship",
status: "waiting",
priority: "urgent",
})
})
test("current ID constructors expose create", () => {
expect(Question.ID.create()).toStartWith("que_")
expect(Pty.ID.create()).toStartWith("pty_")
})
test("reusable public identifiers are stable and unique", () => {
const identifiers = [
Agent.Color,
FileSystem.Submatch,
Model.Ref,
Model.Capabilities,
Model.Cost,
Model.Variant,
Project.Current,
Project.Directory,
Project.DirectoriesInput,
Project.Directories,
Project.Icon,
Project.Commands,
Project.Time,
Project.Info,
Pty.Info,
Session.ListAnchor,
].map((schema) => schema.ast.annotations?.identifier)
expect(identifiers.every((identifier) => typeof identifier === "string")).toBe(true)
expect(new Set(identifiers).size).toBe(identifiers.length)
})
test("current source avoids Any and mutable contract wrappers", async () => {
const files = [...new Bun.Glob("*.ts").scanSync(new URL("../src", import.meta.url).pathname)].filter(
(file) => !file.endsWith("-v1.ts"),
)
const source = await Promise.all(
files.map((file) => Bun.file(new URL(`../src/${file}`, import.meta.url)).text()),
).then((values) => values.join("\n"))
expect(source).not.toContain("Schema.Any")
expect(source).not.toContain("Schema.mutable")
})
test("assistant content keeps only domain identities", () => {
expect(SessionMessage.AssistantText.make({ type: "text", text: "hello" })).toEqual({
type: "text",
text: "hello",
})
expect(
SessionMessage.AssistantReasoning.make({ type: "reasoning", text: "thinking", state: { id: "opaque" } }),
).toEqual({ type: "reasoning", text: "thinking", state: { id: "opaque" } })
expect(
SessionMessage.AssistantTool.make({
type: "tool",
id: "call_1",
name: "search",
executed: true,
providerState: { itemId: "item_1" },
state: { status: "pending", input: "" },
time: { created: DateTime.makeUnsafe(0) },
}),
).not.toHaveProperty("provider")
})
})