|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | + |
| 3 | +const createFeishuClientMock = vi.hoisted(() => vi.fn()); |
| 4 | + |
| 5 | +vi.mock("./client.js", () => ({ |
| 6 | + createFeishuClient: createFeishuClientMock, |
| 7 | +})); |
| 8 | + |
| 9 | +import { probeFeishu, clearProbeCache } from "./probe.js"; |
| 10 | + |
| 11 | +function makeRequestFn(response: Record<string, unknown>) { |
| 12 | + return vi.fn().mockResolvedValue(response); |
| 13 | +} |
| 14 | + |
| 15 | +function setupClient(response: Record<string, unknown>) { |
| 16 | + const requestFn = makeRequestFn(response); |
| 17 | + createFeishuClientMock.mockReturnValue({ request: requestFn }); |
| 18 | + return requestFn; |
| 19 | +} |
| 20 | + |
| 21 | +describe("probeFeishu", () => { |
| 22 | + beforeEach(() => { |
| 23 | + clearProbeCache(); |
| 24 | + vi.restoreAllMocks(); |
| 25 | + }); |
| 26 | + |
| 27 | + afterEach(() => { |
| 28 | + clearProbeCache(); |
| 29 | + }); |
| 30 | + |
| 31 | + it("returns error when credentials are missing", async () => { |
| 32 | + const result = await probeFeishu(); |
| 33 | + expect(result).toEqual({ ok: false, error: "missing credentials (appId, appSecret)" }); |
| 34 | + }); |
| 35 | + |
| 36 | + it("returns error when appId is missing", async () => { |
| 37 | + const result = await probeFeishu({ appSecret: "secret" } as never); |
| 38 | + expect(result).toEqual({ ok: false, error: "missing credentials (appId, appSecret)" }); |
| 39 | + }); |
| 40 | + |
| 41 | + it("returns error when appSecret is missing", async () => { |
| 42 | + const result = await probeFeishu({ appId: "cli_123" } as never); |
| 43 | + expect(result).toEqual({ ok: false, error: "missing credentials (appId, appSecret)" }); |
| 44 | + }); |
| 45 | + |
| 46 | + it("returns bot info on successful probe", async () => { |
| 47 | + const requestFn = setupClient({ |
| 48 | + code: 0, |
| 49 | + bot: { bot_name: "TestBot", open_id: "ou_abc123" }, |
| 50 | + }); |
| 51 | + |
| 52 | + const result = await probeFeishu({ appId: "cli_123", appSecret: "secret" }); |
| 53 | + expect(result).toEqual({ |
| 54 | + ok: true, |
| 55 | + appId: "cli_123", |
| 56 | + botName: "TestBot", |
| 57 | + botOpenId: "ou_abc123", |
| 58 | + }); |
| 59 | + expect(requestFn).toHaveBeenCalledTimes(1); |
| 60 | + }); |
| 61 | + |
| 62 | + it("returns cached result on subsequent calls within TTL", async () => { |
| 63 | + const requestFn = setupClient({ |
| 64 | + code: 0, |
| 65 | + bot: { bot_name: "TestBot", open_id: "ou_abc123" }, |
| 66 | + }); |
| 67 | + |
| 68 | + const creds = { appId: "cli_123", appSecret: "secret" }; |
| 69 | + const first = await probeFeishu(creds); |
| 70 | + const second = await probeFeishu(creds); |
| 71 | + |
| 72 | + expect(first).toEqual(second); |
| 73 | + // Only one API call should have been made |
| 74 | + expect(requestFn).toHaveBeenCalledTimes(1); |
| 75 | + }); |
| 76 | + |
| 77 | + it("makes a fresh API call after cache expires", async () => { |
| 78 | + vi.useFakeTimers(); |
| 79 | + try { |
| 80 | + const requestFn = setupClient({ |
| 81 | + code: 0, |
| 82 | + bot: { bot_name: "TestBot", open_id: "ou_abc123" }, |
| 83 | + }); |
| 84 | + |
| 85 | + const creds = { appId: "cli_123", appSecret: "secret" }; |
| 86 | + await probeFeishu(creds); |
| 87 | + expect(requestFn).toHaveBeenCalledTimes(1); |
| 88 | + |
| 89 | + // Advance time past the 10-minute TTL |
| 90 | + vi.advanceTimersByTime(10 * 60 * 1000 + 1); |
| 91 | + |
| 92 | + await probeFeishu(creds); |
| 93 | + expect(requestFn).toHaveBeenCalledTimes(2); |
| 94 | + } finally { |
| 95 | + vi.useRealTimers(); |
| 96 | + } |
| 97 | + }); |
| 98 | + |
| 99 | + it("does not cache failed probe results (API error)", async () => { |
| 100 | + const requestFn = makeRequestFn({ code: 99, msg: "token expired" }); |
| 101 | + createFeishuClientMock.mockReturnValue({ request: requestFn }); |
| 102 | + |
| 103 | + const creds = { appId: "cli_123", appSecret: "secret" }; |
| 104 | + const first = await probeFeishu(creds); |
| 105 | + expect(first).toMatchObject({ ok: false, error: "API error: token expired" }); |
| 106 | + |
| 107 | + // Second call should make a fresh request since failures are not cached |
| 108 | + await probeFeishu(creds); |
| 109 | + expect(requestFn).toHaveBeenCalledTimes(2); |
| 110 | + }); |
| 111 | + |
| 112 | + it("does not cache results when request throws", async () => { |
| 113 | + const requestFn = vi.fn().mockRejectedValue(new Error("network error")); |
| 114 | + createFeishuClientMock.mockReturnValue({ request: requestFn }); |
| 115 | + |
| 116 | + const creds = { appId: "cli_123", appSecret: "secret" }; |
| 117 | + const first = await probeFeishu(creds); |
| 118 | + expect(first).toMatchObject({ ok: false, error: "network error" }); |
| 119 | + |
| 120 | + await probeFeishu(creds); |
| 121 | + expect(requestFn).toHaveBeenCalledTimes(2); |
| 122 | + }); |
| 123 | + |
| 124 | + it("caches per account independently", async () => { |
| 125 | + const requestFn = setupClient({ |
| 126 | + code: 0, |
| 127 | + bot: { bot_name: "Bot1", open_id: "ou_1" }, |
| 128 | + }); |
| 129 | + |
| 130 | + await probeFeishu({ appId: "cli_aaa", appSecret: "s1" }); |
| 131 | + expect(requestFn).toHaveBeenCalledTimes(1); |
| 132 | + |
| 133 | + // Different appId should trigger a new API call |
| 134 | + await probeFeishu({ appId: "cli_bbb", appSecret: "s2" }); |
| 135 | + expect(requestFn).toHaveBeenCalledTimes(2); |
| 136 | + |
| 137 | + // Same appId + appSecret as first call should return cached |
| 138 | + await probeFeishu({ appId: "cli_aaa", appSecret: "s1" }); |
| 139 | + expect(requestFn).toHaveBeenCalledTimes(2); |
| 140 | + }); |
| 141 | + |
| 142 | + it("does not share cache between accounts with same appId but different appSecret", async () => { |
| 143 | + const requestFn = setupClient({ |
| 144 | + code: 0, |
| 145 | + bot: { bot_name: "Bot1", open_id: "ou_1" }, |
| 146 | + }); |
| 147 | + |
| 148 | + // First account with appId + secret A |
| 149 | + await probeFeishu({ appId: "cli_shared", appSecret: "secret_aaa" }); |
| 150 | + expect(requestFn).toHaveBeenCalledTimes(1); |
| 151 | + |
| 152 | + // Second account with same appId but different secret (e.g. after rotation) |
| 153 | + // must NOT reuse the cached result |
| 154 | + await probeFeishu({ appId: "cli_shared", appSecret: "secret_bbb" }); |
| 155 | + expect(requestFn).toHaveBeenCalledTimes(2); |
| 156 | + }); |
| 157 | + |
| 158 | + it("uses accountId for cache key when available", async () => { |
| 159 | + const requestFn = setupClient({ |
| 160 | + code: 0, |
| 161 | + bot: { bot_name: "Bot1", open_id: "ou_1" }, |
| 162 | + }); |
| 163 | + |
| 164 | + // Two accounts with same appId+appSecret but different accountIds are cached separately |
| 165 | + await probeFeishu({ accountId: "acct-1", appId: "cli_123", appSecret: "secret" }); |
| 166 | + expect(requestFn).toHaveBeenCalledTimes(1); |
| 167 | + |
| 168 | + await probeFeishu({ accountId: "acct-2", appId: "cli_123", appSecret: "secret" }); |
| 169 | + expect(requestFn).toHaveBeenCalledTimes(2); |
| 170 | + |
| 171 | + // Same accountId should return cached |
| 172 | + await probeFeishu({ accountId: "acct-1", appId: "cli_123", appSecret: "secret" }); |
| 173 | + expect(requestFn).toHaveBeenCalledTimes(2); |
| 174 | + }); |
| 175 | + |
| 176 | + it("clearProbeCache forces fresh API call", async () => { |
| 177 | + const requestFn = setupClient({ |
| 178 | + code: 0, |
| 179 | + bot: { bot_name: "TestBot", open_id: "ou_abc123" }, |
| 180 | + }); |
| 181 | + |
| 182 | + const creds = { appId: "cli_123", appSecret: "secret" }; |
| 183 | + await probeFeishu(creds); |
| 184 | + expect(requestFn).toHaveBeenCalledTimes(1); |
| 185 | + |
| 186 | + clearProbeCache(); |
| 187 | + |
| 188 | + await probeFeishu(creds); |
| 189 | + expect(requestFn).toHaveBeenCalledTimes(2); |
| 190 | + }); |
| 191 | + |
| 192 | + it("handles response.data.bot fallback path", async () => { |
| 193 | + setupClient({ |
| 194 | + code: 0, |
| 195 | + data: { bot: { bot_name: "DataBot", open_id: "ou_data" } }, |
| 196 | + }); |
| 197 | + |
| 198 | + const result = await probeFeishu({ appId: "cli_123", appSecret: "secret" }); |
| 199 | + expect(result).toEqual({ |
| 200 | + ok: true, |
| 201 | + appId: "cli_123", |
| 202 | + botName: "DataBot", |
| 203 | + botOpenId: "ou_data", |
| 204 | + }); |
| 205 | + }); |
| 206 | +}); |
0 commit comments