remote-input-origin.test.ts 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. inputIntentMetadata,
  4. inputIntentMetadataFromCanonical,
  5. inputIntentMetadataFromQueueItem,
  6. } from "../src/zcode-protocol-v4/commands/input-intent.js";
  7. import type { CommandEnvelope, QueueItem } from "@zcode/shared/zcode-protocol-v4";
  8. describe("扫码输入来源贯穿 admission", () => {
  9. it.each(["createSession", "sendText", "sendGoalCommand", "editUserQuery", "retryTurn"] as const)(
  10. "%s uses trusted envelope origin",
  11. (type) => {
  12. const envelope: CommandEnvelope = {
  13. commandId: "new",
  14. clientId: "mobile",
  15. sessionId: "talk",
  16. type,
  17. inputOrigin: "mobile",
  18. payload: {},
  19. issuedAt: 1,
  20. };
  21. expect(
  22. inputIntentMetadata(envelope, {
  23. text: "text",
  24. inputOrigin: "desktop",
  25. requestedDelivery: "queue",
  26. }).inputOrigin,
  27. ).toBe("mobile");
  28. expect(
  29. inputIntentMetadataFromCanonical(envelope, {
  30. kind: "sendText",
  31. text: "old",
  32. inputOrigin: "desktop",
  33. }).inputOrigin,
  34. ).toBe("mobile");
  35. expect(
  36. inputIntentMetadataFromCanonical(
  37. { ...envelope, inputOrigin: "desktop" },
  38. { kind: "sendText", text: "old", inputOrigin: "mobile" },
  39. ).inputOrigin,
  40. ).toBe("desktop");
  41. },
  42. );
  43. it("queue promotion preserves original mobile origin and source command", () => {
  44. const item = {
  45. inputOrigin: "mobile",
  46. sourceCommandId: "original",
  47. queueItemId: "queue",
  48. clientId: "mobile",
  49. kind: "sendText",
  50. order: { admissionSeq: 1 },
  51. admittedAt: 1,
  52. delivery: { requested: "queue", admitted: "queue" },
  53. } as QueueItem;
  54. expect(inputIntentMetadataFromQueueItem(item, "text")).toMatchObject({
  55. inputOrigin: "mobile",
  56. sourceCommandId: "original",
  57. });
  58. });
  59. });