Skip to content

Commit d7ca176

Browse files
committed
chore: 采纳 Greptile 建议整理测试工具并增强日志
- 抽出 tests/helpers/env.ts 避免重复 env snapshot\n- drop invalid update 日志增加 isTerminal 且按终态提升严重级别\n- 补充注释说明 loadLastRequests 仅返回有完成记录的用户
1 parent aaea3f6 commit d7ca176

5 files changed

Lines changed: 26 additions & 41 deletions

File tree

src/lib/proxy-status-tracker.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ export class ProxyStatusTracker {
199199
// 注意:该接口需要返回所有用户状态,因此整体复杂度与 users 数量线性相关。
200200
// 这里使用 LATERAL + 索引扫描来避免在 message_request 大表上做全表排序去重(DISTINCT ON),
201201
// 若未来用户规模显著增大(例如 1e4+),建议为该接口增加分页/按需查询,或引入专门的汇总表/物化视图。
202+
// 本查询仅返回“存在已结束请求”的用户;其余用户由 getAllUsersStatus 补齐 lastRequest=null。
202203
const query = sql<LastRequestRow>`
203204
SELECT
204205
u.id AS "userId",

src/repository/message-write-buffer.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -970,8 +970,11 @@ class MessageRequestWriteBuffer {
970970
}
971971

972972
if (lastFailure) {
973-
logger.error("[MessageRequestWriteBuffer] Dropping invalid update to unblock queue", {
973+
const isTerminal = isTerminalPatch(item.patch);
974+
const log = isTerminal ? logger.error : logger.warn;
975+
log("[MessageRequestWriteBuffer] Dropping invalid update to unblock queue", {
974976
requestId: item.id,
977+
isTerminal,
975978
keys: Object.keys(item.patch),
976979
types: summarizePatchTypes(item.patch),
977980
sample: (() => {

tests/helpers/env.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
export type EnvSnapshot = Record<string, string | undefined>;
2+
3+
export function snapshotEnv(keys: string[]): EnvSnapshot {
4+
const snapshot: EnvSnapshot = {};
5+
for (const key of keys) {
6+
snapshot[key] = process.env[key];
7+
}
8+
return snapshot;
9+
}
10+
11+
export function restoreEnv(snapshot: EnvSnapshot): void {
12+
for (const [key, value] of Object.entries(snapshot)) {
13+
if (value === undefined) {
14+
delete process.env[key];
15+
} else {
16+
process.env[key] = value;
17+
}
18+
}
19+
}

tests/unit/repository/message-orphaned-requests.test.ts

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,7 @@
11
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
2+
import { restoreEnv, snapshotEnv } from "../../helpers/env";
23
import { toSqlText } from "../../helpers/drizzle";
34

4-
type EnvSnapshot = Partial<Record<string, string | undefined>>;
5-
6-
function snapshotEnv(keys: string[]): EnvSnapshot {
7-
const snapshot: EnvSnapshot = {};
8-
for (const key of keys) {
9-
snapshot[key] = process.env[key];
10-
}
11-
return snapshot;
12-
}
13-
14-
function restoreEnv(snapshot: EnvSnapshot) {
15-
for (const [key, value] of Object.entries(snapshot)) {
16-
if (value === undefined) {
17-
delete process.env[key];
18-
} else {
19-
process.env[key] = value;
20-
}
21-
}
22-
}
23-
245
describe("sealOrphanedMessageRequests", () => {
256
const envKeys = ["NODE_ENV", "DSN", "FETCH_BODY_TIMEOUT"];
267
const originalEnv = snapshotEnv(envKeys);

tests/unit/repository/message-write-buffer.test.ts

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,7 @@
11
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
2+
import { restoreEnv, snapshotEnv } from "../../helpers/env";
23
import { toSqlText } from "../../helpers/drizzle";
34

4-
type EnvSnapshot = Partial<Record<string, string | undefined>>;
5-
6-
function snapshotEnv(keys: string[]): EnvSnapshot {
7-
const snapshot: EnvSnapshot = {};
8-
for (const key of keys) {
9-
snapshot[key] = process.env[key];
10-
}
11-
return snapshot;
12-
}
13-
14-
function restoreEnv(snapshot: EnvSnapshot) {
15-
for (const [key, value] of Object.entries(snapshot)) {
16-
if (value === undefined) {
17-
delete process.env[key];
18-
} else {
19-
process.env[key] = value;
20-
}
21-
}
22-
}
23-
245
function createDeferred<T>() {
256
let resolve!: (value: T) => void;
267
let reject!: (error: unknown) => void;

0 commit comments

Comments
 (0)