localTtft.test.ts 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import { commandsQueryParamsSchema } from "../src/zcode-protocol-v4/command.js";
  2. import { describe, expect, it } from "vitest";
  3. import {
  4. localTtftContextSchema,
  5. localTtftFactsSchema,
  6. calibrateLocalTtftClock,
  7. } from "../src/localTtft.js";
  8. describe("本地 TTFT 协议与跨进程时钟边界", () => {
  9. it("只接受有界无正文观测上下文", () => {
  10. const context = { version: 1, observationId: "e0b15fc2-3a50-4d73-854d-61fa148cbfd0" };
  11. expect(localTtftContextSchema.safeParse(context).success).toBe(true);
  12. expect(localTtftContextSchema.safeParse({ ...context, prompt: "private" }).success).toBe(false);
  13. expect(
  14. localTtftFactsSchema.safeParse({ ...context, stages: Array.from({ length: 100 }, () => 0) })
  15. .success,
  16. ).toBe(false);
  17. });
  18. it("往返区间扣除服务端处理后给出 offset 与实测误差界限", () => {
  19. expect(
  20. calibrateLocalTtftClock(1000, 1014, { instanceId: "cli", receivedAt: 1202, sentAt: 1212 }),
  21. ).toEqual({ instanceId: "cli", offsetMs: -200, errorMs: 2, measuredAt: 1014 });
  22. expect(
  23. calibrateLocalTtftClock(1000, 1050, { instanceId: "cli", receivedAt: 1202, sentAt: 1203 }),
  24. ).toBeUndefined();
  25. expect(
  26. calibrateLocalTtftClock(1000, 1001, { instanceId: "cli", receivedAt: 1202, sentAt: 1212 }),
  27. ).toBeUndefined();
  28. });
  29. });
  30. it("时钟探测不能携带 session 命令查询", () => {
  31. expect(
  32. commandsQueryParamsSchema.safeParse({
  33. clock: true,
  34. commands: [{ sessionId: "session", commandId: "input" }],
  35. }).success,
  36. ).toBe(false);
  37. expect(
  38. commandsQueryParamsSchema.safeParse({
  39. clock: true,
  40. commands: [{ sessionId: null, commandId: "probe" }],
  41. }).success,
  42. ).toBe(true);
  43. });