Skip to content

Commit 33ecb9c

Browse files
test(daemon): pin perf sampler selection to the facet tag, not the platform
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
1 parent 16b777e commit 33ecb9c

1 file changed

Lines changed: 35 additions & 7 deletions

File tree

‎src/daemon/__tests__/perf-plugin-routing-parity.test.ts‎

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import {
2424
} from '../../__tests__/test-utils/index.ts';
2525
import { getPlugin } from '../../core/platform-plugin/plugin.ts';
2626
import { registerBuiltinPlatformPlugins } from '../../core/interactors/register-builtins.ts';
27-
import { buildPerfResponseData } from '../handlers/session-perf.ts';
27+
import { buildPerfResponseData, type PerfMetricsSamplerTag } from '../handlers/session-perf.ts';
2828
import { PERF_UNAVAILABLE_REASON } from '../handlers/session-startup-metrics.ts';
2929

3030
// Phase 3 step b.3 (issue #974) parity gate for the daemon perf facet. The
@@ -167,12 +167,9 @@ test('buildPerfResponseData routes the support gate through the perf facet', asy
167167
// Shipped-path routing proof for the sampling body (issue #1188). The support-gate test
168168
// above never reaches sampler selection — it uses no `appBundleId`, so execution returns
169169
// through `applyMissingAppPerfMetrics` before `resolvePerfMetricsSampler`. WITH an app
170-
// bundle, execution clears that guard and hits the facet-owned sampler selection. The
171-
// Android sampler is the ONLY arm that threads `options.androidAdb`, so a scripted adb
172-
// executor is invoked exactly when the Android sampler was selected AND run through the
173-
// shipped code path. Breaking the facet lookup (Android returning a non-`android` tag, or
174-
// the resolver yielding no sampler) skips the Android sampler and fails this test — so the
175-
// suite now covers the dispatch line itself, not only registry parity.
170+
// bundle, execution clears that guard and hits the sampler selection. The Android sampler
171+
// is the ONLY arm that threads `options.androidAdb`, so a scripted adb executor is invoked
172+
// exactly when the Android sampler was selected AND run through the shipped code path.
176173
const SAMPLED_ADB_REASON = 'scripted adb unavailable';
177174
function makeThrowingAdb(): { adb: AndroidAdbExecutor; calls: () => number } {
178175
let calls = 0;
@@ -209,6 +206,37 @@ test('buildPerfResponseData dispatches the Android sampler selected by the facet
209206
}
210207
});
211208

209+
// Facet-vs-platform proof (re-review, issue #1188): the test above cannot distinguish the
210+
// facet lookup from the deleted `device.platform === 'android'` branch, because on a real
211+
// Android device both select the same Android sampler with the same options. Here the
212+
// device is an Apple simulator but its plugin's `metricsSamplerTag` is overridden to
213+
// `'android'`, so ONLY a facet-driven selection routes it to the Android sampler (the
214+
// scripted adb fires). The former platform branch keyed on `device.platform`, so restoring
215+
// it keeps the Apple device on the Apple sampler and this assertion fails — pinning the
216+
// shipped selection to `metricsSamplerTag`, not the device platform.
217+
test('buildPerfResponseData selects the sampler by the facet tag, not the device platform', async () => {
218+
const perf = getPlugin('apple').perf;
219+
assert.ok(perf, 'apple plugin exposes the perf facet');
220+
const mutablePerf = perf as { metricsSamplerTag: (device: DeviceInfo) => PerfMetricsSamplerTag };
221+
const originalTag = mutablePerf.metricsSamplerTag;
222+
mutablePerf.metricsSamplerTag = () => 'android';
223+
try {
224+
const { adb, calls } = makeThrowingAdb();
225+
const session = makeSession('perf-facet-tag', {
226+
device: IOS_SIMULATOR,
227+
appBundleId: 'com.example.app',
228+
});
229+
const data = await buildPerfResponseData(session, { androidAdb: adb });
230+
231+
assert.ok(calls() > 0, 'facet tag routed the Apple device to the Android sampler');
232+
const cpu = data.metrics.cpu as { available?: boolean; reason?: string };
233+
assert.equal(cpu.available, false, 'cpu was sampled through the facet-selected sampler');
234+
assert.equal(cpu.reason, SAMPLED_ADB_REASON, 'cpu carries the scripted adb failure');
235+
} finally {
236+
mutablePerf.metricsSamplerTag = originalTag;
237+
}
238+
});
239+
212240
// The missing-app guard precedes sampler selection, so without an `appBundleId` the facet
213241
// sampler is never consulted and the scripted adb stays untouched.
214242
test('buildPerfResponseData consults the sampler only past the missing-app guard', async () => {

0 commit comments

Comments
 (0)