submit-result.test.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. SUBMIT_RESULT_TOOL_NAME,
  4. SubmitResultInputJsonSchema,
  5. SubmitResultInputSchema,
  6. SubmitResultOutputSchema,
  7. typedSubmitResultInputSchema,
  8. } from "../src/tools/submit-result.js";
  9. describe("submit_result contracts", () => {
  10. it("names the tool submit_result", () => {
  11. expect(SUBMIT_RESULT_TOOL_NAME).toBe("submit_result");
  12. });
  13. it("accepts arbitrary JSON as the generic result payload", () => {
  14. // 声明的输入有意通用:具体 per-ask schema 在 ask epilogue 里,不进工具声明。
  15. for (const result of [
  16. { any: "object", nested: { ok: true } },
  17. ["a", 1, null],
  18. "plain string",
  19. 42,
  20. true,
  21. null,
  22. ]) {
  23. const parsed = SubmitResultInputSchema.parse({ result });
  24. expect(parsed.result).toEqual(result);
  25. }
  26. });
  27. it("rejects unknown top-level properties (strict object)", () => {
  28. const parsed = SubmitResultInputSchema.safeParse({ result: {}, extra: 1 });
  29. expect(parsed.success).toBe(false);
  30. });
  31. it("exposes a permissive JSON schema for result and forbids extra keys", () => {
  32. expect(SubmitResultInputJsonSchema.type).toBe("object");
  33. // result 是无约束的任意 JSON:其子 schema 不带类型约束。
  34. const resultSchema = (SubmitResultInputJsonSchema.properties as Record<string, unknown>).result;
  35. expect((resultSchema as Record<string, unknown>).type).toBeUndefined();
  36. expect(SubmitResultInputJsonSchema.additionalProperties).toBe(false);
  37. });
  38. it("only accepts the accepted-status output", () => {
  39. expect(SubmitResultOutputSchema.parse({ status: "accepted" })).toEqual({ status: "accepted" });
  40. expect(SubmitResultOutputSchema.safeParse({ status: "rejected" }).success).toBe(false);
  41. });
  42. // mono 子代理的 typed 声明(docs/execution-engine.md「Typed asks and `submit_result`」):外层与通用声明同形,
  43. // `result` 就是该 actor 唯一的 ask 结果 schema。
  44. it("builds the typed declaration around the ask schema, same outer shape as the generic one", () => {
  45. const resultSchema = {
  46. type: "object",
  47. properties: {
  48. title: { type: "string" },
  49. points: { type: "array", items: { type: "string" } },
  50. },
  51. required: ["title", "points"],
  52. additionalProperties: false,
  53. };
  54. const typed = typedSubmitResultInputSchema(resultSchema);
  55. expect(typed.type).toBe("object");
  56. expect(typed.required).toEqual(["result"]);
  57. expect(typed.additionalProperties).toBe(false);
  58. expect(typed.$schema).toBe(SubmitResultInputJsonSchema.$schema);
  59. const result = (typed.properties as Record<string, Record<string, unknown>>).result;
  60. expect(result).toMatchObject(resultSchema);
  61. expect(typeof result.description).toBe("string");
  62. });
  63. it("keeps the ask schema's own description when it has one", () => {
  64. const typed = typedSubmitResultInputSchema({ type: "string", description: "A verdict." });
  65. const result = (typed.properties as Record<string, Record<string, unknown>>).result;
  66. expect(result.description).toBe("A verdict.");
  67. });
  68. });