helpers.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import { expect } from "bun:test"
  2. import type { InitializeResponse, NewSessionResponse, SessionConfigOption } from "@agentclientprotocol/sdk"
  3. import { Effect } from "effect"
  4. import type { CliFixture } from "../../lib/cli-process"
  5. import { testProviderConfig } from "../../lib/test-provider"
  6. import {
  7. createAcpClient as createJsonRpcAcpClient,
  8. expectOk,
  9. flattenSelectOptions,
  10. selectConfigOption,
  11. type AcpClient,
  12. } from "./acp-test-client"
  13. export function createAcpClient(input: Pick<CliFixture, "kirincode">, env?: Record<string, string>) {
  14. return Effect.gen(function* () {
  15. return createJsonRpcAcpClient(yield* input.kirincode.acp(env ? { env } : undefined))
  16. })
  17. }
  18. export function initialize(acp: AcpClient) {
  19. return Effect.gen(function* () {
  20. return expectOk(
  21. yield* acp.request<InitializeResponse>("initialize", {
  22. protocolVersion: 1,
  23. clientCapabilities: { _meta: { "terminal-auth": true } },
  24. clientInfo: { name: "opencode-local-acp", version: "0.1.0" },
  25. }),
  26. )
  27. })
  28. }
  29. export function newSession(acp: AcpClient, cwd: string) {
  30. return Effect.gen(function* () {
  31. return expectOk(yield* acp.request<NewSessionResponse>("session/new", { cwd, mcpServers: [] }))
  32. })
  33. }
  34. export function verifierConfig(llmUrl: string, skills?: string) {
  35. const config = testProviderConfig(llmUrl)
  36. return {
  37. ...config,
  38. model: "test/test-model",
  39. ...(skills ? { skills: { paths: [skills] } } : {}),
  40. provider: {
  41. test: {
  42. ...config.provider.test,
  43. models: {
  44. "test-model": {
  45. ...config.provider.test.models["test-model"],
  46. variants: {
  47. low: {},
  48. high: {},
  49. },
  50. },
  51. "second-model": {
  52. ...config.provider.test.models["test-model"],
  53. id: "second-model",
  54. name: "Second Test Model",
  55. variants: {
  56. medium: {},
  57. max: {},
  58. },
  59. },
  60. },
  61. },
  62. },
  63. }
  64. }
  65. export function expectErrorCode(error: unknown, code: number) {
  66. if (!error || typeof error !== "object" || !("code" in error)) {
  67. expect(error).toEqual({ code })
  68. return
  69. }
  70. expect(error.code).toBe(code)
  71. }
  72. export function expectSelectOption(options: SessionConfigOption[] | null | undefined, id: string) {
  73. const option = selectConfigOption(options, id)
  74. expect(option).toBeDefined()
  75. return option!
  76. }
  77. export function expectAlternateValue(option: ReturnType<typeof expectSelectOption>) {
  78. const value = flattenSelectOptions(option).find((item) => item.value !== option.currentValue)?.value
  79. expect(value).toBeDefined()
  80. return value!
  81. }
  82. export const verifierSkill = `---
  83. name: verifier-skill
  84. description: Verifier compatibility skill.
  85. ---
  86. # Verifier Skill
  87. `