@threenative/runtime-native 0.2.0 → 0.3.1
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/NOTICE +2 -0
- package/README.md +63 -0
- package/android/README.md +2 -2
- package/android/app/build.gradle.kts +177 -30
- package/android/app/src/main/AndroidManifest.xml +8 -1
- package/android/app/src/main/java/com/mystral/engine/MystralActivity.java +190 -2
- package/android/app/src/main/java/com/mystral/engine/TnUiOverlay.java +261 -0
- package/android/app/src/main/res/values/branding.xml +12 -0
- package/android/app/src/main/res/values/themes.xml +2 -0
- package/android/gradle.properties +1 -1
- package/package.json +44 -8
- package/scripts/asset-preflight.mjs +442 -0
- package/scripts/bundle.mjs +178 -48
- package/scripts/check-source-list.mjs +90 -0
- package/scripts/device-preflight.mjs +456 -0
- package/scripts/install-prebuilt.mjs +326 -30
- package/scripts/package-android.mjs +414 -23
- package/scripts/package-desktop.mjs +132 -12
- package/scripts/package-ios.mjs +249 -25
- package/scripts/physical-device-evidence.mjs +644 -0
- package/scripts/production-evidence.mjs +101 -2
- package/scripts/profile-production.mjs +544 -115
- package/scripts/qualify-physical-mobile.mjs +1327 -0
- package/scripts/verify-starter-desktop.mjs +140 -16
- package/scripts/xvfb.sh +68 -0
|
@@ -0,0 +1,456 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import { homedir } from "node:os";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { spawnSync } from "node:child_process";
|
|
5
|
+
|
|
6
|
+
export const MINIMUM_BATTERY_PERCENT = 50;
|
|
7
|
+
|
|
8
|
+
const THERMAL_STATUS_CODES = {
|
|
9
|
+
NONE: 0,
|
|
10
|
+
LIGHT: 1,
|
|
11
|
+
MODERATE: 2,
|
|
12
|
+
SEVERE: 3,
|
|
13
|
+
CRITICAL: 4,
|
|
14
|
+
EMERGENCY: 5,
|
|
15
|
+
SHUTDOWN: 6,
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export class DevicePreflightError extends Error {
|
|
19
|
+
constructor(code, detail, details = {}) {
|
|
20
|
+
super(`${code}: ${detail}`);
|
|
21
|
+
this.name = code;
|
|
22
|
+
this.code = code;
|
|
23
|
+
this.details = details;
|
|
24
|
+
this.exitCode = 2;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function parseBoolean(value, label) {
|
|
29
|
+
if (value === "true") return true;
|
|
30
|
+
if (value === "false") return false;
|
|
31
|
+
throw new DevicePreflightError(
|
|
32
|
+
`TN_DEVICE_PREFLIGHT_${label.toUpperCase()}_PARSE`,
|
|
33
|
+
`expected true or false, received ${value}`,
|
|
34
|
+
{ condition: label, observed: value },
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function parseBatteryState(output) {
|
|
39
|
+
const source = String(output);
|
|
40
|
+
const levelMatch = /^\s*level:\s*(\d+)\s*$/imu.exec(source);
|
|
41
|
+
if (!levelMatch) {
|
|
42
|
+
throw new DevicePreflightError(
|
|
43
|
+
"TN_DEVICE_PREFLIGHT_BATTERY_PARSE",
|
|
44
|
+
"dumpsys battery has no numeric level",
|
|
45
|
+
{ condition: "battery", observed: source },
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
const batteryPercent = Number(levelMatch[1]);
|
|
49
|
+
if (!Number.isInteger(batteryPercent) || batteryPercent < 0 || batteryPercent > 100) {
|
|
50
|
+
throw new DevicePreflightError(
|
|
51
|
+
"TN_DEVICE_PREFLIGHT_BATTERY_PARSE",
|
|
52
|
+
`battery level is outside 0..100: ${levelMatch[1]}`,
|
|
53
|
+
{ condition: "battery", observed: levelMatch[1] },
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const sources = [
|
|
58
|
+
["AC", /^\s*AC powered:\s*(true|false)\s*$/imu],
|
|
59
|
+
["USB", /^\s*USB powered:\s*(true|false)\s*$/imu],
|
|
60
|
+
["WIRELESS", /^\s*Wireless powered:\s*(true|false)\s*$/imu],
|
|
61
|
+
];
|
|
62
|
+
const poweredBy = sources.map(([name, pattern]) => {
|
|
63
|
+
const match = pattern.exec(source);
|
|
64
|
+
return match ? { name, powered: parseBoolean(match[1].toLowerCase(), "charging") } : null;
|
|
65
|
+
});
|
|
66
|
+
const presentSources = poweredBy.filter(Boolean);
|
|
67
|
+
const statusMatch = /^[ \t]*status:[ \t]*([^\r\n]*?)[ \t]*$/imu.exec(source);
|
|
68
|
+
const statusText = statusMatch?.[1] ?? null;
|
|
69
|
+
const statusIsCanonical = statusText !== null && /^(?:0|[1-9][0-9]*)$/u.test(statusText);
|
|
70
|
+
const status = statusIsCanonical ? Number(statusText) : null;
|
|
71
|
+
if (
|
|
72
|
+
statusText !== null &&
|
|
73
|
+
(!statusIsCanonical || !Number.isInteger(status) || status < 1 || status > 5)
|
|
74
|
+
) {
|
|
75
|
+
throw new DevicePreflightError(
|
|
76
|
+
"TN_DEVICE_PREFLIGHT_CHARGING_PARSE",
|
|
77
|
+
`dumpsys battery has unrecognised status: ${statusText}`,
|
|
78
|
+
{ condition: "charging", observed: statusText },
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
if (status === 1) {
|
|
82
|
+
throw new DevicePreflightError(
|
|
83
|
+
"TN_DEVICE_PREFLIGHT_CHARGING_PARSE",
|
|
84
|
+
"dumpsys battery status UNKNOWN cannot prove discharging",
|
|
85
|
+
{ condition: "charging", observed: statusText },
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
if (presentSources.length !== sources.length && status === null) {
|
|
89
|
+
throw new DevicePreflightError(
|
|
90
|
+
"TN_DEVICE_PREFLIGHT_CHARGING_PARSE",
|
|
91
|
+
"dumpsys battery has incomplete charging sources and no status",
|
|
92
|
+
{ condition: "charging", observed: source },
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const activeSource = poweredBy.find((entry) => entry?.powered === true)?.name;
|
|
97
|
+
const chargingByStatus = status === 2 || status === 5;
|
|
98
|
+
const charging = activeSource !== undefined || chargingByStatus;
|
|
99
|
+
return {
|
|
100
|
+
batteryPercent,
|
|
101
|
+
charging,
|
|
102
|
+
chargingSource: activeSource ?? (chargingByStatus ? "STATUS" : "NONE"),
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function thermalStatusFromValue(value) {
|
|
107
|
+
const normalized = String(value).trim().toUpperCase().replace(/^THERMAL_STATUS_/u, "");
|
|
108
|
+
if (/^\d+$/u.test(normalized)) {
|
|
109
|
+
const code = Number(normalized);
|
|
110
|
+
const status = Object.entries(THERMAL_STATUS_CODES).find(([, candidate]) => candidate === code)?.[0];
|
|
111
|
+
if (status !== undefined) return { code, status };
|
|
112
|
+
}
|
|
113
|
+
const code = THERMAL_STATUS_CODES[normalized];
|
|
114
|
+
if (code !== undefined) return { code, status: normalized };
|
|
115
|
+
throw new DevicePreflightError(
|
|
116
|
+
"TN_DEVICE_PREFLIGHT_THERMAL_PARSE",
|
|
117
|
+
`unrecognised thermal status: ${value}`,
|
|
118
|
+
{ condition: "thermal", observed: value },
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export function parseThermalState(output) {
|
|
123
|
+
const source = String(output);
|
|
124
|
+
const match =
|
|
125
|
+
/(?:current\s+)?thermal\s+status\s*[:=]\s*([A-Za-z_]+|\d+)/iu.exec(source) ??
|
|
126
|
+
/mThermalStatus\s*[:=]\s*([A-Za-z_]+|\d+)/iu.exec(source);
|
|
127
|
+
if (!match) {
|
|
128
|
+
throw new DevicePreflightError(
|
|
129
|
+
"TN_DEVICE_PREFLIGHT_THERMAL_PARSE",
|
|
130
|
+
"dumpsys thermalservice has no thermal status",
|
|
131
|
+
{ condition: "thermal", observed: source },
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
const parsed = thermalStatusFromValue(match[1]);
|
|
135
|
+
return { thermalStatus: parsed.status, thermalStatusCode: parsed.code };
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export function parseScreenState(output) {
|
|
139
|
+
const source = String(output);
|
|
140
|
+
const screenMatch = /(?:^|\n)\s*(?:mScreenOn|screenOn)\s*[:=]\s*(true|false)\s*$/imu.exec(source);
|
|
141
|
+
if (screenMatch) return { screenOn: parseBoolean(screenMatch[1].toLowerCase(), "screen") };
|
|
142
|
+
|
|
143
|
+
const displayMatch = /display\s+power:\s*state\s*=\s*(ON|OFF)\b/iu.exec(source);
|
|
144
|
+
if (displayMatch) return { screenOn: displayMatch[1].toUpperCase() === "ON" };
|
|
145
|
+
|
|
146
|
+
const wakefulnessMatch = /mWakefulness(?:Raw)?\s*=\s*(Awake|Asleep|Dozing)\b/iu.exec(source);
|
|
147
|
+
if (wakefulnessMatch) return { screenOn: wakefulnessMatch[1].toLowerCase() === "awake" };
|
|
148
|
+
|
|
149
|
+
throw new DevicePreflightError(
|
|
150
|
+
"TN_DEVICE_PREFLIGHT_SCREEN_PARSE",
|
|
151
|
+
"dumpsys power has no recognised screen state",
|
|
152
|
+
{ condition: "screen", observed: source },
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* The panel's *active* mode, not the app's vote and not the settings value.
|
|
158
|
+
*
|
|
159
|
+
* A Pixel 8 found with Smooth Display off reports `peak_refresh_rate 60.0` and an active mode of
|
|
160
|
+
* 60 Hz while `supportedModes` still lists 120 — so the app's `Surface.setFrameRate(120)` is
|
|
161
|
+
* clamped away rather than declined, and an fps arm silently measures a different machine than the
|
|
162
|
+
* one it believes. The tell in the data is a SurfaceFlinger histogram of 16 ms and 33 ms intervals
|
|
163
|
+
* with no 8 ms bucket. Reading it here is cheaper than recognising it later.
|
|
164
|
+
*/
|
|
165
|
+
export function parseActiveDisplayMode(output) {
|
|
166
|
+
const source = String(output);
|
|
167
|
+
const active = /mActiveSfDisplayMode\s*=\s*DisplayMode\{[^}]*?peakRefreshRate\s*=\s*([0-9.]+)/u.exec(
|
|
168
|
+
source,
|
|
169
|
+
);
|
|
170
|
+
if (!active) {
|
|
171
|
+
throw new DevicePreflightError(
|
|
172
|
+
"TN_DEVICE_PREFLIGHT_DISPLAY_PARSE",
|
|
173
|
+
"dumpsys display has no mActiveSfDisplayMode",
|
|
174
|
+
{ condition: "display", observed: source.slice(0, 200) },
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
const supported = /mSupportedRefreshRates\s*=\s*\[([^\]]*)\]/u.exec(source);
|
|
178
|
+
const supportedRefreshHz = supported
|
|
179
|
+
? supported[1]
|
|
180
|
+
.split(",")
|
|
181
|
+
.map((entry) => Number(entry.trim()))
|
|
182
|
+
.filter((entry) => Number.isFinite(entry))
|
|
183
|
+
.map((entry) => Math.round(entry))
|
|
184
|
+
: [];
|
|
185
|
+
return {
|
|
186
|
+
activeRefreshHz: Math.round(Number(active[1])),
|
|
187
|
+
supportedRefreshHz: [...new Set(supportedRefreshHz)].sort((a, b) => b - a),
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/** `settings get` prints the literal `null` when a key was never written; that is "unset", not 0. */
|
|
192
|
+
function settingNumber(value) {
|
|
193
|
+
const trimmed = String(value).trim();
|
|
194
|
+
if (trimmed === "" || trimmed === "null") return undefined;
|
|
195
|
+
const parsed = Number(trimmed);
|
|
196
|
+
return Number.isFinite(parsed) ? Math.round(parsed) : undefined;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
export function parseRefreshRateSettings({ peak, min, lowPower }) {
|
|
200
|
+
const power = String(lowPower ?? "").trim();
|
|
201
|
+
if (power !== "" && power !== "null" && power !== "0" && power !== "1") {
|
|
202
|
+
throw new DevicePreflightError(
|
|
203
|
+
"TN_DEVICE_PREFLIGHT_DISPLAY_PARSE",
|
|
204
|
+
`unrecognised low_power value: ${power}`,
|
|
205
|
+
{ condition: "lowPower", observed: power },
|
|
206
|
+
);
|
|
207
|
+
}
|
|
208
|
+
return {
|
|
209
|
+
peakRefreshRateSetting: settingNumber(peak),
|
|
210
|
+
minRefreshRateSetting: settingNumber(min),
|
|
211
|
+
lowPower: power === "1",
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
async function readDisplayState(execute) {
|
|
216
|
+
const [display, peak, min, lowPower] = await Promise.all([
|
|
217
|
+
execute(["shell", "dumpsys", "display"]),
|
|
218
|
+
execute(["shell", "settings", "get", "system", "peak_refresh_rate"]),
|
|
219
|
+
execute(["shell", "settings", "get", "system", "min_refresh_rate"]),
|
|
220
|
+
execute(["shell", "settings", "get", "global", "low_power"]),
|
|
221
|
+
]);
|
|
222
|
+
return { ...parseActiveDisplayMode(display), ...parseRefreshRateSettings({ peak, min, lowPower }) };
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* The one place that answers "which adb binary". Every device lane resolves through this: an
|
|
227
|
+
* explicit THREENATIVE_ADB wins, then the three documented SDK roots in order, then the default
|
|
228
|
+
* layout. Lanes that must fail closed on a missing adb pass `onMissing` and get their own typed
|
|
229
|
+
* error instead of the PATH fallback.
|
|
230
|
+
*/
|
|
231
|
+
export function resolveAdbExecutable(environment = process.env, dependencies = {}) {
|
|
232
|
+
if (environment.THREENATIVE_ADB) return environment.THREENATIVE_ADB;
|
|
233
|
+
const sdk =
|
|
234
|
+
environment.THREENATIVE_ANDROID_SDK ??
|
|
235
|
+
environment.ANDROID_SDK_ROOT ??
|
|
236
|
+
environment.ANDROID_HOME ??
|
|
237
|
+
join(homedir(), "Android", "Sdk");
|
|
238
|
+
const candidate = join(sdk, "platform-tools", process.platform === "win32" ? "adb.exe" : "adb");
|
|
239
|
+
if ((dependencies.existsSyncImpl ?? existsSync)(candidate)) return candidate;
|
|
240
|
+
if (dependencies.onMissing) throw dependencies.onMissing(candidate);
|
|
241
|
+
return "adb";
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
function runAdb(serial, args, environment = process.env) {
|
|
245
|
+
const result = spawnSync(resolveAdbExecutable(environment), ["-s", serial, ...args], {
|
|
246
|
+
encoding: "utf8",
|
|
247
|
+
env: environment,
|
|
248
|
+
maxBuffer: 8 * 1024 * 1024,
|
|
249
|
+
timeout: 120_000,
|
|
250
|
+
});
|
|
251
|
+
if (result.error || result.status !== 0) {
|
|
252
|
+
const detail = result.stderr || result.stdout || result.error?.message || "unknown adb error";
|
|
253
|
+
throw new DevicePreflightError(
|
|
254
|
+
"TN_DEVICE_PREFLIGHT_ADB",
|
|
255
|
+
`${args.join(" ")} failed: ${String(detail).trim()}`,
|
|
256
|
+
{ serial, args, exitCode: result.status ?? 2 },
|
|
257
|
+
);
|
|
258
|
+
}
|
|
259
|
+
return String(result.stdout ?? "");
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
function normaliseOptions(options) {
|
|
263
|
+
if (!options || typeof options !== "object") {
|
|
264
|
+
throw new DevicePreflightError("TN_DEVICE_PREFLIGHT_OPTIONS", "options are required");
|
|
265
|
+
}
|
|
266
|
+
const { minBatteryPercent, requireDischarging, maxThermalStatus, allowOverride } = options;
|
|
267
|
+
if (
|
|
268
|
+
!Number.isFinite(minBatteryPercent) ||
|
|
269
|
+
minBatteryPercent < 0 ||
|
|
270
|
+
minBatteryPercent > 100 ||
|
|
271
|
+
typeof requireDischarging !== "boolean" ||
|
|
272
|
+
typeof maxThermalStatus !== "string" ||
|
|
273
|
+
typeof allowOverride !== "boolean"
|
|
274
|
+
) {
|
|
275
|
+
throw new DevicePreflightError(
|
|
276
|
+
"TN_DEVICE_PREFLIGHT_OPTIONS",
|
|
277
|
+
"expected minBatteryPercent, requireDischarging, maxThermalStatus and allowOverride",
|
|
278
|
+
);
|
|
279
|
+
}
|
|
280
|
+
// Opt-in, and only one caller sets it. See the emulator branch in assertDeviceReady for why a
|
|
281
|
+
// qualification lane must never pass this and a benchmark reasonably may.
|
|
282
|
+
const allowEmulator = options.allowEmulator ?? false;
|
|
283
|
+
if (typeof allowEmulator !== "boolean") {
|
|
284
|
+
throw new DevicePreflightError("TN_DEVICE_PREFLIGHT_OPTIONS", "allowEmulator must be a boolean");
|
|
285
|
+
}
|
|
286
|
+
// Capture of the panel state is unconditional; the *gate* is opt-in, so a cold-start or physics
|
|
287
|
+
// arm is unaffected while an fps arm cannot run on a panel it did not declare.
|
|
288
|
+
const requireRefreshHz = options.requireRefreshHz;
|
|
289
|
+
if (
|
|
290
|
+
requireRefreshHz !== undefined &&
|
|
291
|
+
(!Number.isInteger(requireRefreshHz) || requireRefreshHz <= 0 || requireRefreshHz > 1000)
|
|
292
|
+
) {
|
|
293
|
+
throw new DevicePreflightError(
|
|
294
|
+
"TN_DEVICE_PREFLIGHT_OPTIONS",
|
|
295
|
+
"requireRefreshHz must be a whole number of hertz between 1 and 1000",
|
|
296
|
+
);
|
|
297
|
+
}
|
|
298
|
+
const maximumThermal = thermalStatusFromValue(maxThermalStatus);
|
|
299
|
+
return {
|
|
300
|
+
allowEmulator,
|
|
301
|
+
minBatteryPercent,
|
|
302
|
+
requireDischarging,
|
|
303
|
+
maximumThermal,
|
|
304
|
+
allowOverride,
|
|
305
|
+
requireRefreshHz,
|
|
306
|
+
};
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
function conditionFailure(condition, threshold, observed) {
|
|
310
|
+
return { condition, threshold, observed };
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
// The Play Protect verifier dialog ("Android App Compatibility") appears on every `adb install`
|
|
314
|
+
// of a newly versioned APK, and its own "don't show again" is keyed to the APK rather than the
|
|
315
|
+
// device, so the next build asks again (Pixel 8, 2026-08-27). These three global settings turn
|
|
316
|
+
// verification off for installs and persist on the device across builds and reboots. The dialog
|
|
317
|
+
// is a modal that eats injected touches mid-run, so a suppression that silently failed is worse
|
|
318
|
+
// than none: the readback below fails closed with a named error.
|
|
319
|
+
const PLAY_PROTECT_INSTALL_SETTINGS = [
|
|
320
|
+
"package_verifier_enable",
|
|
321
|
+
"upload_apk_enable",
|
|
322
|
+
"verifier_verify_adb_installs",
|
|
323
|
+
];
|
|
324
|
+
|
|
325
|
+
export function suppressPlayProtectOnAdbInstalls(serial, dependencies = {}) {
|
|
326
|
+
if (typeof serial !== "string" || serial.length === 0) {
|
|
327
|
+
throw new DevicePreflightError("TN_DEVICE_PREFLIGHT_NO_DEVICE", "a device serial is required");
|
|
328
|
+
}
|
|
329
|
+
const execute = dependencies.adb ?? ((args) => runAdb(serial, args, dependencies.environment));
|
|
330
|
+
for (const setting of PLAY_PROTECT_INSTALL_SETTINGS) {
|
|
331
|
+
let observed;
|
|
332
|
+
try {
|
|
333
|
+
execute(["shell", "settings", "put", "global", setting, "0"]);
|
|
334
|
+
observed = String(execute(["shell", "settings", "get", "global", setting])).trim();
|
|
335
|
+
} catch (error) {
|
|
336
|
+
throw new DevicePreflightError(
|
|
337
|
+
"TN_DEVICE_PREFLIGHT_PLAY_PROTECT",
|
|
338
|
+
`could not disable adb-install verification for ${setting}: ${error?.message ?? error}`,
|
|
339
|
+
{ serial, setting },
|
|
340
|
+
);
|
|
341
|
+
}
|
|
342
|
+
if (observed !== "0") {
|
|
343
|
+
throw new DevicePreflightError(
|
|
344
|
+
"TN_DEVICE_PREFLIGHT_PLAY_PROTECT",
|
|
345
|
+
`settings put global ${setting} 0 did not take (observed '${observed}'); the Play Protect install dialog will appear`,
|
|
346
|
+
{ serial, setting, observed },
|
|
347
|
+
);
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
return [...PLAY_PROTECT_INSTALL_SETTINGS];
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
export async function assertDeviceReady(serial, options, dependencies = {}) {
|
|
354
|
+
if (typeof serial !== "string" || serial.length === 0) {
|
|
355
|
+
throw new DevicePreflightError("TN_DEVICE_PREFLIGHT_NO_DEVICE", "a device serial is required");
|
|
356
|
+
}
|
|
357
|
+
const configuration = normaliseOptions(options);
|
|
358
|
+
// An emulator cannot prove arm64, a real GPU driver, touch hardware, thermal behaviour or battery,
|
|
359
|
+
// so every *qualification* lane must refuse one — that is PRD-070's rule and it is the default here.
|
|
360
|
+
//
|
|
361
|
+
// A *performance regression canary* is a different question, and the answer was measured on
|
|
362
|
+
// 2026-08-17 rather than assumed: on the x86_64 emulator the V8-against-QuickJS ratio at the top
|
|
363
|
+
// rung falls from 12.1x to 2.4x, because swiftshader is a CPU rasteriser and software rendering
|
|
364
|
+
// swamps script time. Still far past any sane tolerance, so an engine revert is catchable there —
|
|
365
|
+
// but the absolute numbers are not performance evidence and must never be quoted as device figures.
|
|
366
|
+
// `allowEmulator` is how a caller says it wants the canary and accepts that.
|
|
367
|
+
if (/^emulator-/u.test(serial) && !configuration.allowEmulator) {
|
|
368
|
+
throw new DevicePreflightError(
|
|
369
|
+
"TN_DEVICE_PREFLIGHT_EMULATOR_BLOCKED",
|
|
370
|
+
`${serial} is an emulator; a physical Android device is required`,
|
|
371
|
+
{ serial },
|
|
372
|
+
);
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
const execute = dependencies.adb ?? ((args) => runAdb(serial, args, dependencies.environment));
|
|
376
|
+
const state = String(await execute(["get-state"])).trim();
|
|
377
|
+
if (state !== "device") {
|
|
378
|
+
throw new DevicePreflightError(
|
|
379
|
+
"TN_DEVICE_PREFLIGHT_NO_DEVICE",
|
|
380
|
+
`${serial} is not online (adb state: ${state || "missing"})`,
|
|
381
|
+
{ serial, observed: state || "missing" },
|
|
382
|
+
);
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
const battery = parseBatteryState(await execute(["shell", "dumpsys", "battery"]));
|
|
386
|
+
const thermal = parseThermalState(await execute(["shell", "dumpsys", "thermalservice"]));
|
|
387
|
+
const screen = parseScreenState(await execute(["shell", "dumpsys", "power"]));
|
|
388
|
+
const display = await readDisplayState(execute);
|
|
389
|
+
const failures = [];
|
|
390
|
+
if (battery.batteryPercent < configuration.minBatteryPercent) {
|
|
391
|
+
failures.push(
|
|
392
|
+
conditionFailure(
|
|
393
|
+
"battery",
|
|
394
|
+
`>= ${configuration.minBatteryPercent}%`,
|
|
395
|
+
`${battery.batteryPercent}%`,
|
|
396
|
+
),
|
|
397
|
+
);
|
|
398
|
+
}
|
|
399
|
+
if (configuration.requireDischarging && battery.charging) {
|
|
400
|
+
failures.push(conditionFailure("charging", "discharging", battery.chargingSource));
|
|
401
|
+
}
|
|
402
|
+
if (thermal.thermalStatusCode > configuration.maximumThermal.code) {
|
|
403
|
+
failures.push(
|
|
404
|
+
conditionFailure(
|
|
405
|
+
"thermal",
|
|
406
|
+
`<= ${configuration.maximumThermal.status}`,
|
|
407
|
+
thermal.thermalStatus,
|
|
408
|
+
),
|
|
409
|
+
);
|
|
410
|
+
}
|
|
411
|
+
if (!screen.screenOn) {
|
|
412
|
+
failures.push(conditionFailure("screen", "on", "off"));
|
|
413
|
+
}
|
|
414
|
+
if (configuration.requireRefreshHz !== undefined) {
|
|
415
|
+
if (display.activeRefreshHz !== configuration.requireRefreshHz) {
|
|
416
|
+
failures.push(
|
|
417
|
+
conditionFailure(
|
|
418
|
+
"refreshRate",
|
|
419
|
+
`${configuration.requireRefreshHz} Hz active`,
|
|
420
|
+
`${display.activeRefreshHz} Hz`,
|
|
421
|
+
),
|
|
422
|
+
);
|
|
423
|
+
}
|
|
424
|
+
// Battery Saver clamps the panel's mode range, so a declared rate can be voted for and never
|
|
425
|
+
// applied. It is a separate cause from the Smooth Display setting and both have been observed.
|
|
426
|
+
if (display.lowPower) {
|
|
427
|
+
failures.push(conditionFailure("lowPower", "off", "on"));
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
if (failures.length > 0 && !configuration.allowOverride) {
|
|
432
|
+
const detail = failures
|
|
433
|
+
.map((failure) => `${failure.condition}: expected ${failure.threshold}, observed ${failure.observed}`)
|
|
434
|
+
.join("; ");
|
|
435
|
+
throw new DevicePreflightError("TN_DEVICE_PREFLIGHT_CONDITION_FAILED", detail, {
|
|
436
|
+
serial,
|
|
437
|
+
failures,
|
|
438
|
+
});
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
return {
|
|
442
|
+
serial,
|
|
443
|
+
batteryPercent: battery.batteryPercent,
|
|
444
|
+
charging: battery.charging,
|
|
445
|
+
chargingSource: battery.chargingSource,
|
|
446
|
+
thermalStatus: thermal.thermalStatus,
|
|
447
|
+
thermalStatusCode: thermal.thermalStatusCode,
|
|
448
|
+
screenOn: screen.screenOn,
|
|
449
|
+
activeRefreshHz: display.activeRefreshHz,
|
|
450
|
+
supportedRefreshHz: display.supportedRefreshHz,
|
|
451
|
+
peakRefreshRateSetting: display.peakRefreshRateSetting,
|
|
452
|
+
minRefreshRateSetting: display.minRefreshRateSetting,
|
|
453
|
+
lowPower: display.lowPower,
|
|
454
|
+
provisional: failures.map((failure) => failure.condition),
|
|
455
|
+
};
|
|
456
|
+
}
|