Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

feat(mobile): measure first open, new accounts, testers

tonyd4cf56e5f7f7
diff --git a/mobile/ios/Runner/AppDelegate.swift b/mobile/ios/Runner/AppDelegate.swift
index 27dd9d4cc..068807a00 100644
--- a/mobile/ios/Runner/AppDelegate.swift
+++ b/mobile/ios/Runner/AppDelegate.swift
@@ -19,6 +19,7 @@ import UserNotifications
func didInitializeImplicitFlutterEngine(_ engineBridge: FlutterImplicitEngineBridge) {
GeneratedPluginRegistrant.register(with: engineBridge.pluginRegistry)
DeviceNameChannel.register(with: engineBridge.pluginRegistry)
+ InstallSourceChannel.register(with: engineBridge.pluginRegistry)
ClipboardImageChannel.register(with: engineBridge.pluginRegistry)
SignInPageChannel.register(with: engineBridge.pluginRegistry)
HandoffChannel.register(with: engineBridge.pluginRegistry)
@@ -141,6 +142,22 @@ enum ClipboardImageChannel {
}
}
+/// `harness/install_source` — whether this build came from the App Store, for the measurement
+/// context's channel (Dart: `lib/core/install_source.dart`): a tester's counts are kept apart. An App
+/// Store build's receipt is named `receipt`; TestFlight's, and any build Xcode installed, `sandboxReceipt`.
+/// Only the file's name is read, never the receipt, and only `store` or `sandbox` goes back.
+enum InstallSourceChannel {
+ static func register(with registry: FlutterPluginRegistry) {
+ guard let messenger = registry.registrar(forPlugin: "HarnessInstallSource")?.messenger() else { return }
+ let channel = FlutterMethodChannel(name: "harness/install_source", binaryMessenger: messenger)
+ channel.setMethodCallHandler { call, result in
+ guard call.method == "source" else { result(FlutterMethodNotImplemented); return }
+ let sandbox = Bundle.main.appStoreReceiptURL?.lastPathComponent == "sandboxReceipt"
+ result(sandbox ? "sandbox" : "store")
+ }
+ }
+}
+
/// `harness/device_name` — what this phone is called, for the far side's "took control" banner
/// (Dart: `lib/core/device_name.dart`). `name` is the user's own name for the device where iOS still
/// hands it out (before iOS 16, or with Apple's user-assigned-device-name entitlement); otherwise it
diff --git a/mobile/lib/core/install_source.dart b/mobile/lib/core/install_source.dart
new file mode 100644
index 000000000..8f0cb83c1
--- /dev/null
+++ b/mobile/lib/core/install_source.dart
@@ -0,0 +1,38 @@
+import 'package:flutter/foundation.dart';
+import 'package:flutter/services.dart';
+
+/// Where this build was installed from, as far as the phone can tell — for its measurement context's
+/// channel ([measurementChannel]), so a tester's counts are kept apart from the store's.
+///
+/// Over `harness/install_source` (ios/Runner/AppDelegate.swift): an App Store build's receipt is named
+/// `receipt`, any other build's — TestFlight, one Xcode installed — `sandboxReceipt`. iOS only: on
+/// Android a Play internal test installs exactly as the store release it becomes, so it reads as the
+/// store, as does anything that does not answer.
+enum InstallSource { store, sandbox }
+
+@visibleForTesting
+const installSourceChannel = MethodChannel('harness/install_source');
+
+Future<InstallSource> installSource() async {
+ if (kIsWeb || defaultTargetPlatform != TargetPlatform.iOS) {
+ return InstallSource.store;
+ }
+ try {
+ final source = await installSourceChannel.invokeMethod<String>('source');
+ return source == 'sandbox' ? InstallSource.sandbox : InstallSource.store;
+ } catch (_) {
+ return InstallSource.store;
+ }
+}
+
+/// The measurement context's channel (backend `ProductUsageContext.channel`): a debug build is
+/// `development`, which the backend does not keep; a release build from anywhere but the store is
+/// `testing`, which it keeps apart; the store's is `production`.
+String measurementChannel({
+ required bool release,
+ required InstallSource source,
+}) => !release
+ ? 'development'
+ : source == InstallSource.sandbox
+ ? 'testing'
+ : 'production';
diff --git a/mobile/lib/core/models.dart b/mobile/lib/core/models.dart
index 89febe194..557517572 100644
--- a/mobile/lib/core/models.dart
+++ b/mobile/lib/core/models.dart
@@ -15,11 +15,16 @@ class CurrentUserProfile {
final String email;
final String? avatarUrl;
+ /// When the account's record was made: its first Harness sign-in anywhere. Null from a backend that
+ /// does not say, or says something unreadable.
+ final DateTime? createdAt;
+
const CurrentUserProfile({
this.id,
this.name,
required this.email,
this.avatarUrl,
+ this.createdAt,
});
factory CurrentUserProfile.fromMe(Map<String, dynamic> response) {
@@ -34,6 +39,7 @@ class CurrentUserProfile {
}
final rawName = user['name'];
final rawAvatar = response['avatarUrl'];
+ final rawCreated = user['createdAt'];
return CurrentUserProfile(
id: user['id'] is String ? user['id'] as String : null,
name: rawName is String && rawName.trim().isNotEmpty
@@ -43,6 +49,7 @@ class CurrentUserProfile {
avatarUrl: rawAvatar is String && rawAvatar.trim().isNotEmpty
? rawAvatar.trim()
: null,
+ createdAt: rawCreated is String ? DateTime.tryParse(rawCreated) : null,
);
}
diff --git a/mobile/lib/measurement/onboarding_measurement.dart b/mobile/lib/measurement/onboarding_measurement.dart
index 89cb2654f..63027271e 100644
--- a/mobile/lib/measurement/onboarding_measurement.dart
+++ b/mobile/lib/measurement/onboarding_measurement.dart
@@ -1,3 +1,6 @@
+import 'dart:async';
+
+import '../core/local_key_value_store.dart';
import 'product_measurement.dart';
/// Mirrors `PRODUCT_USAGE_ONBOARDING_MARKS` in backend/src/types/productUsage.ts, with the steps
@@ -9,6 +12,9 @@ enum OnboardingMark {
/// A sign-in went through on another button than the first one tried in this app run.
switched('switched', {ProductFlow.signIn}),
+ /// The sign-in created the account: minutes old, its first Harness sign-in anywhere.
+ newAccount('new_account', {ProductFlow.signIn}),
+
/// The step started in an app run that did not sign in: reopened mid-step, not a new start.
resumed('resumed', {
ProductFlow.computerWait,
@@ -22,6 +28,10 @@ enum OnboardingMark {
/// The computer's address sent from the phone (share sheet).
linkShared('link_shared', {ProductFlow.computerWait}),
+ /// The computer the wait was for turned up after the app had left the wait: a launch later found it
+ /// on the account. That wait never saw it, so it has no `ok` of its own.
+ joinedAfterLeaving('joined_after_leaving', {ProductFlow.computerWait}),
+
/// The joining computer wants its password before its session can open.
blockedPassword('blocked_password', {ProductFlow.sessionOpen}),
@@ -45,11 +55,24 @@ enum OnboardingMark {
/// choice reads off. A step that ends before then is never counted; the sign-in itself, which
/// always does, is the exception ([signedIn]).
class OnboardingMeasurement {
- OnboardingMeasurement(this._usage) {
+ OnboardingMeasurement(this._usage, {this.storage}) {
_usage.addListener(_admit);
}
final ProductMeasurement _usage;
+
+ /// Where a wait for a computer is remembered across launches ([waitingKey]): a yes or nothing, never
+ /// sent. None (a test, the sample): nothing outlives the run.
+ final LocalKeyValueStore? storage;
+
+ /// Set while a wait for a computer is open, cleared once it ends or the account signs out. A launch
+ /// that finds it set and a computer on the account counts [OnboardingMark.joinedAfterLeaving].
+ static const waitingKey = 'onboarding_computer_wait_open';
+
+ /// How old an account may be at its sign-in to count as made by it ([accountCreated]). Its record is
+ /// written by the first Harness sign-in anywhere, seconds before the profile is read; the margin is
+ /// for a slow sign-in and for this phone's clock.
+ static const newAccountWithin = Duration(minutes: 15);
final _waiting = <ProductFlow, ({Stopwatch watch, bool resumed})>{};
final _running = <ProductFlow, void Function(ProductOutcome)>{};
final _seen = <ProductFlow>{};
@@ -91,6 +114,7 @@ class OnboardingMeasurement {
/// [OnboardingMark.resumed] as well.
void start(ProductFlow flow) {
if (!_seen.add(flow)) return;
+ if (flow == ProductFlow.computerWait) _rememberWaiting(true);
_waiting[flow] = (
watch: Stopwatch()..start(),
resumed: !_signedInThisRun && OnboardingMark.resumed.flows.contains(flow),
@@ -100,10 +124,61 @@ class OnboardingMeasurement {
/// Ends [flow] with [outcome] if it is running; otherwise nothing.
void end(ProductFlow flow, ProductOutcome outcome) {
+ if (flow == ProductFlow.computerWait && _seen.contains(flow)) {
+ _rememberWaiting(false);
+ }
_waiting.remove(flow);
_running.remove(flow)?.call(outcome);
}
+ /// The account's profile, read after a sign-in in this run: one created by it ([newAccountWithin])
+ /// counts [OnboardingMark.newAccount]. [createdAt] is the account record's; null says nothing.
+ void accountCreated(DateTime? createdAt, {DateTime? now}) {
+ if (!_signedInThisRun || createdAt == null) return;
+ final age = (now ?? DateTime.now()).difference(createdAt);
+ // A record from the future is this phone's clock running behind, by as much as the margin allows.
+ if (age > newAccountWithin || age < -newAccountWithin) return;
+ if (!_marked.add((ProductFlow.signIn, OnboardingMark.newAccount))) return;
+ _marks.add((ProductFlow.signIn, OnboardingMark.newAccount));
+ _admit();
+ }
+
+ /// The account's computers have been read and it has at least one. A wait for a computer that a
+ /// launch before this one left open ([waitingKey]) ended there: counted once as
+ /// [OnboardingMark.joinedAfterLeaving]. Nothing once this sign-in has a wait of its own, open or
+ /// ended: it saw the computer itself (`ok`), or is still looking — asked again after the read below
+ /// too, which a wait may end meanwhile.
+ Future<void> computersSeen() async {
+ final storage = this.storage;
+ if (storage == null || _seen.contains(ProductFlow.computerWait)) return;
+ String? open;
+ try {
+ open = await storage.read(waitingKey);
+ } catch (_) {
+ return;
+ }
+ if (open == null || _seen.contains(ProductFlow.computerWait)) return;
+ _rememberWaiting(false);
+ if (!_marked.add((
+ ProductFlow.computerWait,
+ OnboardingMark.joinedAfterLeaving,
+ ))) {
+ return;
+ }
+ _marks.add((ProductFlow.computerWait, OnboardingMark.joinedAfterLeaving));
+ _admit();
+ }
+
+ /// Best effort: a flag not written costs one [OnboardingMark.joinedAfterLeaving] at most.
+ void _rememberWaiting(bool open) {
+ final storage = this.storage;
+ if (storage == null) return;
+ unawaited(
+ (open ? storage.write(waitingKey, '1') : storage.delete(waitingKey))
+ .catchError((Object _) {}),
+ );
+ }
+
/// Counts [what] of [flow], once per sign-in; build methods may call it on every frame. Only for
/// a step this sign-in has [start]ed — the same screens outside the onboarding count nothing — and
/// one [what] is listed under.
@@ -131,6 +206,8 @@ class OnboardingMeasurement {
for (final flow in [..._running.keys]) {
end(flow, ProductOutcome.cancelled);
}
+ // A wait this account left open is not the next account's.
+ _rememberWaiting(false);
_clearWaiting();
_seen.clear();
_marked.clear();
diff --git a/mobile/lib/measurement/product_measurement.dart b/mobile/lib/measurement/product_measurement.dart
index a970c22a3..4a7ab015a 100644
--- a/mobile/lib/measurement/product_measurement.dart
+++ b/mobile/lib/measurement/product_measurement.dart
@@ -96,6 +96,11 @@ class ProductMeasurement extends ChangeNotifier {
/// Done once the kept choice is read: until then nothing is counted signed out.
late final Future<void> setupChoiceReady;
+ /// Kept in [setupStorage] once this install's first launch has been counted ([noteFirstOpen]): a yes,
+ /// never sent.
+ static const firstOpenKey = 'product_first_open_counted';
+ bool _firstOpenPending = false;
+
/// Done once the latest choice is written.
Future<void> get setupChoiceSaved => _setupSave ?? Future<void>.value();
Future<void>? _setupSave;
@@ -229,6 +234,7 @@ class ProductMeasurement extends ChangeNotifier {
_clear('ui');
if (scope != null && automatic) unawaited(flush());
_schedule();
+ _countFirstOpen();
}
/// A saved switch applies immediately in this app, even while a report is in flight. [explicit]: the
@@ -265,9 +271,56 @@ class ProductMeasurement extends ChangeNotifier {
_clear('setup');
_setup.clear();
}
+ _countFirstOpen();
notifyListeners();
}
+ /// This install's first launch, counted once: `setup_phone_first_open` signed out — the usual first
+ /// launch, after a store's download — or `ui_phone_first_open` on an account that shares, if it opens
+ /// signed in. The step between the download and a sign-in, which nothing else counts.
+ ///
+ /// The flag is written before anything is counted, so a first launch that may not count it (sharing
+ /// off) never has a later launch counted as the first. Nothing without [setupStorage] (a test, the
+ /// sample), and nothing on the web.
+ Future<void> noteFirstOpen() async {
+ final storage = setupStorage;
+ if (storage == null || surface != 'phone') return;
+ await setupChoiceReady;
+ if (_disposed) return;
+ try {
+ if (await storage.read(firstOpenKey) != null) return;
+ await storage.write(firstOpenKey, '1');
+ } catch (_) {
+ return;
+ }
+ if (_disposed) return;
+ _firstOpenPending = true;
+ _countFirstOpen();
+ }
+
+ /// [noteFirstOpen]'s count, on whichever channel may take it first; dropped once neither ever will.
+ void _countFirstOpen() {
+ if (!_firstOpenPending || _disposed) return;
+ final channel = _account == null && _setupAllowed
+ ? 'setup'
+ : sharing
+ ? 'ui'
+ : null;
+ if (channel == null) {
+ // Off signed out, or the account's choice read off: this launch is not counted, now or later.
+ if (_account == null ? choiceLoaded && !signedOutSharing : declined) {
+ _firstOpenPending = false;
+ }
+ return;
+ }
+ _firstOpenPending = false;
+ _count(
+ channel,
+ _now().toUtc().toIso8601String().substring(0, 10),
+ '${channel}_${surface}_first_open',
+ );
+ }
+
void _clear(String channel) {
_days.removeWhere((key, _) => key.startsWith('$channel:'));
_pending.remove(channel);
diff --git a/mobile/lib/state/app_state.dart b/mobile/lib/state/app_state.dart
index d82b2e131..5616d804a 100644
--- a/mobile/lib/state/app_state.dart
+++ b/mobile/lib/state/app_state.dart
@@ -1,4 +1,5 @@
import '../core/app_version.dart';
+import '../core/install_source.dart';
import '../core/test_run.dart';
import '../measurement/launch_measurement.dart';
import '../measurement/onboarding_measurement.dart';
@@ -440,8 +441,12 @@ class AppNotifier extends ChangeNotifier {
late ApiClient api;
late final ProductMeasurement productMeasurement;
- /// The phone's onboarding steps after sign-in, counted through [productMeasurement].
- late final onboardingMeasurement = OnboardingMeasurement(productMeasurement);
+ /// The phone's onboarding steps after sign-in, counted through [productMeasurement]. A wait for a
+ /// computer is remembered in the app's own store, for the launch that finds the computer.
+ late final onboardingMeasurement = OnboardingMeasurement(
+ productMeasurement,
+ storage: _paneLayout?.storage,
+ );
final _measurementConnections = <String, void Function(ProductOutcome)>{};
/// Each machine's dials, the person's (`connect_user`) apart from the app's own (`connect_auto`).
@@ -1301,6 +1306,8 @@ class AppNotifier extends ChangeNotifier {
// a launch of this app.
if (!kUnderTest && paneLayoutStore != null) {
launchMeasurement = LaunchMeasurement(this.productMeasurement);
+ // The install's first launch, once: the step between a store's download and a sign-in.
+ unawaited(this.productMeasurement.noteFirstOpen());
}
addListener(_syncProductMeasurement);
unawaited(_measurementContext());
@@ -1316,6 +1323,7 @@ class AppNotifier extends ChangeNotifier {
try {
release = await runningAppVersion();
} catch (_) {}
+ final source = await installSource();
if (_disposed) return;
productMeasurement.context = {
'surface': 'phone',
@@ -1328,7 +1336,8 @@ class AppNotifier extends ChangeNotifier {
TargetPlatform.android => 'android',
_ => 'other',
},
- 'channel': kReleaseMode ? 'production' : 'development',
+ // A tester's build (TestFlight) is `testing`: the backend keeps its totals apart.
+ 'channel': measurementChannel(release: kReleaseMode, source: source),
};
}
@@ -1867,6 +1876,8 @@ class AppNotifier extends ChangeNotifier {
}
if (me != null) {
currentUser = CurrentUserProfile.fromMe(me);
+ // Made by a sign-in in this run, or coming back to it.
+ onboardingMeasurement.accountCreated(currentUser?.createdAt);
// A sign-in by hand: the device log keeps which account it was made to.
if (currentUser?.id case final id?) {
unawaited(_deviceLog?.signedInAs(id));
@@ -2710,6 +2721,8 @@ class AppNotifier extends ChangeNotifier {
machines = list
.where((machine) => machine.authMode == MachineAuthMode.remote)
.toList();
+ // A wait for a computer that an earlier launch left open, with the computer now here.
+ if (machines.isNotEmpty) unawaited(onboardingMeasurement.computersSeen());
final visible = machines.map((machine) => machine.machineId).toSet();
// A code scanned for a machine this account does not have can never be
// spent: the pairing screen waits for that machine to show up locked.
diff --git a/mobile/test/app_state_flow_measurement_test.dart b/mobile/test/app_state_flow_measurement_test.dart
index 1c4e55def..39ec370d7 100644
--- a/mobile/test/app_state_flow_measurement_test.dart
+++ b/mobile/test/app_state_flow_measurement_test.dart
@@ -1,5 +1,6 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:harness_mobile/core/models.dart';
+import 'package:harness_mobile/measurement/onboarding_measurement.dart';
import 'package:harness_mobile/measurement/product_measurement.dart';
import 'viewer_app_fixture.dart';
@@ -19,8 +20,15 @@ void main() {
});
/// A signed-in phone whose account shares product usage.
- Future<ViewerRig> sharing(Map<String, List<String>> agents) async {
- final rig = await signedInWith(agents, productMeasurement: usage);
+ Future<ViewerRig> sharing(
+ Map<String, List<String>> agents, {
+ MemoryKeyValueStore? storage,
+ }) async {
+ final rig = await signedInWith(
+ agents,
+ storage: storage,
+ productMeasurement: usage,
+ );
addTearDown(rig.app.dispose);
await usage.flush();
expect(usage.sharing, isTrue);
@@ -217,4 +225,88 @@ void main() {
},
);
});
+
+ group('what the account itself tells the onboarding', () {
+ test(
+ 'a wait an earlier launch left open, its computer now on the account',
+ () async {
+ final storage = MemoryKeyValueStore()
+ ..values[OnboardingMeasurement.waitingKey] = '1';
+ final rig = await sharing({'m': []}, storage: storage);
+ expect(await sent(rig, 'computer_wait'), {
+ 'ui_phone_computer_wait_joined_after_leaving': 1,
+ });
+ expect(
+ storage.values,
+ isNot(contains(OnboardingMeasurement.waitingKey)),
+ );
+
+ await rig.app.refreshMachines();
+ await settle();
+ expect(await sent(rig, 'computer_wait'), {
+ 'ui_phone_computer_wait_joined_after_leaving': 1,
+ }, reason: 'once');
+ },
+ );
+
+ test('a wait left open, still with no computer, is kept', () async {
+ final storage = MemoryKeyValueStore()
+ ..values[OnboardingMeasurement.waitingKey] = '1';
+ final rig = await sharing({}, storage: storage);
+ expect(await sent(rig, 'computer_wait'), isEmpty);
+ expect(storage.values[OnboardingMeasurement.waitingKey], '1');
+ });
+
+ Future<ViewerRig> signingIn(DateTime createdAt) async {
+ final rig = viewerApp(signedIn: false, productMeasurement: usage);
+ addTearDown(rig.app.dispose);
+ rig.api.profile = {
+ 'user': {
+ 'id': 'u1',
+ 'email': '[email protected]',
+ 'createdAt': createdAt.toUtc().toIso8601String(),
+ },
+ };
+ await rig.app.signInWithCode(email: '[email protected]', code: '1234');
+ await settle();
+ return rig;
+ }
+
+ test('a sign-in that made its account says so', () async {
+ final rig = await signingIn(
+ DateTime.now().subtract(const Duration(seconds: 5)),
+ );
+ expect(
+ await sent(rig, 'sign_in'),
+ allOf([
+ containsPair('ui_phone_sign_in_ok', 1),
+ containsPair('ui_phone_sign_in_new_account', 1),
+ ]),
+ );
+ });
+
+ test('a sign-in to an older account is a return', () async {
+ final rig = await signingIn(
+ DateTime.now().subtract(const Duration(days: 30)),
+ );
+ final signIn = await sent(rig, 'sign_in');
+ expect(signIn, containsPair('ui_phone_sign_in_ok', 1));
+ expect(signIn, isNot(contains('ui_phone_sign_in_new_account')));
+ });
+
+ test('a launch already signed in made no account', () async {
+ final rig = viewerApp(productMeasurement: usage);
+ addTearDown(rig.app.dispose);
+ rig.api.profile = {
+ 'user': {
+ 'id': 'u1',
+ 'email': '[email protected]',
+ 'createdAt': DateTime.now().toUtc().toIso8601String(),
+ },
+ };
+ await rig.app.bootstrap();
+ await settle();
+ expect(await sent(rig, 'sign_in'), isEmpty);
+ });
+ });
}
diff --git a/mobile/test/core/install_source_test.dart b/mobile/test/core/install_source_test.dart
new file mode 100644
index 000000000..75e63aa9f
--- /dev/null
+++ b/mobile/test/core/install_source_test.dart
@@ -0,0 +1,74 @@
+import 'package:flutter/foundation.dart';
+import 'package:flutter/services.dart';
+import 'package:flutter_test/flutter_test.dart';
+import 'package:harness_mobile/core/install_source.dart';
+
+void main() {
+ TestWidgetsFlutterBinding.ensureInitialized();
+ final messenger =
+ TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger;
+ late List<String> calls;
+
+ void answer(Object? Function() reply) {
+ calls = [];
+ messenger.setMockMethodCallHandler(installSourceChannel, (call) async {
+ calls.add(call.method);
+ return reply();
+ });
+ }
+
+ tearDown(() {
+ messenger.setMockMethodCallHandler(installSourceChannel, null);
+ debugDefaultTargetPlatformOverride = null;
+ });
+
+ group('installSource', () {
+ setUp(() => debugDefaultTargetPlatformOverride = TargetPlatform.iOS);
+
+ test('a sandbox receipt is a tester\'s build', () async {
+ answer(() => 'sandbox');
+ expect(await installSource(), InstallSource.sandbox);
+ expect(calls, ['source']);
+ });
+
+ test('the store\'s receipt is the store', () async {
+ answer(() => 'store');
+ expect(await installSource(), InstallSource.store);
+ });
+
+ test(
+ 'an unknown answer, no answer or a failure reads as the store',
+ () async {
+ answer(() => 'weird');
+ expect(await installSource(), InstallSource.store);
+ answer(() => null);
+ expect(await installSource(), InstallSource.store);
+ answer(() => throw PlatformException(code: 'broken'));
+ expect(await installSource(), InstallSource.store);
+ messenger.setMockMethodCallHandler(installSourceChannel, null);
+ expect(await installSource(), InstallSource.store);
+ },
+ );
+
+ test('Android is never asked: it reads as the store', () async {
+ debugDefaultTargetPlatformOverride = TargetPlatform.android;
+ answer(() => 'sandbox');
+ expect(await installSource(), InstallSource.store);
+ expect(calls, isEmpty);
+ });
+ });
+
+ test('measurementChannel: debug, a tester\'s release, the store\'s', () {
+ for (final source in InstallSource.values) {
+ expect(measurementChannel(release: false, source: source), 'development');
+ }
+ expect(
+ measurementChannel(release: true, source: InstallSource.sandbox),
+ 'testing',
+ );
+ expect(
+ measurementChannel(release: true, source: InstallSource.store),
+ 'production',
+ );
+ });
+}
diff --git a/mobile/test/core_units_test.dart b/mobile/test/core_units_test.dart
index 51fe3d9fd..62be698ce 100644
--- a/mobile/test/core_units_test.dart
+++ b/mobile/test/core_units_test.dart
@@ -410,6 +410,25 @@ void main() {
expect(const CurrentUserProfile(email: ' ').initials, '?');
});
+ test('when the account was made, if the backend says readably', () {
+ CurrentUserProfile made(Object? createdAt) => CurrentUserProfile.fromMe({
+ 'user': {'email': '[email protected]', 'createdAt': createdAt},
+ });
+ expect(
+ made('2026-10-19T08:30:00.000Z').createdAt,
+ DateTime.utc(2026, 10, 19, 8, 30),
+ );
+ expect(made(null).createdAt, isNull);
+ expect(made('yesterday').createdAt, isNull);
+ expect(made(1760862600000).createdAt, isNull);
+ expect(
+ CurrentUserProfile.fromMe({
+ 'user': {'email': '[email protected]'},
+ }).createdAt,
+ isNull,
+ );
+ });
+
test('a machine with no name is called by its id', () {
const long = Machine(
machineId: 'abcdef0123456789',
diff --git a/mobile/test/onboarding_measurement_test.dart b/mobile/test/onboarding_measurement_test.dart
index 30912953c..8918cd907 100644
--- a/mobile/test/onboarding_measurement_test.dart
+++ b/mobile/test/onboarding_measurement_test.dart
@@ -6,6 +6,8 @@ import 'package:flutter_test/flutter_test.dart';
import 'package:harness_mobile/measurement/onboarding_measurement.dart';
import 'package:harness_mobile/measurement/product_measurement.dart';
+import 'voice_fakes.dart' show MemoryKeyValueStore;
+
void main() {
late ProductMeasurement usage;
late OnboardingMeasurement onboarding;
@@ -340,6 +342,142 @@ void main() {
);
});
+ group('a wait for a computer the app left', () {
+ late MemoryKeyValueStore store;
+
+ setUp(() => store = MemoryKeyValueStore());
+
+ /// One launch of the app over [store]: its own onboarding, the account's usage shared with it.
+ OnboardingMeasurement launch() {
+ final run = OnboardingMeasurement(usage, storage: store);
+ addTearDown(run.dispose);
+ return run;
+ }
+
+ Future<void> settled() => Future<void>.delayed(Duration.zero);
+
+ test(
+ 'is remembered, and the launch that finds the computer counts it once',
+ () async {
+ await signIn();
+ final first = launch()
+ ..signedIn(ProductFlow.signInGoogle, Duration.zero)
+ ..start(ProductFlow.computerWait);
+ await settled();
+ expect(store.values[OnboardingMeasurement.waitingKey], '1');
+ first.dispose(); // The app is closed with the wait still open.
+
+ final next = launch();
+ await next.computersSeen();
+ await next.computersSeen();
+ await usage.flush();
+ expect(counters('ui_phone_computer_wait_joined_after_leaving'), {
+ 'ui_phone_computer_wait_joined_after_leaving': 1,
+ });
+ // That wait is over: no `ok` of its own, and nothing left to count.
+ expect(counters('ui_phone_computer_wait_ok'), isEmpty);
+ await settled();
+ expect(store.values, isEmpty);
+ },
+ );
+
+ test('that ended in its own launch leaves nothing for the next', () async {
+ await signIn();
+ launch()
+ ..signedIn(ProductFlow.signInGoogle, Duration.zero)
+ ..start(ProductFlow.computerWait)
+ ..end(ProductFlow.computerWait, ProductOutcome.ok);
+ await settled();
+ expect(store.values, isEmpty);
+
+ await launch().computersSeen();
+ await usage.flush();
+ expect(counters('ui_phone_computer_wait_joined_after_leaving'), isEmpty);
+ });
+
+ test('is not counted again by the launch that has a wait of its own', () async {
+ await signIn();
+ launch().start(ProductFlow.computerWait);
+ await settled();
+
+ // Reopened mid-wait: this launch waits too, and sees the computer itself.
+ final next = launch()..start(ProductFlow.computerWait);
+ await settled();
+ expect(store.values[OnboardingMeasurement.waitingKey], '1');
+ await next.computersSeen();
+ next.end(ProductFlow.computerWait, ProductOutcome.ok);
+ await next.computersSeen();
+ await usage.flush();
+ expect(counters('ui_phone_computer_wait_joined_after_leaving'), isEmpty);
+ expect(counters('ui_phone_computer_wait_ok'), {
+ 'ui_phone_computer_wait_ok': 1,
+ });
+ await settled();
+ expect(store.values, isEmpty);
+ });
+
+ test('is forgotten when the account signs out', () async {
+ await signIn();
+ final run = launch()..start(ProductFlow.computerWait);
+ await settled();
+ run.reset();
+ await settled();
+ expect(store.values, isEmpty);
+ });
+
+ test('counts nothing for an account that does not share', () async {
+ choice = {'enabled': false, 'revision': 0};
+ await signIn();
+ store.values[OnboardingMeasurement.waitingKey] = '1';
+ await launch().computersSeen();
+ await usage.flush();
+ expect(reports, isEmpty);
+ });
+ });
+
+ group('a sign-in that made its account', () {
+ final now = DateTime.utc(2026, 10, 20, 9);
+
+ test('counts new_account, once', () async {
+ await signIn();
+ signedInHere();
+ onboarding.accountCreated(
+ now.subtract(const Duration(minutes: 2)),
+ now: now,
+ );
+ onboarding.accountCreated(
+ now.subtract(const Duration(minutes: 2)),
+ now: now,
+ );
+ await usage.flush();
+ expect(counters('ui_phone_sign_in_new_account'), {
+ 'ui_phone_sign_in_new_account': 1,
+ });
+ });
+
+ test('allows for this phone\'s clock running behind', () async {
+ await signIn();
+ signedInHere();
+ onboarding.accountCreated(now.add(const Duration(minutes: 5)), now: now);
+ await usage.flush();
+ expect(counters('ui_phone_sign_in_new_account'), hasLength(1));
+ });
+
+ test('counts nothing for an older account, an unknown age, or a launch that did not sign in', () async {
+ await signIn();
+ // Opened signed in: whatever its age, this launch made nothing.
+ onboarding.accountCreated(now, now: now);
+ signedInHere();
+ onboarding.accountCreated(
+ now.subtract(const Duration(days: 3)),
+ now: now,
+ );
+ onboarding.accountCreated(null, now: now);
+ await usage.flush();
+ expect(counters('ui_phone_sign_in_new_account'), isEmpty);
+ });
+ });
+
test('every mark is in the backend vocabulary under each step it names', () {
final vocabulary = File('../backend/src/types/productUsage.ts')
.readAsStringSync();
diff --git a/mobile/test/product_measurement_test.dart b/mobile/test/product_measurement_test.dart
index d728ca3bd..1c1051341 100644
--- a/mobile/test/product_measurement_test.dart
+++ b/mobile/test/product_measurement_test.dart
@@ -381,6 +381,109 @@ void main() {
expect(phone.accountOptOutPending, isFalse);
},
);
+
+ group('the first launch of an install', () {
+ Map<String, dynamic> counted(List<Map<String, dynamic>> sent) => {
+ for (final report in sent) ...(report['counters'] as Map),
+ };
+
+ test('signed out it is counted once, never on a later launch', () async {
+ final first = launch(keeping: store);
+ await first.noteFirstOpen();
+ await first.noteFirstOpen();
+ await first.flush();
+ expect(counted(setup), {'setup_phone_first_open': 1});
+ expect(store.values[ProductMeasurement.firstOpenKey], '1');
+
+ final second = launch(keeping: store);
+ await second.noteFirstOpen();
+ await second.flush();
+ expect(setup, hasLength(1));
+ });
+
+ test(
+ 'off signed out it is never counted, nor a later launch instead',
+ () async {
+ final first = launch(keeping: store);
+ await first.setupChoiceReady;
+ await first.setSignedOutSharing(false);
+ await first.noteFirstOpen();
+ await first.setSignedOutSharing(true);
+ await first.flush();
+ expect(setup, isEmpty);
+
+ final second = launch(keeping: store);
+ await second.noteFirstOpen();
+ await second.flush();
+ expect(setup, isEmpty);
+ },
+ );
+
+ test(
+ 'opened signed in, it waits for the account and counts there',
+ () async {
+ final phone = launch(keeping: store);
+ phone.account(
+ 'PRIVATE_ACCOUNT',
+ choice: () async => {'enabled': true, 'revision': 0},
+ send: (r) async {
+ reports.add(jsonDecode(jsonEncode(r)));
+ return {'accepted': true};
+ },
+ );
+ await phone.noteFirstOpen();
+ expect(reports, isEmpty, reason: 'the account has not answered');
+ await phone.flush();
+ await phone.flush();
+ expect(setup, isEmpty);
+ expect(counted(reports), {'ui_phone_first_open': 1});
+ },
+ );
+
+ test('an account that reads off drops it for good', () async {
+ final phone = launch(keeping: store);
+ await signIn(phone, () => {'enabled': false, 'revision': 1});
+ await phone.noteFirstOpen();
+ phone.savedChoice({'enabled': true, 'revision': 2}, explicit: true);
+ await phone.flush();
+ expect(reports, isEmpty);
+ expect(setup, isEmpty);
+ });
+
+ test('nothing without storage, nor off the phone', () async {
+ final sample = launch();
+ await sample.setSignedOutSharing(true);
+ await sample.noteFirstOpen();
+ await sample.flush();
+ final desktop = ProductMeasurement(
+ surface: 'desktop',
+ automatic: false,
+ now: () => now,
+ setupStorage: store,
+ sendSetup: (r) async {
+ setup.add(jsonDecode(jsonEncode(r)));
+ return {'accepted': true};
+ },
+ );
+ opened.add(desktop);
+ await desktop.noteFirstOpen();
+ await desktop.flush();
+ expect(setup, isEmpty);
+ expect(
+ store.values.containsKey(ProductMeasurement.firstOpenKey),
+ isFalse,
+ );
+ });
+
+ test('a flag that cannot be read counts nothing', () async {
+ final phone = launch(keeping: _Unreadable());
+ await phone.setupChoiceReady;
+ await phone.setSignedOutSharing(true);
+ await phone.noteFirstOpen();
+ await phone.flush();
+ expect(setup, isEmpty);
+ });
+ });
});
group('the phone\'s side pages', () {