contract-hygiene.test.ts 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import { describe, expect, test } from "bun:test"
  2. import { Schema } from "effect"
  3. import { Agent } from "../src/agent"
  4. import { FileSystem } from "../src/filesystem"
  5. import { Model } from "../src/model"
  6. import { Project } from "../src/project"
  7. import { Pty } from "../src/pty"
  8. import { Question } from "../src/question"
  9. import { Session } from "../src/session"
  10. import { SessionEvent } from "../src/session-event"
  11. import { SessionTodo } from "../src/session-todo"
  12. import { optional } from "../src/schema"
  13. describe("contract hygiene", () => {
  14. test("optional properties preserve transformations and omit undefined while encoding", () => {
  15. const Value = Schema.Struct({ value: optional(Schema.FiniteFromString) })
  16. expect(Schema.decodeUnknownSync(Value)({ value: "1" })).toEqual({ value: 1 })
  17. expect(Schema.encodeSync(Value)({ value: 1 })).toEqual({ value: "1" })
  18. expect(Schema.encodeSync(Value)({ value: undefined })).toEqual({})
  19. })
  20. test("todo status and priority preserve arbitrary strings", () => {
  21. const decode = Schema.decodeUnknownSync(SessionTodo.Info)
  22. expect(decode({ content: "ship", status: "waiting", priority: "urgent" })).toEqual({
  23. content: "ship",
  24. status: "waiting",
  25. priority: "urgent",
  26. })
  27. })
  28. test("current ID constructors expose create", () => {
  29. expect(Question.ID.create()).toStartWith("que_")
  30. expect(Pty.ID.create()).toStartWith("pty_")
  31. })
  32. test("reusable public identifiers are stable and unique", () => {
  33. const identifiers = [
  34. Agent.Color,
  35. FileSystem.Submatch,
  36. Model.Ref,
  37. Model.Capabilities,
  38. Model.Cost,
  39. Model.Api,
  40. Project.Icon,
  41. Project.Commands,
  42. Project.Time,
  43. Project.Info,
  44. Pty.Info,
  45. Session.ListAnchor,
  46. ].map((schema) => schema.ast.annotations?.identifier)
  47. expect(identifiers.every((identifier) => typeof identifier === "string")).toBe(true)
  48. expect(new Set(identifiers).size).toBe(identifiers.length)
  49. })
  50. test("current source avoids Any and mutable contract wrappers", async () => {
  51. const files = [...new Bun.Glob("*.ts").scanSync(new URL("../src", import.meta.url).pathname)].filter(
  52. (file) => !file.endsWith("-v1.ts"),
  53. )
  54. const source = await Promise.all(
  55. files.map((file) => Bun.file(new URL(`../src/${file}`, import.meta.url)).text()),
  56. ).then((values) => values.join("\n"))
  57. expect(source).not.toContain("Schema.Any")
  58. expect(source).not.toContain("Schema.mutable")
  59. })
  60. })