botTopicHistory.test.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import { describe, expect, it, vi } from "vitest";
  2. import { readTopicHistory } from "../src/bots/topicHistory.js";
  3. const row = (id: string, overrides = {}) => ({
  4. id,
  5. chatId: "chat",
  6. threadId: "thread",
  7. senderId: "user",
  8. senderType: "user" as const,
  9. text: id,
  10. createdAt: 1,
  11. kind: "text",
  12. ...overrides,
  13. });
  14. const request = { chatId: "chat", threadId: "thread", messageId: "current", rootMessageId: "root" };
  15. describe("bounded topic history", () => {
  16. it("excludes bot cards and their bot confirmations without changing history boundaries", async () => {
  17. const result = await readTopicHistory(request, {
  18. list: async () => ({
  19. messages: [
  20. row("current"),
  21. row("confirmation", { senderType: "app", parentId: "card" }),
  22. row("human-quote", { parentId: "card" }),
  23. row("bot-text", { senderType: "app", kind: "interactive" }),
  24. row("card", { senderType: "app", kind: "interactive", controlCard: true }),
  25. row("root"),
  26. ],
  27. }),
  28. get: async () => row("root"),
  29. });
  30. expect(result.messages.map((message) => message.id)).toEqual([
  31. "root",
  32. "bot-text",
  33. "human-quote",
  34. ]);
  35. expect(result.checkpoint).toBe("current");
  36. expect(result.hasGap).toBe(false);
  37. });
  38. it("checks history access even when the root first invokes the bot", async () => {
  39. const list = vi.fn(async () => ({ messages: [] }));
  40. const result = await readTopicHistory(
  41. { ...request, messageId: "root" },
  42. { list, get: async () => row("root") },
  43. );
  44. expect(result).toEqual({ messages: [], checkpoint: "root", hasGap: false });
  45. expect(list).toHaveBeenCalledOnce();
  46. });
  47. it("freezes at the invoking message, removes overlapping pages and stops at the checkpoint", async () => {
  48. const list = vi
  49. .fn()
  50. .mockResolvedValueOnce({
  51. messages: [row("future"), row("current"), row("new")],
  52. next: "page2",
  53. })
  54. .mockResolvedValueOnce({
  55. messages: [row("new"), row("older"), row("checkpoint"), row("past")],
  56. });
  57. const result = await readTopicHistory(
  58. { ...request, checkpoint: "checkpoint" },
  59. {
  60. list,
  61. get: vi.fn(async () => row("root")),
  62. },
  63. );
  64. expect(result.messages.map((m) => m.id)).toEqual(["older", "new"]);
  65. expect(result.hasGap).toBe(false);
  66. expect(result.checkpoint).toBe("current");
  67. });
  68. it("keeps the root once on first invocation and includes bots but excludes deleted content", async () => {
  69. const result = await readTopicHistory(request, {
  70. list: async () => ({
  71. messages: [
  72. row("current"),
  73. row("bot", { senderType: "app" }),
  74. row("gone", { deleted: true }),
  75. row("old"),
  76. row("root"),
  77. ],
  78. }),
  79. get: async () => row("root"),
  80. });
  81. expect(result.messages.map((m) => m.id)).toEqual(["root", "old", "bot"]);
  82. expect(result.hasGap).toBe(true);
  83. });
  84. it("preserves an app-authored root once and keeps its sender type", async () => {
  85. const result = await readTopicHistory(request, {
  86. list: async () => ({ messages: [row("current"), row("root", { senderType: "app" })] }),
  87. get: async () => row("root", { senderType: "app" }),
  88. });
  89. expect(result.messages).toEqual([row("root", { senderType: "app" })]);
  90. });
  91. it("bounds large history and records the uncovered range", async () => {
  92. const result = await readTopicHistory(request, {
  93. list: async () => ({
  94. messages: [row("current"), ...Array.from({ length: 220 }, (_, i) => row(String(i)))],
  95. next: "more",
  96. }),
  97. get: async () => row("root"),
  98. });
  99. expect(result.messages).toHaveLength(201);
  100. expect(result.messages[0]?.id).toBe("root");
  101. expect(result.hasGap).toBe(true);
  102. });
  103. it("fails closed for cross-topic history and missing invocation boundary", async () => {
  104. await expect(
  105. readTopicHistory(request, {
  106. list: async () => ({ messages: [row("current"), row("secret", { threadId: "other" })] }),
  107. get: async () => row("root"),
  108. }),
  109. ).rejects.toThrow(/scope/);
  110. await expect(
  111. readTopicHistory(request, {
  112. list: async () => ({ messages: [row("future")] }),
  113. get: async () => row("root"),
  114. }),
  115. ).rejects.toThrow(/boundary/);
  116. });
  117. it("does not treat a missing checkpoint as complete incremental history", async () => {
  118. const result = await readTopicHistory(
  119. { ...request, checkpoint: "missing" },
  120. {
  121. list: async () => ({ messages: [row("current"), row("old")] }),
  122. get: async () => row("root"),
  123. },
  124. );
  125. expect(result.hasGap).toBe(true);
  126. });
  127. });