contract-hygiene.test.ts 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. import { describe, expect, test } from "bun:test"
  2. import { DateTime, Schema } from "effect"
  3. import { Agent } from "../src/agent.js"
  4. import { FileSystem } from "../src/filesystem.js"
  5. import { Mcp } from "../src/mcp.js"
  6. import { Model } from "../src/model.js"
  7. import { Project } from "../src/project.js"
  8. import { Provider } from "../src/provider.js"
  9. import { Pty } from "../src/pty.js"
  10. import { Question } from "../src/question.js"
  11. import { Session } from "../src/session.js"
  12. import { SessionMessage } from "../src/session-message.js"
  13. import { SessionInput } from "../src/session-input.js"
  14. import { FileDiff } from "../src/file-diff.js"
  15. import { Money } from "../src/money.js"
  16. import { Skill } from "../src/skill.js"
  17. import { Shell } from "../src/shell.js"
  18. import { PersistedRevert } from "../src/session-revert.js"
  19. import { SessionTodo } from "../src/session-todo.js"
  20. import { optional } from "../src/schema.js"
  21. describe("contract hygiene", () => {
  22. test("keeps absolute costs distinct from model rates", () => {
  23. const usd = Money.USD.make(1)
  24. const rate = Money.USDPerMillionTokens.make(1)
  25. // @ts-expect-error Model rates are not absolute costs.
  26. const invalidUSD: Money.USD = rate
  27. // @ts-expect-error Absolute costs are not model rates.
  28. const invalidRate: Money.USDPerMillionTokens = usd
  29. expect(invalidUSD).toBe(Money.USD.make(1))
  30. expect(invalidRate).toBe(Money.USDPerMillionTokens.make(1))
  31. expect(Money.USD.zero).toBe(Money.USD.make(0))
  32. expect(Money.USDPerMillionTokens.zero).toBe(Money.USDPerMillionTokens.make(0))
  33. })
  34. test("optional properties preserve transformations and omit undefined while encoding", () => {
  35. const Value = Schema.Struct({ value: optional(Schema.FiniteFromString) })
  36. expect(Schema.decodeUnknownSync(Value)({ value: "1" })).toEqual({ value: 1 })
  37. expect(Schema.encodeSync(Value)({ value: 1 })).toEqual({ value: "1" })
  38. expect(Schema.encodeSync(Value)({ value: undefined })).toEqual({})
  39. })
  40. test("model defaults and provider overlays preserve public invariants", () => {
  41. const id = Model.ID.make("model")
  42. expect(Model.Info.empty(Provider.ID.make("provider"), id)).toMatchObject({ modelID: id, variants: [] })
  43. expect(() =>
  44. Schema.decodeUnknownSync(Provider.Info)({
  45. id: "provider",
  46. name: "Provider",
  47. package: "native",
  48. settings: { invalid: 1n },
  49. }),
  50. ).toThrow()
  51. })
  52. test("todo status and priority preserve arbitrary strings", () => {
  53. const decode = Schema.decodeUnknownSync(SessionTodo.Info)
  54. expect(decode({ content: "ship", status: "waiting", priority: "urgent" })).toEqual({
  55. content: "ship",
  56. status: "waiting",
  57. priority: "urgent",
  58. })
  59. })
  60. test("current ID constructors expose create", () => {
  61. expect(Question.ID.create()).toStartWith("que_")
  62. expect(Pty.ID.create()).toStartWith("pty_")
  63. })
  64. test("reusable public identifiers are stable and unique", () => {
  65. const identifiers = [
  66. Agent.Color,
  67. FileSystem.Submatch,
  68. Mcp.Resource,
  69. Mcp.ResourceTemplate,
  70. Mcp.ResourceCatalog,
  71. Mcp.ResourceContentPart,
  72. Mcp.ResourceContent,
  73. Model.Ref,
  74. Model.Capabilities,
  75. Model.Cost,
  76. Model.Variant,
  77. Project.Current,
  78. Project.Directory,
  79. Project.DirectoriesInput,
  80. Project.Directories,
  81. Project.Icon,
  82. Project.Commands,
  83. Project.Time,
  84. Project.Info,
  85. Pty.Info,
  86. Session.ListAnchor,
  87. Session.Revert,
  88. ].map((schema) => schema.ast.annotations?.identifier)
  89. expect(identifiers.every((identifier) => typeof identifier === "string")).toBe(true)
  90. expect(new Set(identifiers).size).toBe(identifiers.length)
  91. })
  92. test("current source avoids Any and mutable contract wrappers", async () => {
  93. const files = [...new Bun.Glob("*.ts").scanSync(new URL("../src", import.meta.url).pathname)].filter(
  94. (file) => !file.endsWith("-v1.ts"),
  95. )
  96. const source = await Promise.all(
  97. files.map((file) => Bun.file(new URL(`../src/${file}`, import.meta.url)).text()),
  98. ).then((values) => values.join("\n"))
  99. expect(source).not.toContain("Schema.Any")
  100. expect(source).not.toContain("Schema.mutable")
  101. })
  102. test("assistant content keeps only domain identities", () => {
  103. expect(SessionMessage.AssistantText.make({ type: "text", text: "hello" })).toEqual({
  104. type: "text",
  105. text: "hello",
  106. })
  107. expect(
  108. SessionMessage.AssistantReasoning.make({ type: "reasoning", text: "thinking", state: { id: "opaque" } }),
  109. ).toEqual({ type: "reasoning", text: "thinking", state: { id: "opaque" } })
  110. expect(
  111. SessionMessage.AssistantTool.make({
  112. type: "tool",
  113. id: "call_1",
  114. name: "search",
  115. executed: true,
  116. providerState: { itemId: "item_1" },
  117. state: { status: "streaming", input: "" },
  118. time: { created: DateTime.makeUnsafe(0) },
  119. }),
  120. ).not.toHaveProperty("provider")
  121. })
  122. test("reviewed session contracts use their canonical current shapes", () => {
  123. expect(SessionMessage.Info.ast.annotations?.identifier).toBe("Session.Message.Info")
  124. expect(SessionInput.Info.ast.annotations?.identifier).toBe("SessionInput.Info")
  125. expect(Money.USD).not.toBe(Money.USDPerMillionTokens)
  126. expect(
  127. FileDiff.Info.make({ file: "src/index.ts", patch: "@@", additions: 1, deletions: 0, status: "modified" }),
  128. ).toEqual({ file: "src/index.ts", patch: "@@", additions: 1, deletions: 0, status: "modified" })
  129. expect(
  130. SessionMessage.Shell.make({
  131. id: SessionMessage.ID.make("msg_shell"),
  132. type: "shell",
  133. shellID: Shell.ID.make("sh_test"),
  134. command: "pwd",
  135. status: "exited",
  136. exit: 0,
  137. time: { created: DateTime.makeUnsafe(0) },
  138. }),
  139. ).not.toHaveProperty("shell")
  140. expect(
  141. SessionMessage.Skill.make({
  142. id: SessionMessage.ID.make("msg_skill"),
  143. type: "skill",
  144. skill: Skill.ID.make("effect"),
  145. name: Skill.Name.make("Effect"),
  146. text: "Use Effect",
  147. time: { created: DateTime.makeUnsafe(0) },
  148. }),
  149. ).toMatchObject({ skill: "effect", name: "Effect" })
  150. expect(
  151. SessionMessage.CompactionFailed.make({
  152. id: SessionMessage.ID.make("msg_compaction"),
  153. type: "compaction",
  154. status: "failed",
  155. reason: "manual",
  156. error: { type: "compaction.failed", message: "failed" },
  157. time: { created: DateTime.makeUnsafe(0) },
  158. }),
  159. ).not.toHaveProperty("summary")
  160. })
  161. test("keeps shared persisted revert compatibility", () => {
  162. expect(
  163. Schema.decodeUnknownSync(Session.Revert)({
  164. messageID: "msg_legacy",
  165. snapshot: "tree",
  166. diff: "legacy patch",
  167. }),
  168. ).not.toHaveProperty("diff")
  169. const revert = Schema.decodeUnknownSync(PersistedRevert)({
  170. messageID: "msg_legacy",
  171. snapshot: "tree",
  172. diff: "legacy patch",
  173. files: [{ path: "src/index.ts", status: "modified", additions: 1, deletions: 0, patch: "@@" }],
  174. })
  175. expect(String(revert.messageID)).toBe("msg_legacy")
  176. expect(String(revert.snapshot)).toBe("tree")
  177. expect(revert.files).toEqual([
  178. { file: "src/index.ts", status: "modified", additions: 1, deletions: 0, patch: "@@" },
  179. ])
  180. expect(Schema.encodeSync(PersistedRevert)(revert)).toEqual({
  181. messageID: "msg_legacy",
  182. snapshot: "tree",
  183. files: [{ file: "src/index.ts", status: "modified", additions: 1, deletions: 0, patch: "@@" }],
  184. })
  185. })
  186. })