11import { afterEach , beforeEach , describe , expect , it , vi } from "vitest" ;
22import type { FeishuConfig , ResolvedFeishuAccount } from "./types.js" ;
33
4+ const clientCtorMock = vi . hoisted ( ( ) => vi . fn ( ) ) ;
45const wsClientCtorMock = vi . hoisted ( ( ) =>
56 vi . fn ( function wsClientCtor ( ) {
67 return { connected : true } ;
@@ -22,29 +23,14 @@ const mockBaseHttpInstance = vi.hoisted(() => ({
2223 head : vi . fn ( ) . mockResolvedValue ( { } ) ,
2324 options : vi . fn ( ) . mockResolvedValue ( { } ) ,
2425} ) ) ;
25-
26- vi . mock ( "@larksuiteoapi/node-sdk" , ( ) => ( {
27- AppType : { SelfBuild : "self" } ,
28- Domain : { Feishu : "https://open.feishu.cn" , Lark : "https://open.larksuite.com" } ,
29- LoggerLevel : { info : "info" } ,
30- Client : vi . fn ( ) ,
31- WSClient : wsClientCtorMock ,
32- EventDispatcher : vi . fn ( ) ,
33- defaultHttpInstance : mockBaseHttpInstance ,
34- } ) ) ;
35-
36- vi . mock ( "https-proxy-agent" , ( ) => ( {
37- HttpsProxyAgent : httpsProxyAgentCtorMock ,
38- } ) ) ;
39-
40- import { Client as LarkClient } from "@larksuiteoapi/node-sdk" ;
4126import {
4227 createFeishuClient ,
4328 createFeishuWSClient ,
4429 clearClientCache ,
4530 FEISHU_HTTP_TIMEOUT_MS ,
4631 FEISHU_HTTP_TIMEOUT_MAX_MS ,
4732 FEISHU_HTTP_TIMEOUT_ENV_VAR ,
33+ setFeishuClientRuntimeForTest ,
4834} from "./client.js" ;
4935
5036const proxyEnvKeys = [ "https_proxy" , "HTTPS_PROXY" , "http_proxy" , "HTTP_PROXY" ] as const ;
@@ -78,6 +64,21 @@ beforeEach(() => {
7864 delete process . env [ key ] ;
7965 }
8066 vi . clearAllMocks ( ) ;
67+ setFeishuClientRuntimeForTest ( {
68+ sdk : {
69+ AppType : { SelfBuild : "self" } as never ,
70+ Domain : {
71+ Feishu : "https://open.feishu.cn" ,
72+ Lark : "https://open.larksuite.com" ,
73+ } as never ,
74+ LoggerLevel : { info : "info" } as never ,
75+ Client : clientCtorMock as never ,
76+ WSClient : wsClientCtorMock as never ,
77+ EventDispatcher : vi . fn ( ) as never ,
78+ defaultHttpInstance : mockBaseHttpInstance as never ,
79+ } ,
80+ HttpsProxyAgent : httpsProxyAgentCtorMock as never ,
81+ } ) ;
8182} ) ;
8283
8384afterEach ( ( ) => {
@@ -94,6 +95,7 @@ afterEach(() => {
9495 } else {
9596 process . env [ FEISHU_HTTP_TIMEOUT_ENV_VAR ] = priorFeishuTimeoutEnv ;
9697 }
98+ setFeishuClientRuntimeForTest ( ) ;
9799} ) ;
98100
99101describe ( "createFeishuClient HTTP timeout" , ( ) => {
@@ -102,7 +104,7 @@ describe("createFeishuClient HTTP timeout", () => {
102104 } ) ;
103105
104106 const getLastClientHttpInstance = ( ) => {
105- const calls = ( LarkClient as unknown as ReturnType < typeof vi . fn > ) . mock . calls ;
107+ const calls = clientCtorMock . mock . calls ;
106108 const lastCall = calls [ calls . length - 1 ] ?. [ 0 ] as
107109 | { httpInstance ?: { get : ( ...args : unknown [ ] ) => Promise < unknown > } }
108110 | undefined ;
@@ -122,15 +124,15 @@ describe("createFeishuClient HTTP timeout", () => {
122124 it ( "passes a custom httpInstance with default timeout to Lark.Client" , ( ) => {
123125 createFeishuClient ( { appId : "app_1" , appSecret : "secret_1" , accountId : "timeout-test" } ) ; // pragma: allowlist secret
124126
125- const calls = ( LarkClient as unknown as ReturnType < typeof vi . fn > ) . mock . calls ;
127+ const calls = clientCtorMock . mock . calls ;
126128 const lastCall = calls [ calls . length - 1 ] [ 0 ] as { httpInstance ?: unknown } ;
127129 expect ( lastCall . httpInstance ) . toBeDefined ( ) ;
128130 } ) ;
129131
130132 it ( "injects default timeout into HTTP request options" , async ( ) => {
131133 createFeishuClient ( { appId : "app_2" , appSecret : "secret_2" , accountId : "timeout-inject" } ) ; // pragma: allowlist secret
132134
133- const calls = ( LarkClient as unknown as ReturnType < typeof vi . fn > ) . mock . calls ;
135+ const calls = clientCtorMock . mock . calls ;
134136 const lastCall = calls [ calls . length - 1 ] [ 0 ] as {
135137 httpInstance : { post : ( ...args : unknown [ ] ) => Promise < unknown > } ;
136138 } ;
@@ -152,7 +154,7 @@ describe("createFeishuClient HTTP timeout", () => {
152154 it ( "allows explicit timeout override per-request" , async ( ) => {
153155 createFeishuClient ( { appId : "app_3" , appSecret : "secret_3" , accountId : "timeout-override" } ) ; // pragma: allowlist secret
154156
155- const calls = ( LarkClient as unknown as ReturnType < typeof vi . fn > ) . mock . calls ;
157+ const calls = clientCtorMock . mock . calls ;
156158 const lastCall = calls [ calls . length - 1 ] [ 0 ] as {
157159 httpInstance : { get : ( ...args : unknown [ ] ) => Promise < unknown > } ;
158160 } ;
@@ -241,7 +243,7 @@ describe("createFeishuClient HTTP timeout", () => {
241243 config : { httpTimeoutMs : 45_000 } ,
242244 } ) ;
243245
244- const calls = ( LarkClient as unknown as ReturnType < typeof vi . fn > ) . mock . calls ;
246+ const calls = clientCtorMock . mock . calls ;
245247 expect ( calls . length ) . toBe ( 2 ) ;
246248
247249 const lastCall = calls [ calls . length - 1 ] [ 0 ] as {
0 commit comments