submit-result.test.ts 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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).toBeDefined();
  36. expect((resultSchema as Record<string, unknown>).type).toBeUndefined();
  37. expect(SubmitResultInputJsonSchema.additionalProperties).toBe(false);
  38. });
  39. it("only accepts the accepted-status output", () => {
  40. expect(SubmitResultOutputSchema.parse({ status: "accepted" })).toEqual({ status: "accepted" });
  41. expect(SubmitResultOutputSchema.safeParse({ status: "rejected" }).success).toBe(false);
  42. });
  43. // mono 子代理的 typed 声明(docs/execution-engine.md「Typed asks and `submit_result`」):外层与通用声明同形,
  44. // `result` 就是该 actor 唯一的 ask 结果 schema。
  45. it("builds the typed declaration around the ask schema, same outer shape as the generic one", () => {
  46. const resultSchema = {
  47. type: "object",
  48. properties: {
  49. title: { type: "string" },
  50. points: { type: "array", items: { type: "string" } },
  51. },
  52. required: ["title", "points"],
  53. additionalProperties: false,
  54. };
  55. const typed = typedSubmitResultInputSchema(resultSchema);
  56. expect(typed.type).toBe("object");
  57. expect(typed.required).toEqual(["result"]);
  58. expect(typed.additionalProperties).toBe(false);
  59. expect(typed.$schema).toBe(SubmitResultInputJsonSchema.$schema);
  60. const result = (typed.properties as Record<string, Record<string, unknown>>).result;
  61. expect(result).toMatchObject(resultSchema);
  62. expect(typeof result.description).toBe("string");
  63. });
  64. it("keeps the ask schema's own description when it has one", () => {
  65. const typed = typedSubmitResultInputSchema({ type: "string", description: "A verdict." });
  66. const result = (typed.properties as Record<string, Record<string, unknown>>).result;
  67. expect(result.description).toBe("A verdict.");
  68. });
  69. });