hostSessionCreateTelemetry.test.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { describe, expect, it, vi } from "vitest";
  2. import { hostResponseMessageSchema, buildRemoteWorkspaceIdentity } from "@zcode/shared";
  3. import { reportHostSessionCreate } from "../src/host/hostSessionCreateTelemetry.js";
  4. describe("Host session_create 转发合同", () => {
  5. it.each(["automation_idle", "automation_scheduled"] as const)(
  6. "%s 带远端类型,隐藏路径",
  7. (source) => {
  8. const port = { postMessage: vi.fn() };
  9. reportHostSessionCreate(port, {
  10. sessionId: "s1",
  11. messageId: "first-command",
  12. source,
  13. workspaceIdentity: buildRemoteWorkspaceIdentity("/private", {
  14. kind: "ssh",
  15. host: "private-host",
  16. username: "private-user",
  17. }),
  18. });
  19. const message = port.postMessage.mock.calls[0]![0];
  20. expect(hostResponseMessageSchema.safeParse(message).success).toBe(true);
  21. expect(message).toMatchObject({
  22. type: "session-create-telemetry",
  23. event: {
  24. talkId: "s1",
  25. messageId: "first-command",
  26. context: { screenResolution: "" },
  27. eventExtraDetail: {
  28. create_source: source,
  29. client_kind: "desktop",
  30. workspace_kind: "remote",
  31. remote_kind: "ssh",
  32. },
  33. },
  34. });
  35. expect(JSON.stringify(message)).not.toContain("private");
  36. expect(
  37. hostResponseMessageSchema.safeParse({
  38. ...message,
  39. event: { ...message.event, userId: "spoof" },
  40. }).success,
  41. ).toBe(false);
  42. },
  43. );
  44. it("IPC 已关闭不改变派发成功", () => {
  45. expect(() =>
  46. reportHostSessionCreate(
  47. {
  48. postMessage: () => {
  49. throw new Error("closed");
  50. },
  51. },
  52. { sessionId: "s1", messageId: "first-command", source: "automation_idle" },
  53. ),
  54. ).not.toThrow();
  55. });
  56. });