errors.test.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. isOpenCodeRuntimeCrashError,
  4. isZCodeFileLockTimeoutError,
  5. normalizeUnknownError,
  6. ZCODE_FILE_LOCK_TIMEOUT_ERROR_CODE,
  7. } from "../src/errors.js";
  8. describe("normalizeUnknownError", () => {
  9. it("extracts code and message from plain json-rpc errors", () => {
  10. expect(
  11. normalizeUnknownError({
  12. code: -32603,
  13. message: "Internal error: You've hit your limit · resets 9pm (Asia/Shanghai)",
  14. }),
  15. ).toEqual({
  16. code: "-32603",
  17. message: "Internal error: You've hit your limit · resets 9pm (Asia/Shanghai)",
  18. });
  19. });
  20. it("unwraps nested error envelopes", () => {
  21. expect(
  22. normalizeUnknownError({
  23. jsonrpc: "2.0",
  24. error: {
  25. code: -32603,
  26. message: "Internal error: You've hit your limit · resets 9pm (Asia/Shanghai)",
  27. },
  28. }),
  29. ).toEqual({
  30. code: "-32603",
  31. message: "Internal error: You've hit your limit · resets 9pm (Asia/Shanghai)",
  32. });
  33. });
  34. });
  35. describe("isOpenCodeRuntimeCrashError", () => {
  36. it("matches Bun crash signatures", () => {
  37. expect(isOpenCodeRuntimeCrashError("oh no: Bun has crashed. Segmentation fault")).toBe(true);
  38. expect(isOpenCodeRuntimeCrashError("[ZCode Agent] opencode runtime 崩溃 (code=3221226505)")).toBe(true);
  39. });
  40. it("does not match generic connection failures", () => {
  41. expect(isOpenCodeRuntimeCrashError("ZCode Agent connection closed")).toBe(false);
  42. });
  43. });
  44. describe("isZCodeFileLockTimeoutError", () => {
  45. it("matches direct and RPC-wrapped timeout codes", () => {
  46. expect(isZCodeFileLockTimeoutError({ code: ZCODE_FILE_LOCK_TIMEOUT_ERROR_CODE })).toBe(true);
  47. expect(
  48. isZCodeFileLockTimeoutError({
  49. error: { code: ZCODE_FILE_LOCK_TIMEOUT_ERROR_CODE, message: "lock timeout" },
  50. }),
  51. ).toBe(true);
  52. });
  53. it("does not classify legacy EEXIST as the explicit timeout code", () => {
  54. expect(isZCodeFileLockTimeoutError({ code: "EEXIST" })).toBe(false);
  55. });
  56. });