|
| 1 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | + |
| 3 | +const resolveFeishuRuntimeAccountMock = vi.hoisted(() => vi.fn()); |
| 4 | +const createFeishuClientMock = vi.hoisted(() => vi.fn()); |
| 5 | +const createReplyPrefixContextMock = vi.hoisted(() => vi.fn()); |
| 6 | +const createCommentTypingReactionLifecycleMock = vi.hoisted(() => vi.fn()); |
| 7 | +const deliverCommentThreadTextMock = vi.hoisted(() => vi.fn()); |
| 8 | +const createReplyDispatcherWithTypingMock = vi.hoisted(() => vi.fn()); |
| 9 | +const getFeishuRuntimeMock = vi.hoisted(() => vi.fn()); |
| 10 | + |
| 11 | +vi.mock("./accounts.js", () => ({ |
| 12 | + resolveFeishuRuntimeAccount: resolveFeishuRuntimeAccountMock, |
| 13 | +})); |
| 14 | + |
| 15 | +vi.mock("./client.js", () => ({ |
| 16 | + createFeishuClient: createFeishuClientMock, |
| 17 | +})); |
| 18 | + |
| 19 | +vi.mock("./comment-dispatcher-runtime-api.js", () => ({ |
| 20 | + createReplyPrefixContext: createReplyPrefixContextMock, |
| 21 | +})); |
| 22 | + |
| 23 | +vi.mock("./comment-reaction.js", () => ({ |
| 24 | + createCommentTypingReactionLifecycle: createCommentTypingReactionLifecycleMock, |
| 25 | +})); |
| 26 | + |
| 27 | +vi.mock("./drive.js", () => ({ |
| 28 | + deliverCommentThreadText: deliverCommentThreadTextMock, |
| 29 | +})); |
| 30 | + |
| 31 | +vi.mock("./runtime.js", () => ({ |
| 32 | + getFeishuRuntime: getFeishuRuntimeMock, |
| 33 | +})); |
| 34 | + |
| 35 | +import { createFeishuCommentReplyDispatcher } from "./comment-dispatcher.js"; |
| 36 | + |
| 37 | +describe("createFeishuCommentReplyDispatcher", () => { |
| 38 | + beforeEach(() => { |
| 39 | + vi.clearAllMocks(); |
| 40 | + resolveFeishuRuntimeAccountMock.mockReturnValue({ |
| 41 | + accountId: "main", |
| 42 | + appId: "app_id", |
| 43 | + appSecret: "app_secret", |
| 44 | + domain: "feishu", |
| 45 | + config: {}, |
| 46 | + }); |
| 47 | + createFeishuClientMock.mockReturnValue({}); |
| 48 | + createReplyPrefixContextMock.mockReturnValue({ |
| 49 | + responsePrefix: undefined, |
| 50 | + responsePrefixContextProvider: undefined, |
| 51 | + }); |
| 52 | + deliverCommentThreadTextMock.mockResolvedValue({ |
| 53 | + delivery_mode: "reply_comment", |
| 54 | + reply_id: "reply_1", |
| 55 | + }); |
| 56 | + createCommentTypingReactionLifecycleMock.mockReturnValue({ |
| 57 | + start: vi.fn(async () => {}), |
| 58 | + cleanup: vi.fn(async () => {}), |
| 59 | + }); |
| 60 | + createReplyDispatcherWithTypingMock.mockImplementation(() => ({ |
| 61 | + dispatcher: { |
| 62 | + markComplete: vi.fn(), |
| 63 | + waitForIdle: vi.fn(async () => {}), |
| 64 | + }, |
| 65 | + replyOptions: {}, |
| 66 | + markDispatchIdle: vi.fn(), |
| 67 | + markRunComplete: vi.fn(), |
| 68 | + })); |
| 69 | + getFeishuRuntimeMock.mockReturnValue({ |
| 70 | + channel: { |
| 71 | + text: { |
| 72 | + resolveTextChunkLimit: vi.fn(() => 4000), |
| 73 | + resolveChunkMode: vi.fn(() => "line"), |
| 74 | + chunkTextWithMode: vi.fn((text: string) => [text]), |
| 75 | + }, |
| 76 | + reply: { |
| 77 | + createReplyDispatcherWithTyping: createReplyDispatcherWithTypingMock, |
| 78 | + resolveHumanDelayConfig: vi.fn(() => undefined), |
| 79 | + }, |
| 80 | + }, |
| 81 | + }); |
| 82 | + }); |
| 83 | + |
| 84 | + it("sends final comment text without waiting for typing cleanup", async () => { |
| 85 | + let resolveCleanup: (() => void) | undefined; |
| 86 | + const cleanup = vi.fn( |
| 87 | + () => |
| 88 | + new Promise<void>((resolve) => { |
| 89 | + resolveCleanup = resolve; |
| 90 | + }), |
| 91 | + ); |
| 92 | + createCommentTypingReactionLifecycleMock.mockReturnValue({ |
| 93 | + start: vi.fn(async () => {}), |
| 94 | + cleanup, |
| 95 | + }); |
| 96 | + |
| 97 | + createFeishuCommentReplyDispatcher({ |
| 98 | + cfg: {} as never, |
| 99 | + agentId: "main", |
| 100 | + runtime: { log: vi.fn(), error: vi.fn() } as never, |
| 101 | + accountId: "main", |
| 102 | + fileToken: "doc_token_1", |
| 103 | + fileType: "docx", |
| 104 | + commentId: "comment_1", |
| 105 | + replyId: "reply_1", |
| 106 | + isWholeComment: false, |
| 107 | + }); |
| 108 | + |
| 109 | + const options = createReplyDispatcherWithTypingMock.mock.calls.at(-1)?.[0]; |
| 110 | + const deliverPromise = options.deliver({ text: "hello world" }, { kind: "final" }); |
| 111 | + const status = await Promise.race([ |
| 112 | + deliverPromise.then(() => "done"), |
| 113 | + new Promise<string>((resolve) => setTimeout(() => resolve("pending"), 0)), |
| 114 | + ]); |
| 115 | + |
| 116 | + expect(status).toBe("done"); |
| 117 | + expect(deliverCommentThreadTextMock).toHaveBeenCalledWith( |
| 118 | + expect.anything(), |
| 119 | + expect.objectContaining({ |
| 120 | + file_token: "doc_token_1", |
| 121 | + file_type: "docx", |
| 122 | + comment_id: "comment_1", |
| 123 | + content: "hello world", |
| 124 | + is_whole_comment: false, |
| 125 | + }), |
| 126 | + ); |
| 127 | + expect(cleanup).not.toHaveBeenCalled(); |
| 128 | + |
| 129 | + options.onCleanup?.(); |
| 130 | + expect(cleanup).toHaveBeenCalledTimes(1); |
| 131 | + |
| 132 | + resolveCleanup?.(); |
| 133 | + await deliverPromise; |
| 134 | + }); |
| 135 | + |
| 136 | + it("starts the typing reaction from dispatcher onReplyStart", async () => { |
| 137 | + const start = vi.fn(async () => {}); |
| 138 | + createCommentTypingReactionLifecycleMock.mockReturnValue({ |
| 139 | + start, |
| 140 | + cleanup: vi.fn(async () => {}), |
| 141 | + }); |
| 142 | + |
| 143 | + createFeishuCommentReplyDispatcher({ |
| 144 | + cfg: {} as never, |
| 145 | + agentId: "main", |
| 146 | + runtime: { log: vi.fn(), error: vi.fn() } as never, |
| 147 | + accountId: "main", |
| 148 | + fileToken: "doc_token_1", |
| 149 | + fileType: "docx", |
| 150 | + commentId: "comment_1", |
| 151 | + replyId: "reply_1", |
| 152 | + isWholeComment: false, |
| 153 | + }); |
| 154 | + |
| 155 | + const options = createReplyDispatcherWithTypingMock.mock.calls.at(-1)?.[0]; |
| 156 | + await options.onReplyStart?.(); |
| 157 | + |
| 158 | + expect(start).toHaveBeenCalledTimes(1); |
| 159 | + }); |
| 160 | +}); |
0 commit comments