command.test.ts 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import { describe, expect } from "bun:test"
  2. import { Effect } from "effect"
  3. import { Command } from "@opencode-ai/core/command"
  4. import { Config } from "@opencode-ai/core/config"
  5. import { AppNodeBuilder } from "@opencode-ai/core/effect/app-node-builder"
  6. import { Location } from "@opencode-ai/core/location"
  7. import { MCP } from "@opencode-ai/core/mcp/index"
  8. import { Model } from "@opencode-ai/core/model"
  9. import { Provider } from "@opencode-ai/core/provider"
  10. import { emptyConfigLayer, emptyMcpLayer, testLocationLayer } from "./fixture/mcp"
  11. import { testEffect } from "./lib/effect"
  12. const it = testEffect(
  13. AppNodeBuilder.build(Command.node, [
  14. [MCP.node, emptyMcpLayer],
  15. [Config.node, emptyConfigLayer],
  16. [Location.node, testLocationLayer],
  17. ]),
  18. )
  19. describe("Command", () => {
  20. it.effect("applies command transforms and preserves later overrides", () =>
  21. Effect.gen(function* () {
  22. const command = yield* Command.Service
  23. yield* command.transform((editor) => {
  24. editor.update("review", (command) => {
  25. command.template = "First"
  26. command.description = "Review code"
  27. })
  28. editor.update("review", (command) => {
  29. command.template = "Second"
  30. command.model = {
  31. id: Model.ID.make("claude"),
  32. providerID: Provider.ID.make("anthropic"),
  33. variant: Model.VariantID.make("high"),
  34. }
  35. })
  36. })
  37. expect(yield* command.get("review")).toEqual(
  38. Command.Info.make({
  39. name: "review",
  40. template: "Second",
  41. description: "Review code",
  42. model: {
  43. id: Model.ID.make("claude"),
  44. providerID: Provider.ID.make("anthropic"),
  45. variant: Model.VariantID.make("high"),
  46. },
  47. }),
  48. )
  49. expect(yield* command.list()).toEqual([
  50. Command.Info.make({
  51. name: "review",
  52. template: "Second",
  53. description: "Review code",
  54. model: {
  55. id: Model.ID.make("claude"),
  56. providerID: Provider.ID.make("anthropic"),
  57. variant: Model.VariantID.make("high"),
  58. },
  59. }),
  60. ])
  61. }),
  62. )
  63. it.effect("evaluates command template shell blocks", () =>
  64. Effect.gen(function* () {
  65. const command = yield* Command.Service
  66. yield* command.transform((editor) => {
  67. editor.update("review", (command) => {
  68. command.template = "Output: !`echo command-output`"
  69. })
  70. })
  71. expect((yield* command.evaluate({ name: "review" })).text.replace(/\r?\n$/, "")).toEqual("Output: command-output")
  72. }),
  73. )
  74. })