miaoda-react-devkit 0.1.1-beta.24 → 0.1.1-beta.25
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/smoke.mjs +39 -3
- package/dist/smoke/cli.js +160 -62
- package/package.json +1 -1
package/bin/smoke.mjs
CHANGED
|
@@ -28,11 +28,47 @@ function ensureCliBuild() {
|
|
|
28
28
|
}
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
+
// 计时开关与 src/smoke/timing.ts 的约定一致:关闭时直接透传,不做包装。
|
|
32
|
+
// stages 数组贯穿 CLI 全生命周期,最终由 dist 侧写入 startup.timing.json;
|
|
33
|
+
// 这里只补齐 dist 加载之前无法在 CLI 内计时的阶段。
|
|
34
|
+
const verboseTiming = process.env.DEVKIT_SMOKE_TIMING === '1';
|
|
35
|
+
const stages = [];
|
|
36
|
+
if (verboseTiming) {
|
|
37
|
+
// exit 事件兜底打印 Node 总耗时(含进程退出前的同步清理):
|
|
38
|
+
// 用 shell `real` 减去它,可估算 pnpm 启动等 CLI 之外的耗时。
|
|
39
|
+
process.once('exit', () => {
|
|
40
|
+
console.error(`[startup-smoke:timing] Node elapsed ${Math.round(performance.now())}ms`);
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// 与 SmokeTimings.measure 同构:找不到 dist(构建失败)时 CLI 层还没加载,
|
|
45
|
+
// 这两个前置阶段只能在这里记录;同样只在开启计时时才包装。
|
|
46
|
+
async function measure(stage, action) {
|
|
47
|
+
if (!verboseTiming) {
|
|
48
|
+
return action();
|
|
49
|
+
}
|
|
50
|
+
const startMs = Math.round(performance.now());
|
|
51
|
+
console.error(`[startup-smoke:timing] +${startMs}ms ${stage} start`);
|
|
52
|
+
let status = 'error';
|
|
53
|
+
try {
|
|
54
|
+
const result = await action();
|
|
55
|
+
status = 'completed';
|
|
56
|
+
return result;
|
|
57
|
+
} finally {
|
|
58
|
+
const durationMs = Math.round(performance.now()) - startMs;
|
|
59
|
+
stages.push({ stage, startMs, durationMs, status });
|
|
60
|
+
console.error(`[startup-smoke:timing] ${stage} ${status} ${durationMs}ms`);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
31
64
|
try {
|
|
32
|
-
ensureCliBuild
|
|
65
|
+
await measure('cli.ensure-build', ensureCliBuild);
|
|
33
66
|
// 应用侧依赖(vite)在 CLI 内部以应用目录为基准解析,此处无需注入任何路径。
|
|
34
|
-
const { runStartupSmokeCli } = await
|
|
35
|
-
|
|
67
|
+
const { runStartupSmokeCli } = await measure(
|
|
68
|
+
'cli.import',
|
|
69
|
+
() => import(pathToFileURL(cliPath).href),
|
|
70
|
+
);
|
|
71
|
+
await runStartupSmokeCli(undefined, stages);
|
|
36
72
|
} catch (error) {
|
|
37
73
|
console.error(`[startup-smoke] FAIL: ${error instanceof Error ? error.message : String(error)}`);
|
|
38
74
|
process.exit(1);
|
package/dist/smoke/cli.js
CHANGED
|
@@ -120,31 +120,39 @@ function hasVisibleContent() {
|
|
|
120
120
|
async function inspectPixels(screenshot) {
|
|
121
121
|
const bytes = Uint8Array.from(atob(screenshot), (char) => char.charCodeAt(0));
|
|
122
122
|
const image = await createImageBitmap(new Blob([bytes], { type: "image/png" }));
|
|
123
|
+
const width = 160;
|
|
124
|
+
const height = 100;
|
|
123
125
|
const canvas = document.createElement("canvas");
|
|
124
|
-
canvas.width =
|
|
125
|
-
canvas.height =
|
|
126
|
+
canvas.width = width;
|
|
127
|
+
canvas.height = height;
|
|
126
128
|
const context = canvas.getContext("2d", { willReadFrequently: true });
|
|
127
129
|
if (context === null) {
|
|
128
130
|
throw new Error("Could not inspect screenshot pixels");
|
|
129
131
|
}
|
|
130
|
-
context.drawImage(image, 0, 0);
|
|
132
|
+
context.drawImage(image, 0, 0, width, height);
|
|
131
133
|
image.close();
|
|
132
|
-
const { data } = context.getImageData(0, 0,
|
|
133
|
-
const
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
134
|
+
const { data } = context.getImageData(0, 0, width, height);
|
|
135
|
+
const quantCounts = /* @__PURE__ */ new Map();
|
|
136
|
+
const exactColors = /* @__PURE__ */ new Set();
|
|
137
|
+
const total = width * height;
|
|
138
|
+
let dominant = 0;
|
|
139
|
+
for (let index = 0; index < data.length; index += 4) {
|
|
140
|
+
const r = data[index];
|
|
141
|
+
const g = data[index + 1];
|
|
142
|
+
const b = data[index + 2];
|
|
143
|
+
exactColors.add(r << 16 | g << 8 | b);
|
|
144
|
+
const bucket = r >> 4 << 8 | g >> 4 << 4 | b >> 4;
|
|
145
|
+
const next = (quantCounts.get(bucket) ?? 0) + 1;
|
|
146
|
+
quantCounts.set(bucket, next);
|
|
147
|
+
if (next > dominant) {
|
|
148
|
+
dominant = next;
|
|
142
149
|
}
|
|
143
150
|
}
|
|
144
|
-
const
|
|
145
|
-
|
|
151
|
+
const mainColorRatio = dominant / total;
|
|
152
|
+
const uniqColors = exactColors.size;
|
|
153
|
+
return { mainColorRatio, uniqColors, nearlyUniform: mainColorRatio >= 0.99 || uniqColors <= 12 };
|
|
146
154
|
}
|
|
147
|
-
async function inspectStartup(page, startUrl) {
|
|
155
|
+
async function inspectStartup(page, startUrl, timings) {
|
|
148
156
|
const pending = /* @__PURE__ */ new Set();
|
|
149
157
|
const navigationErrors = [];
|
|
150
158
|
const pageErrors = [];
|
|
@@ -197,28 +205,40 @@ async function inspectStartup(page, startUrl) {
|
|
|
197
205
|
let documentStatus = null;
|
|
198
206
|
const navigationStartedAt = performance.now();
|
|
199
207
|
try {
|
|
200
|
-
const response = await
|
|
208
|
+
const response = await timings.measure(
|
|
209
|
+
"page.navigate",
|
|
210
|
+
() => page.goto(startUrl, { waitUntil: "domcontentloaded", timeout: navigationTimeoutMs })
|
|
211
|
+
);
|
|
201
212
|
documentStatus = response?.status() ?? null;
|
|
202
213
|
} catch (error) {
|
|
203
214
|
navigationErrors.push(error instanceof Error ? error.message : String(error));
|
|
204
215
|
}
|
|
205
216
|
const navigationDurationMs = Math.round(performance.now() - navigationStartedAt);
|
|
206
217
|
if (documentStatus !== null && documentStatus < 400) {
|
|
207
|
-
await
|
|
218
|
+
await timings.measure(
|
|
219
|
+
"page.wait-visible",
|
|
220
|
+
() => page.waitForFunction(hasVisibleContent, void 0, { timeout: firstScreenTimeoutMs })
|
|
221
|
+
).catch(() => {
|
|
222
|
+
});
|
|
223
|
+
await timings.measure("page.render-grace", () => delay(300));
|
|
224
|
+
await timings.measure("page.wait-requests", async () => {
|
|
225
|
+
const deadline = performance.now() + requestSettleTimeoutMs;
|
|
226
|
+
while (pending.size > 0 && performance.now() < deadline) {
|
|
227
|
+
await delay(50);
|
|
228
|
+
}
|
|
208
229
|
});
|
|
209
|
-
await delay(300);
|
|
210
|
-
const deadline = performance.now() + requestSettleTimeoutMs;
|
|
211
|
-
while (pending.size > 0 && performance.now() < deadline) {
|
|
212
|
-
await delay(50);
|
|
213
|
-
}
|
|
214
230
|
}
|
|
215
231
|
let visibleContent = false;
|
|
216
232
|
let pixels = null;
|
|
217
233
|
let screenshot;
|
|
218
234
|
try {
|
|
219
|
-
visibleContent = await page.evaluate(hasVisibleContent);
|
|
220
|
-
screenshot = await page.screenshot
|
|
221
|
-
|
|
235
|
+
visibleContent = await timings.measure("page.check-visible", () => page.evaluate(hasVisibleContent));
|
|
236
|
+
screenshot = await timings.measure("page.screenshot", async () => {
|
|
237
|
+
const cdp = await page.context().newCDPSession(page);
|
|
238
|
+
const shot = await cdp.send("Page.captureScreenshot", { format: "png" });
|
|
239
|
+
return Buffer.from(shot.data, "base64");
|
|
240
|
+
});
|
|
241
|
+
pixels = await timings.measure("page.inspect-pixels", () => page.evaluate(inspectPixels, screenshot.toString("base64")));
|
|
222
242
|
} catch (error) {
|
|
223
243
|
reviewNotes.push(`First-screen capture: ${error instanceof Error ? error.message : String(error)}`);
|
|
224
244
|
}
|
|
@@ -226,6 +246,13 @@ async function inspectStartup(page, startUrl) {
|
|
|
226
246
|
reviewNotes.push("The screenshot contains visual detail, but no visible text, control or media was detected.");
|
|
227
247
|
}
|
|
228
248
|
const firstScreen = { looksBlank: pixels?.nearlyUniform ?? !visibleContent, visibleContent, pixels };
|
|
249
|
+
let title = "";
|
|
250
|
+
let textHead = "";
|
|
251
|
+
try {
|
|
252
|
+
title = await page.title();
|
|
253
|
+
textHead = (await page.evaluate(() => document.body?.innerText ?? "")).trim().slice(0, 200);
|
|
254
|
+
} catch {
|
|
255
|
+
}
|
|
229
256
|
return {
|
|
230
257
|
screenshot,
|
|
231
258
|
diagnostics: {
|
|
@@ -234,6 +261,8 @@ async function inspectStartup(page, startUrl) {
|
|
|
234
261
|
documentStatus,
|
|
235
262
|
navigationDurationMs,
|
|
236
263
|
firstScreen,
|
|
264
|
+
title,
|
|
265
|
+
textHead,
|
|
237
266
|
navigationErrors,
|
|
238
267
|
pageErrors,
|
|
239
268
|
consoleErrors,
|
|
@@ -297,7 +326,7 @@ function collectStartupFailures(diagnostics) {
|
|
|
297
326
|
}
|
|
298
327
|
return failures;
|
|
299
328
|
}
|
|
300
|
-
async function runStartupSmoke({ root, output }) {
|
|
329
|
+
async function runStartupSmoke({ root, output, timings }) {
|
|
301
330
|
const failures = [];
|
|
302
331
|
let server;
|
|
303
332
|
let browser;
|
|
@@ -306,26 +335,51 @@ async function runStartupSmoke({ root, output }) {
|
|
|
306
335
|
let screenshot;
|
|
307
336
|
let trace;
|
|
308
337
|
try {
|
|
309
|
-
const
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
338
|
+
const browserLaunching = timings.measure("browser.launch", launchChromium);
|
|
339
|
+
void browserLaunching.then(
|
|
340
|
+
(launched) => {
|
|
341
|
+
browser = launched;
|
|
342
|
+
},
|
|
343
|
+
() => {
|
|
344
|
+
}
|
|
345
|
+
// 失败由下方 Promise.all 抛出并统一记录,这里只防未处理拒绝
|
|
346
|
+
);
|
|
347
|
+
const [viteServer, chromiumBrowser] = await Promise.all([
|
|
348
|
+
timings.measure("vite.start", async () => {
|
|
349
|
+
const { createServer } = await timings.measure("vite.import", () => resolveAppVite(root));
|
|
350
|
+
const started = await timings.measure("vite.create-server", () => createServer({
|
|
351
|
+
root,
|
|
352
|
+
// 依赖预构建缓存指到本次运行目录,不污染应用自身的 node_modules/.vite。
|
|
353
|
+
cacheDir: join2(output, "vite-cache"),
|
|
354
|
+
server: { host: "127.0.0.1", port: 0, open: false, hmr: false }
|
|
355
|
+
}));
|
|
356
|
+
server = started;
|
|
357
|
+
await timings.measure("vite.listen", () => started.listen());
|
|
358
|
+
return started;
|
|
359
|
+
}),
|
|
360
|
+
browserLaunching
|
|
361
|
+
]);
|
|
362
|
+
browser = chromiumBrowser;
|
|
363
|
+
const url = viteServer.resolvedUrls?.local[0];
|
|
318
364
|
if (url === void 0) {
|
|
319
365
|
throw new Error("Vite \u672A\u8FD4\u56DE\u672C\u5730\u8BBF\u95EE\u5730\u5740");
|
|
320
366
|
}
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
367
|
+
const browserContext = await timings.measure(
|
|
368
|
+
"browser.new-context",
|
|
369
|
+
() => chromiumBrowser.newContext({ viewport: { width: 1280, height: 800 } })
|
|
370
|
+
);
|
|
371
|
+
context = browserContext;
|
|
372
|
+
await timings.measure(
|
|
373
|
+
"trace.start",
|
|
374
|
+
() => browserContext.tracing.start({ screenshots: true, snapshots: true, sources: true })
|
|
375
|
+
);
|
|
376
|
+
const page = await timings.measure("browser.new-page", () => browserContext.newPage());
|
|
377
|
+
const result = await inspectStartup(page, url, timings);
|
|
325
378
|
diagnostics = result.diagnostics;
|
|
326
379
|
failures.push(...collectStartupFailures(diagnostics));
|
|
327
|
-
|
|
328
|
-
|
|
380
|
+
const screenshotBytes = result.screenshot;
|
|
381
|
+
if (screenshotBytes !== void 0) {
|
|
382
|
+
await timings.measure("artifact.write-screenshot", () => writeFile(join2(output, "startup.png"), screenshotBytes));
|
|
329
383
|
screenshot = relative(root, join2(output, "startup.png"));
|
|
330
384
|
}
|
|
331
385
|
} catch (error) {
|
|
@@ -336,7 +390,7 @@ async function runStartupSmoke({ root, output }) {
|
|
|
336
390
|
const serverRef = server;
|
|
337
391
|
const cleanupSteps = [];
|
|
338
392
|
if (contextRef !== void 0) {
|
|
339
|
-
cleanupSteps.push(["save Playwright trace", async () => {
|
|
393
|
+
cleanupSteps.push(["trace.stop", "save Playwright trace", async () => {
|
|
340
394
|
if (failures.length > 0) {
|
|
341
395
|
const tracePath = join2(output, "startup.trace.zip");
|
|
342
396
|
await contextRef.tracing.stop({ path: tracePath });
|
|
@@ -345,17 +399,17 @@ async function runStartupSmoke({ root, output }) {
|
|
|
345
399
|
await contextRef.tracing.stop();
|
|
346
400
|
}
|
|
347
401
|
}]);
|
|
348
|
-
cleanupSteps.push(["close browser context", () => contextRef.close()]);
|
|
402
|
+
cleanupSteps.push(["browser.close-context", "close browser context", () => contextRef.close()]);
|
|
349
403
|
}
|
|
350
404
|
if (browserRef !== void 0) {
|
|
351
|
-
cleanupSteps.push(["close browser", () => browserRef.close()]);
|
|
405
|
+
cleanupSteps.push(["browser.close", "close browser", () => browserRef.close()]);
|
|
352
406
|
}
|
|
353
407
|
if (serverRef !== void 0) {
|
|
354
|
-
cleanupSteps.push(["close Vite server", () => serverRef.close()]);
|
|
408
|
+
cleanupSteps.push(["vite.close", "close Vite server", () => serverRef.close()]);
|
|
355
409
|
}
|
|
356
|
-
for (const [check, action] of cleanupSteps) {
|
|
410
|
+
for (const [stage, check, action] of cleanupSteps) {
|
|
357
411
|
try {
|
|
358
|
-
await action
|
|
412
|
+
await timings.measure(stage, action);
|
|
359
413
|
} catch (error) {
|
|
360
414
|
failures.push({ check, details: errorText(error) });
|
|
361
415
|
}
|
|
@@ -364,21 +418,71 @@ async function runStartupSmoke({ root, output }) {
|
|
|
364
418
|
return { status: failures.length > 0 ? "failed" : "passed", failures, diagnostics, screenshot, trace };
|
|
365
419
|
}
|
|
366
420
|
|
|
421
|
+
// src/smoke/timing.ts
|
|
422
|
+
var SmokeTimings = class {
|
|
423
|
+
constructor(stages = [], enabled = process.env.DEVKIT_SMOKE_TIMING === "1") {
|
|
424
|
+
this.stages = stages;
|
|
425
|
+
this.enabled = enabled;
|
|
426
|
+
}
|
|
427
|
+
stages;
|
|
428
|
+
enabled;
|
|
429
|
+
/**
|
|
430
|
+
* 测量一个阶段:记录起点、终点与状态后原样返回动作结果。
|
|
431
|
+
* 实时日志在每个阶段开始前就打印,进程卡死时能直接看到停在哪个阶段。
|
|
432
|
+
*/
|
|
433
|
+
async measure(stage, action) {
|
|
434
|
+
if (!this.enabled) {
|
|
435
|
+
return action();
|
|
436
|
+
}
|
|
437
|
+
const start = performance.now();
|
|
438
|
+
console.error(`[startup-smoke:timing] +${Math.round(start)}ms ${stage} start`);
|
|
439
|
+
let status = "error";
|
|
440
|
+
try {
|
|
441
|
+
const result = await action();
|
|
442
|
+
status = "completed";
|
|
443
|
+
return result;
|
|
444
|
+
} finally {
|
|
445
|
+
const durationMs = Math.round(performance.now() - start);
|
|
446
|
+
this.stages.push({ stage, startMs: Math.round(start), durationMs, status });
|
|
447
|
+
console.error(`[startup-smoke:timing] ${stage} ${status} ${durationMs}ms`);
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
};
|
|
451
|
+
|
|
367
452
|
// src/smoke/cli.ts
|
|
368
|
-
async function runStartupSmokeCli(root = process.cwd()) {
|
|
369
|
-
const
|
|
453
|
+
async function runStartupSmokeCli(root = process.cwd(), stages = []) {
|
|
454
|
+
const timings = new SmokeTimings(stages);
|
|
455
|
+
const entry = await timings.measure("cli.classify-entry", () => classifyEntry(root));
|
|
370
456
|
if ("skip" in entry) {
|
|
371
457
|
console.log(`[startup-smoke] SKIP: ${entry.skip}`);
|
|
372
458
|
return;
|
|
373
459
|
}
|
|
374
|
-
const
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
460
|
+
const output = await timings.measure("cli.prepare-output", async () => {
|
|
461
|
+
const resultsDir = join3(root, "test-results");
|
|
462
|
+
await mkdir(resultsDir, { recursive: true });
|
|
463
|
+
return mkdtemp(join3(resultsDir, "startup-"));
|
|
464
|
+
});
|
|
465
|
+
const result = await runStartupSmoke({ root, output, timings });
|
|
378
466
|
const reportPath = join3(output, "startup.json");
|
|
379
467
|
const report = { checkedAt: (/* @__PURE__ */ new Date()).toISOString(), ...result };
|
|
380
|
-
await writeFile2(reportPath, `${JSON.stringify(report, null, 2)}
|
|
381
|
-
`);
|
|
468
|
+
await timings.measure("cli.write-report", () => writeFile2(reportPath, `${JSON.stringify(report, null, 2)}
|
|
469
|
+
`));
|
|
470
|
+
if (timings.enabled) {
|
|
471
|
+
const timingReport = {
|
|
472
|
+
nodeStartedAt: new Date(performance.timeOrigin).toISOString(),
|
|
473
|
+
reportWrittenMs: Math.round(performance.now()),
|
|
474
|
+
stages
|
|
475
|
+
};
|
|
476
|
+
try {
|
|
477
|
+
await timings.measure(
|
|
478
|
+
"cli.write-timing",
|
|
479
|
+
() => writeFile2(join3(output, "startup.timing.json"), `${JSON.stringify(timingReport, null, 2)}
|
|
480
|
+
`)
|
|
481
|
+
);
|
|
482
|
+
} catch (error) {
|
|
483
|
+
console.error(`[startup-smoke:timing] ${error instanceof Error ? error.message : String(error)}`);
|
|
484
|
+
}
|
|
485
|
+
}
|
|
382
486
|
if (result.status === "passed") {
|
|
383
487
|
console.log("[startup-smoke] PASS: homepage loaded, painted content, and started without errors");
|
|
384
488
|
return;
|
|
@@ -391,12 +495,6 @@ async function runStartupSmokeCli(root = process.cwd()) {
|
|
|
391
495
|
}
|
|
392
496
|
console.log("Startup smoke diagnostics for this failure:");
|
|
393
497
|
console.log(`- app report (page errors, console errors, failed network requests): ${relative2(root, reportPath)}`);
|
|
394
|
-
if (result.screenshot !== void 0) {
|
|
395
|
-
console.log(`- first screen screenshot: ${result.screenshot}`);
|
|
396
|
-
}
|
|
397
|
-
if (result.trace !== void 0) {
|
|
398
|
-
console.log(`- Playwright trace: ${result.trace}`);
|
|
399
|
-
}
|
|
400
498
|
}
|
|
401
499
|
export {
|
|
402
500
|
runStartupSmokeCli
|