botsGroupSessions.test.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import { describe, expect, it } from "vitest";
  2. import { botsStateFileSchema, botGroupInputSourceSchema, type BotActor } from "@zcode/shared";
  3. import {
  4. getBotConversationKey,
  5. getBotStateKey,
  6. isGroupOwnerCommand,
  7. canAnswerGroupQuestion,
  8. } from "../src/bots/groupSessions.js";
  9. const actor: BotActor = {
  10. botId: "bot",
  11. provider: "feishu",
  12. chatType: "group",
  13. chatId: "oc_a",
  14. providerUserId: "ou_a",
  15. };
  16. describe("group conversation authority", () => {
  17. it("preserves a bounded input background snapshot and rejects cross-topic content", () => {
  18. const source = {
  19. botId: "bot",
  20. provider: "feishu",
  21. chatId: "chat",
  22. threadId: "thread",
  23. senderId: "user",
  24. senderName: "User",
  25. messageId: "current",
  26. topicContext: {
  27. checkpoint: "current",
  28. hasGap: false,
  29. messages: [
  30. {
  31. id: "old",
  32. chatId: "chat",
  33. threadId: "thread",
  34. senderId: "other",
  35. senderType: "user",
  36. text: "background",
  37. createdAt: 1,
  38. kind: "text",
  39. },
  40. ],
  41. },
  42. };
  43. expect(botGroupInputSourceSchema.parse(source)).toEqual(source);
  44. source.topicContext.messages[0]!.threadId = "another";
  45. expect(botGroupInputSourceSchema.safeParse(source).success).toBe(false);
  46. });
  47. it("isolates topics while preserving the existing default/private keys", () => {
  48. const topic = { ...actor, threadId: "omt_a" };
  49. expect(getBotConversationKey(topic)).toBe(JSON.stringify(["bot", "oc_a", "omt_a"]));
  50. expect(getBotConversationKey(topic)).not.toBe(getBotConversationKey(actor));
  51. expect(getBotConversationKey(topic)).not.toBe(
  52. getBotConversationKey({ ...topic, threadId: "omt_b" }),
  53. );
  54. expect(getBotConversationKey({ ...topic, providerUserId: "other" })).toBe(
  55. getBotConversationKey(topic),
  56. );
  57. expect(getBotConversationKey(actor)).toBe(JSON.stringify(["bot", "oc_a"]));
  58. expect(getBotConversationKey({ ...topic, chatType: "private" })).toBe("bot");
  59. });
  60. it("round trips native topic identity and keys persisted state by topic", () => {
  61. const state = {
  62. botId: "bot",
  63. workspacePath: "/work",
  64. mode: "draft",
  65. activeTaskId: null,
  66. updatedAt: 1,
  67. group: {
  68. chatId: "oc_a",
  69. threadId: "omt_a",
  70. rootMessageId: "om_root",
  71. topicTitle: "Design",
  72. name: "研发",
  73. ownerId: "owner",
  74. enabled: true,
  75. taskIds: [],
  76. currentOptions: {},
  77. historyEnabled: false,
  78. },
  79. };
  80. const parsed = botsStateFileSchema.parse({ version: 3, bots: { topic: state } }).bots.topic!;
  81. expect(parsed).toEqual(state);
  82. expect(getBotStateKey(parsed)).toBe(JSON.stringify(["bot", "oc_a", "omt_a"]));
  83. });
  84. it("shares a context between members, not between chats or bots", () => {
  85. expect(getBotConversationKey(actor)).toBe(
  86. getBotConversationKey({ ...actor, providerUserId: "ou_b" }),
  87. );
  88. expect(getBotConversationKey(actor)).not.toBe(
  89. getBotConversationKey({ ...actor, chatId: "oc_b" }),
  90. );
  91. expect(getBotConversationKey(actor)).not.toBe(
  92. getBotConversationKey({ ...actor, botId: "other" }),
  93. );
  94. expect(getBotConversationKey({ ...actor, chatType: "private", chatId: undefined })).toBe("bot");
  95. expect(() => getBotConversationKey({ ...actor, chatId: undefined })).toThrow();
  96. });
  97. it("requires owner for configuration and approvals, not prompts/status", () => {
  98. for (const command of ["new", "workspace", "mode", "approve", "stop", "task", "reply"])
  99. expect(isGroupOwnerCommand(command)).toBe(true);
  100. for (const command of ["help", "status", "message", "answer"])
  101. expect(isGroupOwnerCommand(command)).toBe(false);
  102. expect(canAnswerGroupQuestion("ou_a", "owner", "ou_a", false)).toBe(true);
  103. expect(canAnswerGroupQuestion("stranger", "owner", "ou_a", false)).toBe(false);
  104. expect(canAnswerGroupQuestion("ou_a", "owner", "ou_a", true)).toBe(false);
  105. expect(canAnswerGroupQuestion("owner", "owner", "ou_a", true)).toBe(true);
  106. });
  107. it("persists group identity without changing private v3 records", () => {
  108. const state = {
  109. botId: "bot",
  110. workspacePath: "/work",
  111. mode: "draft",
  112. activeTaskId: null,
  113. updatedAt: 1,
  114. };
  115. expect(botsStateFileSchema.parse({ version: 3, bots: { bot: state } }).bots.bot).toEqual(state);
  116. const group = {
  117. ...state,
  118. group: {
  119. chatId: "oc_a",
  120. name: "研发",
  121. ownerId: "owner",
  122. enabled: true,
  123. taskIds: [],
  124. currentOptions: {},
  125. },
  126. };
  127. expect(botsStateFileSchema.parse({ version: 3, bots: { group } }).bots.group).toEqual(group);
  128. });
  129. });