Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(tests): count waits through promise timer wrappers

MagicLizid267f2de5243
diff --git a/docs/dev-rules/desktop-unit-test-performance.md b/docs/dev-rules/desktop-unit-test-performance.md
index a87124dd3c..050c725728 100644
--- a/docs/dev-rules/desktop-unit-test-performance.md
+++ b/docs/dev-rules/desktop-unit-test-performance.md
@@ -108,6 +108,9 @@ CI 仍须重新通过。
保存的原生函数不会因之后启用假时钟而豁免,假时钟下保存的函数也不会因恢复真实
时钟而误报。`node:timers` 的 named/namespace/default 绑定保留原生身份;
当前 Vitest 3.2.7 的真实函数身份验证与 scanner 正反例均通过,基线保持 374 个文件。
+Promise timer 的本地 wrapper 也按导入绑定识别,并使用 Promise API 的首个时长参数;
+named 与 namespace wrapper、短等待、常量内层等待和参数/fixture 遮蔽均有回归,
+避免把包装后的真实等待漏出卫生门。
导入设置恢复真实组件挂载回归,验证首次自动扫描与重入时先显示缓存、再更新结果,
2 项单文件命令约 2.2 秒。slash 空结果、目录队列插入与语音输入保护并入既有 ChatInput
文件,经过真实编辑器/hook 验证键盘消费、目录 chip、语音全生命周期只读与顶层弹层
diff --git a/scripts/__tests__/check-test-hygiene.test.mjs b/scripts/__tests__/check-test-hygiene.test.mjs
index cf12ecb2f4..36124dd465 100644
--- a/scripts/__tests__/check-test-hygiene.test.mjs
+++ b/scripts/__tests__/check-test-hygiene.test.mjs
@@ -114,6 +114,11 @@ test("scanTestSource counts only unjustified hits for each rule", () => {
["promise timer namespace keeps the duration", "import * as timers from 'node:timers/promises'; await timers.setTimeout(1_000);", { realWaits: 1 }],
["promise timer namespace alias keeps the duration", "import timers from 'timers/promises'; const promises = timers; await promises.setTimeout(1_000);", { realWaits: 1 }],
["short promise timer ignores its numeric return value", "import { setTimeout } from 'node:timers/promises'; await setTimeout(20, 1_000);", { realWaits: 0 }],
+ ["promise timer wrapper uses its first argument", "import { setTimeout as delay } from 'node:timers/promises'; const tick = ms => delay(ms); await tick(1_000);", { realWaits: 1 }],
+ ["promise timer namespace wrapper uses its first argument", "import * as timers from 'node:timers/promises'; const tick = ms => timers.setTimeout(ms); await tick(1_000);", { realWaits: 1 }],
+ ["short promise timer wrapper stays below the threshold", "import { setTimeout as delay } from 'node:timers/promises'; const tick = ms => delay(ms); await tick(20);", { realWaits: 0 }],
+ ["promise timer wrapper does not double count a constant inner wait", "import { setTimeout as delay } from 'node:timers/promises'; const tick = () => delay(1_000); await tick(20);", { realWaits: 1 }],
+ ["promise timer wrapper respects parameter and fixture shadows", "import { setTimeout as delay } from 'node:timers/promises'; const tick = ms => delay(ms); function example(tick) { tick(1_000); } const fixture = { tick() {} }; fixture.tick();", { realWaits: 0 }],
["a parameter can shadow the promise timer", "import { setTimeout as delay } from 'node:timers/promises'; function example(delay) { delay(1_000); }", { realWaits: 0 }],
["a local function can shadow the promise timer", "import { setTimeout as delay } from 'node:timers/promises'; function example() { const delay = value => value; delay(1_000); }", { realWaits: 0 }],
["a parameter can shadow the timer namespace", "import * as timers from 'node:timers/promises'; function example(timers) { timers.setTimeout(1_000); }", { realWaits: 0 }],
diff --git a/scripts/check-test-hygiene.mjs b/scripts/check-test-hygiene.mjs
index ff1651be9d..8f38b24550 100644
--- a/scripts/check-test-hygiene.mjs
+++ b/scripts/check-test-hygiene.mjs
@@ -456,12 +456,19 @@ function staticWaitCalls(code) {
const body = ts.isBlock(fn.body)
? fn.body.statements.find(ts.isReturnStatement)?.expression : fn.body;
if (!body) return null;
- if (ts.isCallExpression(body) && callbackTimer(body.expression)) return body;
+ const timerCall = node => {
+ if (!node || !ts.isCallExpression(node)) return null;
+ if (callbackTimer(node.expression)) return { call: node, kind: 'callback' };
+ if (isPromiseTimer(node.expression)) return { call: node, kind: 'promise' };
+ return null;
+ };
+ const direct = timerCall(body);
+ if (direct) return direct;
if (!ts.isNewExpression(body) || body.expression.getText(source) !== 'Promise') return null;
const executor = body.arguments?.[0];
if (!executor || !isFunction(executor)) return null;
const candidates = ts.isBlock(executor.body) ? executor.body.statements.map(statement => statement.expression) : [executor.body];
- return candidates.find(node => node && ts.isCallExpression(node) && callbackTimer(node.expression)) ?? null;
+ return candidates.map(timerCall).find(Boolean) ?? null;
}
const waits = [];
for (const call of calls) {
@@ -483,13 +490,15 @@ function staticWaitCalls(code) {
const bound = binding(call.expression);
const fn = bound && ts.isVariableDeclaration(bound) ? bound.initializer : bound;
if (!fn || !isFunction(fn)) continue;
- const timer = wrapperTimer(fn);
- if (!timer) continue;
+ const timerInfo = wrapperTimer(fn);
+ if (!timerInfo) continue;
+ const timer = timerInfo.call;
+ const delayArgument = timerInfo.kind === 'promise' ? 0 : 1;
const parameters = new Map(fn.parameters.map((parameter, index) => [parameter, number(call.arguments[index] ?? parameter.initializer)]));
// Constant waits inside a helper are already counted at their own site.
- if (number(timer.arguments[1]) !== undefined) continue;
- duration = number(timer.arguments[1], parameters);
- clockIndex = callbackTimer(timer.expression).clockIndex;
+ if (number(timer.arguments[delayArgument]) !== undefined) continue;
+ duration = number(timer.arguments[delayArgument], parameters);
+ if (timerInfo.kind === 'callback') clockIndex = callbackTimer(timer.expression).clockIndex;
// A wrapper-local capture executes when the wrapper is called.
if (clockIndex >= fn.getStart(source) && clockIndex < fn.end) clockIndex = undefined;
}