create-expo-updates-server 0.1.5 → 0.2.0
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/dist/template/.dockerignore +16 -0
- package/dist/template/README.md +51 -660
- package/dist/template/app/api/admin/device-targets/route.test.ts +43 -0
- package/dist/template/app/api/admin/device-targets/route.ts +91 -0
- package/dist/template/app/api/admin/devices/route.test.ts +41 -0
- package/dist/template/app/api/admin/devices/route.ts +15 -0
- package/dist/template/app/api/admin/updates/[encodedKey]/audience/route.ts +75 -0
- package/dist/template/app/api/device-heartbeat/route.ts +2 -0
- package/dist/template/app/globals.css +114 -15
- package/dist/template/app/layout.tsx +12 -4
- package/dist/template/app/login/page.tsx +22 -18
- package/dist/template/app/updates/[encodedKey]/page.tsx +112 -88
- package/dist/template/app/updates/devices/page.tsx +264 -0
- package/dist/template/app/updates/embedded/page.tsx +102 -68
- package/dist/template/app/updates/emergency-redirects/page.tsx +144 -111
- package/dist/template/app/updates/layout.tsx +6 -1
- package/dist/template/app/updates/page.tsx +262 -212
- package/dist/template/components/admin/admin-shell.tsx +196 -0
- package/dist/template/components/admin/language-switcher.tsx +25 -0
- package/dist/template/components/admin/theme-provider.tsx +15 -0
- package/dist/template/components/ui/alert.tsx +65 -0
- package/dist/template/components/ui/badge.tsx +47 -0
- package/dist/template/components/ui/button.tsx +63 -0
- package/dist/template/components/ui/card.tsx +91 -0
- package/dist/template/components/ui/chart.tsx +373 -0
- package/dist/template/components/ui/checkbox.tsx +31 -0
- package/dist/template/components/ui/dialog.tsx +158 -0
- package/dist/template/components/ui/dropdown-menu.tsx +256 -0
- package/dist/template/components/ui/input.tsx +20 -0
- package/dist/template/components/ui/label.tsx +23 -0
- package/dist/template/components/ui/native-select.tsx +61 -0
- package/dist/template/components/ui/popover.tsx +88 -0
- package/dist/template/components/ui/scroll-area.tsx +57 -0
- package/dist/template/components/ui/select.tsx +189 -0
- package/dist/template/components/ui/separator.tsx +27 -0
- package/dist/template/components/ui/sheet.tsx +142 -0
- package/dist/template/components/ui/sidebar.tsx +736 -0
- package/dist/template/components/ui/skeleton.tsx +13 -0
- package/dist/template/components/ui/switch.tsx +34 -0
- package/dist/template/components/ui/table.tsx +115 -0
- package/dist/template/components/ui/tabs.tsx +90 -0
- package/dist/template/components/ui/textarea.tsx +17 -0
- package/dist/template/components/ui/tooltip.tsx +56 -0
- package/dist/template/components.json +22 -0
- package/dist/template/docs/admin-ui.md +35 -0
- package/dist/template/docs/device-targeting.md +129 -0
- package/dist/template/docs/doctor.md +137 -0
- package/dist/template/docs/expo-app-setup.md +174 -47
- package/dist/template/docs/getting-started.md +105 -146
- package/dist/template/docs/getting-started.ru.md +106 -191
- package/dist/template/docs/index.md +14 -33
- package/dist/template/docs/installation-telemetry.md +233 -0
- package/dist/template/docs/migrations/2026-10-16-manifest-identity.sql +89 -0
- package/dist/template/docs/migrations/2026-10-19-device-heartbeat.sql +131 -0
- package/dist/template/docs/migrations/2026-10-22-device-analytics.sql +264 -0
- package/dist/template/docs/migrations/2026-10-22-device-targeting-activation.sql +108 -0
- package/dist/template/docs/migrations/2026-10-22-device-targeting.sql +130 -0
- package/dist/template/docs/migrations/2026-10-22-device-version-metadata.sql +154 -0
- package/dist/template/docs/migrations/schema.sql +868 -6
- package/dist/template/docs/npm-installer.md +15 -30
- package/dist/template/docs/operations-guide.md +87 -0
- package/dist/template/docs/ota-client-lifecycle.md +263 -0
- package/dist/template/docs/ota-update-policy.md +36 -2
- package/dist/template/docs/supabase-setup.md +4 -0
- package/dist/template/hooks/use-mobile.ts +21 -0
- package/dist/template/lib/utils.ts +6 -0
- package/dist/template/package-lock.json +2325 -101
- package/dist/template/package.json +13 -3
- package/dist/template/scripts/configure-expo-app.mjs +11 -15
- package/dist/template/scripts/doctor.mjs +51 -0
- package/dist/template/scripts/ensure-test-bucket.mjs +21 -0
- package/dist/template/scripts/lib/device-identity.test.ts +29 -0
- package/dist/template/scripts/lib/doctor-probes.mjs +139 -0
- package/dist/template/scripts/lib/doctor.mjs +208 -0
- package/dist/template/scripts/lib/doctor.test.mjs +262 -0
- package/dist/template/scripts/lib/expo-app-configurator.mjs +14 -9
- package/dist/template/scripts/lib/expo-app-configurator.test.ts +3 -2
- package/dist/template/scripts/lib/ota-client-configurator.mjs +69 -0
- package/dist/template/scripts/lib/ota-client-configurator.test.ts +88 -0
- package/dist/template/scripts/lib/ota-client.test.ts +55 -0
- package/dist/template/scripts/lib/ota-root-hook-patcher.mjs +173 -0
- package/dist/template/scripts/lib/ota-root-hook-patcher.test.ts +123 -0
- package/dist/template/scripts/lib/ota-update-controller.test.ts +231 -0
- package/dist/template/scripts/tests/device-analytics-migrations.mjs +80 -0
- package/dist/template/scripts/tests/device-analytics.mjs +126 -0
- package/dist/template/scripts/tests/device-heartbeat.mjs +86 -0
- package/dist/template/scripts/tests/device-targeting-activation.mjs +111 -0
- package/dist/template/scripts/tests/device-targeting.mjs +39 -0
- package/dist/template/scripts/tests/device-version-metadata.mjs +31 -0
- package/dist/template/scripts/tests/manifest-identity-concurrency.mjs +106 -0
- package/dist/template/scripts/tests/manifest-identity.sql +204 -0
- package/dist/template/src/client/admin-types.ts +1 -0
- package/dist/template/src/client/devices/analytics.tsx +429 -0
- package/dist/template/src/client/devices/device-name.tsx +82 -0
- package/dist/template/src/client/devices/registry.tsx +399 -0
- package/dist/template/src/client/devices/use-devices-report.ts +108 -0
- package/dist/template/src/client/devices/version-adoption-chart.tsx +269 -0
- package/dist/template/src/client/devices/version-groups.test.ts +57 -0
- package/dist/template/src/client/devices/version-groups.ts +101 -0
- package/dist/template/src/client/distribution-control.tsx +135 -81
- package/dist/template/src/client/guard-action-combobox.tsx +13 -24
- package/dist/template/src/client/i18n/en.json +286 -0
- package/dist/template/src/client/i18n/messages.test.ts +42 -0
- package/dist/template/src/client/i18n/messages.ts +31 -0
- package/dist/template/src/client/i18n/provider.tsx +55 -0
- package/dist/template/src/client/i18n/use-formatters.ts +38 -0
- package/dist/template/src/client/logout-button.tsx +3 -1
- package/dist/template/src/client/update-audience-editor.tsx +285 -0
- package/dist/template/src/client/update-policy-editor.tsx +53 -41
- package/dist/template/src/server/controllers/manifest.test.ts +250 -7
- package/dist/template/src/server/controllers/manifest.ts +39 -31
- package/dist/template/src/server/devices/audience.test.ts +48 -0
- package/dist/template/src/server/devices/audience.ts +86 -0
- package/dist/template/src/server/devices/heartbeat.test.ts +39 -0
- package/dist/template/src/server/devices/heartbeat.ts +75 -0
- package/dist/template/src/server/devices/report-types.ts +114 -0
- package/dist/template/src/server/devices/report.test.ts +29 -0
- package/dist/template/src/server/devices/report.ts +27 -0
- package/dist/template/src/server/lib/admin-audience-activation.test.ts +94 -0
- package/dist/template/src/server/lib/admin-updates.ts +32 -168
- package/dist/template/src/server/lib/distribution-control.test.ts +23 -2
- package/dist/template/src/server/lib/distribution-control.ts +13 -4
- package/dist/template/src/server/lib/supabase.js +10 -10
- package/dist/template/src/server/lib/supabase.test.ts +11 -0
- package/dist/template/src/server/manifest/manifest-identity.test.ts +30 -0
- package/dist/template/src/server/manifest/manifest-identity.ts +24 -0
- package/dist/template/src/server/manifest/manifest-repository.ts +31 -102
- package/dist/template/src/server/manifest/manifest-response.ts +1 -1
- package/dist/template/templates/expo-app/ota-client/README.md +59 -0
- package/dist/template/templates/expo-app/ota-client/controller.js +60 -0
- package/dist/template/templates/expo-app/ota-client/device-identity.js +26 -0
- package/dist/template/templates/expo-app/ota-client/docs/update-lifecycle.md +52 -0
- package/dist/template/templates/expo-app/ota-client/index.d.ts +47 -0
- package/dist/template/templates/expo-app/ota-client/index.js +3 -0
- package/dist/template/templates/expo-app/ota-client/model/hooks/useOtaUpdates.js +48 -0
- package/dist/template/templates/expo-app/ota-client/model/update-controller.js +182 -0
- package/dist/template/templates/expo-app/ota-client/runtime.js +112 -0
- package/dist/template/templates/expo-app/ota-client/ui/ota-update-gate.js +56 -0
- package/dist/template-manifest.json +582 -77
- package/lib/cli.mjs +4 -6
- package/package.json +1 -1
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
// Run against a disposable database after schema.sql. Never use a production URL.
|
|
2
|
+
// OTA_TEST_DATABASE_URL=... node scripts/tests/device-analytics.mjs
|
|
3
|
+
import assert from 'node:assert/strict';
|
|
4
|
+
import { randomUUID } from 'node:crypto';
|
|
5
|
+
import { Pool } from 'pg';
|
|
6
|
+
|
|
7
|
+
if (!process.env.OTA_TEST_DATABASE_URL) throw new Error('Set OTA_TEST_DATABASE_URL to a disposable database');
|
|
8
|
+
const pool = new Pool({ connectionString: process.env.OTA_TEST_DATABASE_URL, max: 4, statement_timeout: 10000 });
|
|
9
|
+
const ids = Array.from({ length: 10 }, () => randomUUID());
|
|
10
|
+
const scope = `analytics-${randomUUID()}`;
|
|
11
|
+
const base = {
|
|
12
|
+
installationId: ids[0], platform: 'android', channel: scope, runtimeVersion: scope,
|
|
13
|
+
eventId: randomUUID(), sequence: 1, observedAt: '2020-01-01T00:00:00.000Z',
|
|
14
|
+
updateId: null, nativeVersion: '1.0', nativeBuild: '1', isEmbeddedLaunch: true, isEmergencyLaunch: false,
|
|
15
|
+
};
|
|
16
|
+
const send = async (patch = {}, connection = pool) => (await connection.query(
|
|
17
|
+
'select * from ota_record_heartbeat($1::jsonb)', [JSON.stringify({ ...base, ...patch })],
|
|
18
|
+
)).rows[0].result;
|
|
19
|
+
const report = async (patch = {}) => {
|
|
20
|
+
const p = { days: 7, platform: null, channel: scope, runtime: null, offset: 0, kind: null, search: null, ...patch };
|
|
21
|
+
return (await pool.query('select * from ota_devices_report_v2($1,$2,$3,$4,$5,$6,$7)',
|
|
22
|
+
[p.days, p.platform, p.channel, p.runtime, p.offset, p.kind, p.search])).rows[0].report;
|
|
23
|
+
};
|
|
24
|
+
const facts = async (id = ids[0]) => (await pool.query(
|
|
25
|
+
'select d.*,activity_date::text as activity_date from ota_device_activity_daily d where installation_id=$1 order by d.activity_date', [id],
|
|
26
|
+
)).rows;
|
|
27
|
+
const metadata = (await pool.query('select * from ota_device_analytics_metadata')).rows[0];
|
|
28
|
+
try {
|
|
29
|
+
const empty = await report();
|
|
30
|
+
assert.equal(empty.total, 0);
|
|
31
|
+
assert.equal(empty.activity.length, 7);
|
|
32
|
+
assert.equal(empty.activity.at(-1).coverage, 'partial');
|
|
33
|
+
assert.equal(empty.activity.at(-1).activeInstallations, 0);
|
|
34
|
+
assert.equal(empty.activity[0].activeInstallations, null, 'Do not manufacture activity before collection');
|
|
35
|
+
|
|
36
|
+
assert.equal(await send(), 'accepted');
|
|
37
|
+
const first = await facts();
|
|
38
|
+
assert.equal(first.length, 1);
|
|
39
|
+
assert.equal(first[0].activity_date, empty.activity.at(-1).date, 'Use receipt UTC day, not client timestamp');
|
|
40
|
+
assert.equal(await send(), 'duplicate');
|
|
41
|
+
assert.deepEqual(await facts(), first, 'Duplicate cannot update daily facts');
|
|
42
|
+
assert.equal(await send({ sequence: 2, eventId: randomUUID(), isEmbeddedLaunch: false, runtimeVersion: 'changed', isEmergencyLaunch: true }), 'accepted');
|
|
43
|
+
assert.equal(await send(), 'stale');
|
|
44
|
+
const updated = await facts();
|
|
45
|
+
assert.equal(updated.length, 1);
|
|
46
|
+
assert.equal(updated[0].launch_kind, 'unknown');
|
|
47
|
+
assert.equal(updated[0].first_received_at.toISOString(), first[0].first_received_at.toISOString());
|
|
48
|
+
const cohort = (await pool.query('select * from ota_device_activity_cohorts where installation_id=$1', [ids[0]])).rows[0];
|
|
49
|
+
assert.equal(cohort.launch_kind, 'embedded', 'Entry segmentation must stay immutable');
|
|
50
|
+
assert.equal(cohort.runtime_version, scope);
|
|
51
|
+
|
|
52
|
+
const current = await report({ kind: 'unknown', search: ids[0].slice(0, 8) });
|
|
53
|
+
assert.equal(current.total, 1);
|
|
54
|
+
assert.equal(current.summary.emergencyInstallations, 1);
|
|
55
|
+
assert.equal(current.activity.at(-1).activeInstallations, 1);
|
|
56
|
+
assert.equal(current.activity.at(-1).newInstallations, 1);
|
|
57
|
+
assert.equal(current.cohorts.length, 0, 'Retention filters use entry state, not latest state');
|
|
58
|
+
assert.equal(current.adoptionCohorts[0].unknownInstallations, 1);
|
|
59
|
+
assert.equal((await report({ kind: 'embedded' })).cohorts[0].cells[0].rate, 1);
|
|
60
|
+
assert.equal((await report({ offset: 50 })).devices.length, 0);
|
|
61
|
+
assert.equal((await report({ offset: 50 })).summary.activeInstallations, 1, 'Pagination cannot alter aggregate counts');
|
|
62
|
+
assert.deepEqual(current.options.launchKinds, ['embedded', 'unknown']);
|
|
63
|
+
|
|
64
|
+
// A pre-existing instrumented installation has no historical daily/cohort data.
|
|
65
|
+
await send({ installationId: ids[1], eventId: randomUUID() });
|
|
66
|
+
await pool.query('delete from ota_device_activity_daily where installation_id=$1', [ids[1]]);
|
|
67
|
+
await pool.query('delete from ota_device_activity_cohorts where installation_id=$1', [ids[1]]);
|
|
68
|
+
await pool.query("update ota_device_state set first_seen_at=now()-interval '50 days' where installation_id=$1", [ids[1]]);
|
|
69
|
+
await send({ installationId: ids[1], sequence: 2, eventId: randomUUID() });
|
|
70
|
+
assert.equal((await facts(ids[1])).length, 1);
|
|
71
|
+
assert.equal((await pool.query('select count(*)::int as n from ota_device_activity_cohorts where installation_id=$1', [ids[1]])).rows[0].n, 0);
|
|
72
|
+
assert.equal((await report()).adoptionCohorts.reduce((sum, c) => sum + c.installations, 0), 2);
|
|
73
|
+
|
|
74
|
+
// Synthetic historical facts validate the aggregation without backdating real receipts.
|
|
75
|
+
await pool.query("update ota_device_analytics_metadata set collection_started_at=((now() at time zone 'UTC')::date-40)::timestamp at time zone 'UTC'");
|
|
76
|
+
const firstDay = (await pool.query("select ((now() at time zone 'UTC')::date-10)::text as day")).rows[0].day;
|
|
77
|
+
for (const id of [ids[2], ids[3]]) {
|
|
78
|
+
await pool.query(`insert into ota_device_activity_cohorts(installation_id,platform,first_observed_at,channel,runtime_version,launch_kind)
|
|
79
|
+
values($1,'android',$2::date::timestamp at time zone 'UTC',$3,$3,'embedded')`, [id, firstDay, scope]);
|
|
80
|
+
await pool.query(`insert into ota_device_activity_daily(activity_date,installation_id,platform,channel,runtime_version,launch_kind,
|
|
81
|
+
emergency_launch,first_received_at,last_received_at) values($1,$2,'android',$3,$3,'embedded',false,now(),now())`, [firstDay, id, scope]);
|
|
82
|
+
}
|
|
83
|
+
// The retained member changed scope: return activity is still retention of the entry cohort.
|
|
84
|
+
await pool.query(`insert into ota_device_activity_daily(activity_date,installation_id,platform,channel,runtime_version,launch_kind,
|
|
85
|
+
emergency_launch,first_received_at,last_received_at) values($1::date+7,$2,'android','other','other','unknown',false,now(),now())`, [firstDay, ids[2]]);
|
|
86
|
+
const historical = await report({ days: 1 });
|
|
87
|
+
const week = (await pool.query("select date_trunc('week',$1::date)::date::text as day", [firstDay])).rows[0].day;
|
|
88
|
+
const row = historical.cohorts.find(c => c.weekStart === week);
|
|
89
|
+
assert.equal(row.installations, 2, 'Retention cohort horizon is independent of one-day chart filter');
|
|
90
|
+
assert.equal(row.cells.find(c => c.day === 7).eligibleInstallations, 2);
|
|
91
|
+
assert.equal(row.cells.find(c => c.day === 7).retainedInstallations, 1);
|
|
92
|
+
assert.equal(row.cells.find(c => c.day === 7).rate, 0.5);
|
|
93
|
+
assert.equal(row.cells.find(c => c.day === 1).rate, 0, 'A complete eligible day without a return is zero');
|
|
94
|
+
assert.equal(row.cells.find(c => c.day === 14).rate, null, 'Immature observation windows are unknown');
|
|
95
|
+
assert.equal(historical.coverage.cohortWindowDays, 90);
|
|
96
|
+
assert.equal((await report({ days: 30 })).activity[0].activeInstallations, 0, 'Covered day without facts is a real zero');
|
|
97
|
+
|
|
98
|
+
// D30 can mature even when the activity chart only shows seven days.
|
|
99
|
+
await pool.query(`insert into ota_device_activity_cohorts(installation_id,platform,first_observed_at,channel,runtime_version,launch_kind)
|
|
100
|
+
values($1,'android',((now() at time zone 'UTC')::date-32)::timestamp at time zone 'UTC',$2,$2,'embedded')`, [ids[4], scope]);
|
|
101
|
+
const mature = (await report()).cohorts.find(c => c.cells.find(cell => cell.day === 30).eligibleInstallations > 0);
|
|
102
|
+
assert.equal(mature.cells.find(c => c.day === 30).rate, 0);
|
|
103
|
+
|
|
104
|
+
// Two concurrent accepted events still produce one daily installation fact.
|
|
105
|
+
const outcomes = await Promise.all([
|
|
106
|
+
send({ installationId: ids[5], sequence: 1, eventId: randomUUID() }),
|
|
107
|
+
send({ installationId: ids[5], sequence: 2, eventId: randomUUID() }),
|
|
108
|
+
]);
|
|
109
|
+
assert.ok(outcomes.includes('accepted'));
|
|
110
|
+
assert.equal((await facts(ids[5])).length, 1);
|
|
111
|
+
|
|
112
|
+
await pool.query(`insert into ota_device_activity_daily(activity_date,installation_id,platform,channel,runtime_version,launch_kind,
|
|
113
|
+
emergency_launch,first_received_at,last_received_at)
|
|
114
|
+
select (now() at time zone 'UTC')::date - days,$1,'android',$2,$2,'unknown',false,now(),now() from unnest(array[89,90]) days`, [ids[6], scope]);
|
|
115
|
+
await pool.query("update ota_device_analytics_metadata set last_pruned_on=(now() at time zone 'UTC')::date-1");
|
|
116
|
+
await send({ installationId: ids[7], eventId: randomUUID() });
|
|
117
|
+
assert.equal((await facts(ids[6])).length, 1, 'Prune deletes day90 and retains day89');
|
|
118
|
+
console.log('PASS: daily acceptance/deduplication, snapshot/entry segmentation, honest coverage, filters/options/KPIs, adoption, mature retention, concurrent writes, bounded retention.');
|
|
119
|
+
} finally {
|
|
120
|
+
await pool.query('delete from ota_device_activity_daily where installation_id=any($1)', [ids]);
|
|
121
|
+
await pool.query('delete from ota_device_activity_cohorts where installation_id=any($1)', [ids]);
|
|
122
|
+
await pool.query('delete from ota_device_transitions where installation_id=any($1)', [ids]);
|
|
123
|
+
await pool.query('delete from ota_device_state where installation_id=any($1)', [ids]);
|
|
124
|
+
await pool.query('update ota_device_analytics_metadata set collection_started_at=$1,last_pruned_on=$2', [metadata.collection_started_at, metadata.last_pruned_on]);
|
|
125
|
+
await pool.end();
|
|
126
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
// Use only a disposable database with the heartbeat migration applied.
|
|
2
|
+
import assert from 'node:assert/strict';
|
|
3
|
+
import { randomUUID } from 'node:crypto';
|
|
4
|
+
import { setTimeout as delay } from 'node:timers/promises';
|
|
5
|
+
import { Pool } from 'pg';
|
|
6
|
+
if (!process.env.OTA_TEST_DATABASE_URL) throw new Error('Set OTA_TEST_DATABASE_URL to a disposable database');
|
|
7
|
+
const pool = new Pool({ connectionString: process.env.OTA_TEST_DATABASE_URL, max: 3, statement_timeout: 10000 });
|
|
8
|
+
const scope = `heartbeat-${randomUUID()}`;
|
|
9
|
+
const installationId = randomUUID();
|
|
10
|
+
const concurrentId = randomUUID();
|
|
11
|
+
const updateId = randomUUID();
|
|
12
|
+
const launchId = randomUUID();
|
|
13
|
+
const launchId2 = randomUUID();
|
|
14
|
+
const base = { installationId, eventId: randomUUID(), sequence: 1, observedAt: new Date().toISOString(), platform: 'android', channel: scope, runtimeVersion: scope, nativeVersion: '1.0', nativeBuild: '1', updateId: launchId, isEmbeddedLaunch: false, isEmergencyLaunch: false };
|
|
15
|
+
const record = async (event, db = pool) => (await db.query('select * from ota_record_heartbeat($1::jsonb)', [JSON.stringify(event)])).rows[0].result;
|
|
16
|
+
const state = async () => (await pool.query('select * from ota_device_state where installation_id=$1', [installationId])).rows[0];
|
|
17
|
+
const transitions = async () => Number((await pool.query('select count(*) as n from ota_device_transitions where installation_id=$1', [installationId])).rows[0].n);
|
|
18
|
+
try {
|
|
19
|
+
assert.equal(await record(base), 'accepted');
|
|
20
|
+
assert.equal((await state()).launch_kind, 'unknown');
|
|
21
|
+
const first = await state();
|
|
22
|
+
assert.equal(await record(base), 'duplicate');
|
|
23
|
+
await assert.rejects(() => record({ ...base, sequence: 0 }), /INVALID_SEQUENCE/);
|
|
24
|
+
await assert.rejects(() => record({ ...base, eventId: randomUUID() }), /HEARTBEAT_SEQUENCE_CONFLICT/);
|
|
25
|
+
assert.equal((await state()).last_seen_at.toISOString(), first.last_seen_at.toISOString());
|
|
26
|
+
await pool.query(`insert into ota_updates(update_id,build_id,runtime_version,channel,platform,created_at,created_at_path,storage_bucket,storage_base_path,manifest,comment)
|
|
27
|
+
values($1,$1,$2,$2,'android',now(),'test','test','test','{}','Smoke test')`, [updateId, scope]);
|
|
28
|
+
for (const id of [launchId, launchId2]) await pool.query(`insert into ota_served_manifest_log(served_manifest_id,update_id,runtime_version,channel,platform) values($1,$2,$3,$3,'android')`, [id, updateId, scope]);
|
|
29
|
+
assert.equal(await record({ ...base, sequence: 2, eventId: randomUUID() }), 'accepted');
|
|
30
|
+
assert.equal((await state()).resolved_update_id, updateId);
|
|
31
|
+
assert.equal(await transitions(), 2);
|
|
32
|
+
assert.equal(await record({ ...base, sequence: 3, eventId: randomUUID(), updateId: launchId2 }), 'accepted');
|
|
33
|
+
assert.equal(await transitions(), 2, 'Epoch rotation of same artifact is not a version transition');
|
|
34
|
+
assert.equal(await record({ ...base, sequence: 2 }), 'stale');
|
|
35
|
+
// Assert both exclusions while a matching artifact and mapping still exist.
|
|
36
|
+
await record({ ...base, installationId: concurrentId, eventId: randomUUID(), isEmbeddedLaunch: true });
|
|
37
|
+
let isolated = (await pool.query('select * from ota_device_state where installation_id=$1', [concurrentId])).rows[0];
|
|
38
|
+
assert.equal(isolated.launch_kind, 'embedded'); assert.equal(isolated.resolved_update_id, null);
|
|
39
|
+
await record({ ...base, installationId: concurrentId, sequence: 2, eventId: randomUUID(), channel: 'other' });
|
|
40
|
+
isolated = (await pool.query('select * from ota_device_state where installation_id=$1', [concurrentId])).rows[0];
|
|
41
|
+
assert.equal(isolated.launch_kind, 'unknown'); assert.equal(isolated.resolved_update_id, null);
|
|
42
|
+
await pool.query('delete from ota_device_transitions where installation_id=$1', [concurrentId]);
|
|
43
|
+
await pool.query('delete from ota_device_state where installation_id=$1', [concurrentId]);
|
|
44
|
+
await pool.query('delete from ota_updates where update_id=$1', [updateId]);
|
|
45
|
+
await record({ ...base, sequence: 4, eventId: randomUUID(), updateId: launchId2 });
|
|
46
|
+
assert.equal((await state()).resolved_update_id, updateId, 'Deleted artifacts retain snapshot');
|
|
47
|
+
assert.equal((await state()).current_update_id, null);
|
|
48
|
+
assert.equal(await transitions(), 2);
|
|
49
|
+
await record({ ...base, sequence: 5, eventId: randomUUID(), isEmbeddedLaunch: true });
|
|
50
|
+
assert.equal((await state()).launch_kind, 'embedded');
|
|
51
|
+
assert.equal((await state()).resolved_update_id, null);
|
|
52
|
+
assert.equal(await transitions(), 3);
|
|
53
|
+
await record({ ...base, sequence: 6, eventId: randomUUID(), channel: 'other', isEmbeddedLaunch: false });
|
|
54
|
+
assert.equal((await state()).launch_kind, 'unknown', 'Scope mismatch must not resolve');
|
|
55
|
+
await record({ ...base, sequence: 7, eventId: randomUUID(), isEmbeddedLaunch: true, nativeBuild: '2' });
|
|
56
|
+
const report = (await pool.query('select * from ota_devices_report(7,null,$1,null,0)', [scope])).rows[0].report;
|
|
57
|
+
assert.equal(report.total, 1); assert.equal(report.devices.length, 1); assert.equal(report.versions[0].installations, 1);
|
|
58
|
+
await pool.query("update ota_device_state set last_seen_at=now()-interval '8 days' where installation_id=$1", [installationId]);
|
|
59
|
+
const staleReport = (await pool.query('select * from ota_devices_report(7,null,$1,null,0)', [scope])).rows[0].report;
|
|
60
|
+
assert.equal(staleReport.total, 0);
|
|
61
|
+
const winner = await pool.connect(); const contender = await pool.connect();
|
|
62
|
+
let pending;
|
|
63
|
+
try {
|
|
64
|
+
await winner.query('begin');
|
|
65
|
+
const event = { ...base, installationId: concurrentId, eventId: randomUUID() };
|
|
66
|
+
assert.equal(await record(event, winner), 'accepted');
|
|
67
|
+
const pid = (await contender.query('select pg_backend_pid() as pid')).rows[0].pid;
|
|
68
|
+
pending = record({ ...event, sequence: 2, eventId: randomUUID() }, contender);
|
|
69
|
+
let waiting = false;
|
|
70
|
+
for (let i = 0; i < 100; i++) {
|
|
71
|
+
if ((await pool.query('select wait_event_type from pg_stat_activity where pid=$1', [pid])).rows[0].wait_event_type === 'Lock') { waiting = true; break; }
|
|
72
|
+
await delay(20);
|
|
73
|
+
}
|
|
74
|
+
assert.ok(waiting, 'Concurrent registration must wait for first writer');
|
|
75
|
+
await winner.query('commit'); assert.equal(await pending, 'accepted');
|
|
76
|
+
const row = (await pool.query('select last_sequence from ota_device_state where installation_id=$1', [concurrentId])).rows[0];
|
|
77
|
+
assert.equal(Number(row.last_sequence), 2);
|
|
78
|
+
assert.equal(Number((await pool.query('select count(*) as n from ota_device_transitions where installation_id=$1', [concurrentId])).rows[0].n), 1);
|
|
79
|
+
} finally { await winner.query('rollback'); if (pending) await pending; winner.release(); contender.release(); }
|
|
80
|
+
console.log('PASS: atomic registration, duplicate/stale/conflict, mapping, epoch rotation, deletion, scope, embedded, statistics, concurrent first registration.');
|
|
81
|
+
} finally {
|
|
82
|
+
await pool.query('delete from ota_device_transitions where installation_id=any($1)', [[installationId, concurrentId]]);
|
|
83
|
+
await pool.query('delete from ota_device_state where installation_id=any($1)', [[installationId, concurrentId]]);
|
|
84
|
+
await pool.query('delete from ota_updates where update_id=$1', [updateId]);
|
|
85
|
+
await pool.end();
|
|
86
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
// Use an EMPTY disposable database after schema.sql. Never uses environment files.
|
|
2
|
+
// OTA_TEST_DATABASE_URL=... node scripts/tests/device-targeting-activation.mjs
|
|
3
|
+
import assert from 'node:assert/strict';
|
|
4
|
+
import { randomUUID } from 'node:crypto';
|
|
5
|
+
import { setTimeout as delay } from 'node:timers/promises';
|
|
6
|
+
import { Pool } from 'pg';
|
|
7
|
+
if (!process.env.OTA_TEST_DATABASE_URL) throw new Error('Set OTA_TEST_DATABASE_URL to an empty disposable database');
|
|
8
|
+
const db = new Pool({ connectionString: process.env.OTA_TEST_DATABASE_URL, max: 4, statement_timeout: 10000 });
|
|
9
|
+
const scope = `activation-${randomUUID()}`, pub = randomUUID(), target = randomUUID();
|
|
10
|
+
const device = 'a'.repeat(64);
|
|
11
|
+
const active = async (connection = db) => (await connection.query('select update_id from ota_updates where runtime_version=$1 and is_active order by update_id', [scope])).rows.map(r => r.update_id).sort();
|
|
12
|
+
const expectedBoth = [pub, target].sort();
|
|
13
|
+
async function failing(connection, sql, args, pattern) {
|
|
14
|
+
await connection.query('savepoint expected_failure');
|
|
15
|
+
await assert.rejects(connection.query(sql, args), pattern);
|
|
16
|
+
await connection.query('rollback to savepoint expected_failure');
|
|
17
|
+
}
|
|
18
|
+
async function waitForLock(pid) {
|
|
19
|
+
for (let i = 0; i < 100; i++) {
|
|
20
|
+
const row = (await db.query('select wait_event_type from pg_stat_activity where pid=$1', [pid])).rows[0];
|
|
21
|
+
if (row?.wait_event_type === 'Lock') return;
|
|
22
|
+
await delay(10);
|
|
23
|
+
}
|
|
24
|
+
throw new Error('Contender did not wait for the distribution lock');
|
|
25
|
+
}
|
|
26
|
+
async function changeWhileActivating(mode, expected) {
|
|
27
|
+
const owner = await db.connect(), contender = await db.connect();
|
|
28
|
+
let pending;
|
|
29
|
+
try {
|
|
30
|
+
await owner.query('begin');
|
|
31
|
+
const version = (await owner.query('select audience_version from ota_updates where update_id=$1', [target])).rows[0].audience_version;
|
|
32
|
+
await owner.query('select * from ota_replace_audience($1,$2,$3,$4,$5)', [target, mode, '[]', JSON.stringify(mode === 'devices' ? [device] : []), version]);
|
|
33
|
+
const pid = (await contender.query('select pg_backend_pid() as pid')).rows[0].pid;
|
|
34
|
+
pending = contender.query('update ota_updates set is_active=true,disabled_at=null where update_id=$1', [target]);
|
|
35
|
+
await waitForLock(pid);
|
|
36
|
+
await owner.query('commit');
|
|
37
|
+
await pending;
|
|
38
|
+
assert.deepEqual(await active(), expected, `Activation must use the committed ${mode} audience after waiting`);
|
|
39
|
+
} finally {
|
|
40
|
+
await owner.query('rollback');
|
|
41
|
+
if (pending) await pending.catch(() => {});
|
|
42
|
+
owner.release(); contender.release();
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
try {
|
|
46
|
+
assert.deepEqual((await db.query("select action_key from ota_guard_actions where action_key in ('ota-force-update','ota-force-store-update') order by action_key")).rows.map(r => r.action_key), ['ota-force-store-update','ota-force-update']);
|
|
47
|
+
assert.equal((await db.query('select count(*)::int as n from ota_updates')).rows[0].n, 0,
|
|
48
|
+
'Refusing a database containing releases: use an empty disposable database');
|
|
49
|
+
for (const [id, date] of [[pub,'2026-10-20'],[target,'2026-10-21']]) {
|
|
50
|
+
await db.query(`insert into ota_updates(update_id,build_id,runtime_version,channel,platform,created_at,created_at_path,storage_bucket,storage_base_path,manifest) values($1,$1,$2,$2,'android',$3::timestamptz,$3::text,'fixture','fixture','{}')`, [id, scope, date]);
|
|
51
|
+
}
|
|
52
|
+
await db.query('update ota_updates set is_active=true,disabled_at=null where update_id=$1', [pub]);
|
|
53
|
+
await db.query(`insert into ota_update_channels(runtime_version,channel,platform,latest_update_id,latest_created_at,latest_created_at_path) select runtime_version,channel,platform,update_id,created_at,created_at_path from ota_updates where update_id=$1`, [target]);
|
|
54
|
+
await changeWhileActivating('devices', expectedBoth);
|
|
55
|
+
await db.query('update ota_updates set is_active=false where update_id=$1', [target]);
|
|
56
|
+
await changeWhileActivating('all', [target]);
|
|
57
|
+
|
|
58
|
+
// Failed activation must roll back peer deactivation in the same statement.
|
|
59
|
+
const tx = await db.connect();
|
|
60
|
+
try {
|
|
61
|
+
await tx.query('begin');
|
|
62
|
+
await tx.query('update ota_updates set is_active=true,disabled_at=null where update_id=$1', [pub]);
|
|
63
|
+
await failing(tx, 'update ota_updates set is_active=true,disabled_at=now() where update_id=$1', [target], /ota_updates_active_dates_chk/);
|
|
64
|
+
assert.deepEqual(await active(tx), [pub]);
|
|
65
|
+
await tx.query('rollback');
|
|
66
|
+
|
|
67
|
+
// Widening keeps public fallback; subsequent public activation restores the
|
|
68
|
+
// normal single-release behavior, including direct update-meta writes.
|
|
69
|
+
await tx.query('begin');
|
|
70
|
+
await tx.query('update ota_updates set is_active=true,disabled_at=null where update_id=$1', [pub]);
|
|
71
|
+
await tx.query("select * from ota_replace_audience($1,'devices','[]',$2,(select audience_version from ota_updates where update_id=$1))", [target, JSON.stringify([device])]);
|
|
72
|
+
await tx.query('update ota_updates set is_active=true,disabled_at=null where update_id=$1', [target]);
|
|
73
|
+
assert.deepEqual(await active(tx), expectedBoth);
|
|
74
|
+
await tx.query("select * from ota_replace_audience($1,'all','[]','[]',(select audience_version from ota_updates where update_id=$1))", [target]);
|
|
75
|
+
assert.deepEqual(await active(tx), expectedBoth);
|
|
76
|
+
await tx.query('update ota_updates set is_active=true,disabled_at=null where update_id=$1', [target]);
|
|
77
|
+
assert.deepEqual(await active(tx), [target]);
|
|
78
|
+
await tx.query('rollback');
|
|
79
|
+
|
|
80
|
+
// Incident repair: global stop must still permit removal of a selected OTA.
|
|
81
|
+
await tx.query('begin');
|
|
82
|
+
await tx.query('update ota_updates set is_active=true,disabled_at=null where update_id=$1', [pub]);
|
|
83
|
+
await tx.query("select * from ota_replace_audience($1,'devices','[]',$2,(select audience_version from ota_updates where update_id=$1))", [target, JSON.stringify([device])]);
|
|
84
|
+
await tx.query('update ota_updates set is_active=true,disabled_at=null where update_id=$1', [target]);
|
|
85
|
+
await tx.query("update ota_distribution_control set blocked=true,reason='activation regression' where singleton_id=1");
|
|
86
|
+
await tx.query('update ota_updates set is_active=false,disabled_at=now() where update_id=$1', [target]);
|
|
87
|
+
await tx.query(`update ota_update_channels c set latest_update_id=u.update_id,latest_created_at=u.created_at,latest_created_at_path=u.created_at_path,active_update_id=null from ota_updates u where c.runtime_version=$1 and u.update_id=$2`, [scope, pub]);
|
|
88
|
+
assert.deepEqual(await active(tx), [pub]);
|
|
89
|
+
await failing(tx, 'update ota_updates set is_active=true,disabled_at=null where update_id=$1', [target], /OTA_DISTRIBUTION_BLOCKED/);
|
|
90
|
+
await failing(tx, 'update ota_update_channels set latest_update_id=$1 where runtime_version=$2', [target, scope], /OTA_DISTRIBUTION_BLOCKED/);
|
|
91
|
+
await tx.query('rollback');
|
|
92
|
+
|
|
93
|
+
// Clearing a live rollback remains blocked; disabling it permits cleanup.
|
|
94
|
+
await tx.query('begin');
|
|
95
|
+
await tx.query('update ota_updates set is_active=true,disabled_at=null where update_id=$1', [pub]);
|
|
96
|
+
await tx.query("select * from ota_replace_audience($1,'devices','[]',$2,(select audience_version from ota_updates where update_id=$1))", [target, JSON.stringify([device])]);
|
|
97
|
+
await tx.query('update ota_updates set is_active=true,disabled_at=null where update_id=$1', [target]);
|
|
98
|
+
await tx.query('update ota_update_channels set active_update_id=$1 where runtime_version=$2', [pub, scope]);
|
|
99
|
+
await tx.query("update ota_distribution_control set blocked=true,reason='rollback regression' where singleton_id=1");
|
|
100
|
+
await failing(tx, 'update ota_update_channels set active_update_id=null where runtime_version=$1', [scope], /OTA_DISTRIBUTION_BLOCKED/);
|
|
101
|
+
await tx.query('update ota_updates set is_active=false,disabled_at=now() where update_id=$1', [pub]);
|
|
102
|
+
await tx.query('update ota_update_channels set active_update_id=null where runtime_version=$1', [scope]);
|
|
103
|
+
assert.deepEqual(await active(tx), [target]);
|
|
104
|
+
await tx.query('rollback');
|
|
105
|
+
} finally { await tx.query('rollback'); tx.release(); }
|
|
106
|
+
console.log('PASS: concurrent audience/activation serialization, atomic activation failure, public/targeted/widening semantics, blocked deactivation cleanup and promotion rejection.');
|
|
107
|
+
} finally {
|
|
108
|
+
await db.query('delete from ota_update_channels where runtime_version=$1', [scope]);
|
|
109
|
+
await db.query('delete from ota_updates where runtime_version=$1', [scope]);
|
|
110
|
+
await db.end();
|
|
111
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
// Explicit test DB only. All fixture changes are rolled back.
|
|
2
|
+
import assert from 'node:assert/strict';
|
|
3
|
+
import { randomUUID } from 'node:crypto';
|
|
4
|
+
import { Client } from 'pg';
|
|
5
|
+
if (!process.env.OTA_TEST_DATABASE_URL) throw new Error('Set OTA_TEST_DATABASE_URL');
|
|
6
|
+
const db = new Client({ connectionString: process.env.OTA_TEST_DATABASE_URL });
|
|
7
|
+
await db.connect();
|
|
8
|
+
const scope = `target-${randomUUID()}`, pub = randomUUID(), privateId = randomUUID(), device = 'a'.repeat(64), install = randomUUID();
|
|
9
|
+
const event = { installationId: install, deviceId: device, platform: 'android', channel: scope, runtimeVersion: scope, eventId: randomUUID(), sequence: 1, observedAt: new Date().toISOString(), updateId: null, nativeVersion: '1', nativeBuild: '1', isEmbeddedLaunch: true, isEmergencyLaunch: false };
|
|
10
|
+
const send = async (e) => (await db.query('select * from ota_record_heartbeat($1)', [e])).rows[0].result;
|
|
11
|
+
const latest = async (i = null, d = null, runtime = scope, platform = 'android', channel = scope) => (await db.query('select update_id from ota_latest_eligible_update($1,$2,$3,$4,$5)', [runtime, channel, platform, i, d])).rows[0]?.update_id;
|
|
12
|
+
async function fails(sql, args, pattern) {
|
|
13
|
+
await db.query('savepoint expected_failure');
|
|
14
|
+
await assert.rejects(db.query(sql, args), pattern);
|
|
15
|
+
await db.query('rollback to savepoint expected_failure');
|
|
16
|
+
}
|
|
17
|
+
try {
|
|
18
|
+
await db.query('begin');
|
|
19
|
+
for (const [id, date] of [[pub,'2026-10-20'],[privateId,'2026-10-21']]) await db.query(`insert into ota_updates(update_id,build_id,runtime_version,channel,platform,created_at,created_at_path,storage_bucket,storage_base_path,is_active,manifest) values($1,$1,$2,$2,'android',$3,'fixture','fixture','fixture',false,'{}')`, [id,scope,date]);
|
|
20
|
+
await db.query('select * from ota_replace_audience($1,$2,$3,$4,$5)', [privateId,'devices',JSON.stringify([install]),JSON.stringify([device]),1]);
|
|
21
|
+
await db.query('update ota_updates set is_active=true where update_id=$1', [pub]);
|
|
22
|
+
await db.query('update ota_updates set is_active=true where update_id=$1', [privateId]);
|
|
23
|
+
assert.equal(await latest(), pub); assert.equal(await latest(null,'b'.repeat(64)), pub);
|
|
24
|
+
assert.equal(await latest(install), privateId); assert.equal(await latest(null,device), privateId);
|
|
25
|
+
assert.equal(await latest(null,device,scope+'-other'), undefined); assert.equal(await latest(null,device,scope,'ios'), undefined); assert.equal(await latest(null,device,scope,'android',scope+'-other'), undefined);
|
|
26
|
+
await fails('select * from ota_replace_audience($1,$2,$3,$4,$5)',[privateId,'all','[]','[]',1],/AUDIENCE_CONFLICT/);
|
|
27
|
+
await fails('select * from ota_replace_audience($1,$2,$3,$4,$5)',[privateId,'devices','[]','[]',2],/INVALID_AUDIENCE/);
|
|
28
|
+
assert.equal(await send(event),'accepted'); assert.equal(await send({...event,deviceId:'b'.repeat(64)}),'duplicate');
|
|
29
|
+
assert.equal(await send({...event,sequence:2,eventId:randomUUID(),deviceId:undefined}),'accepted');
|
|
30
|
+
assert.equal((await db.query('select device_id from ota_device_state where installation_id=$1',[install])).rows[0].device_id,device);
|
|
31
|
+
const nextInstall=randomUUID(); await send({...event,installationId:nextInstall,eventId:randomUUID()});
|
|
32
|
+
const rows=(await db.query('select installation_id from ota_device_state where device_id=$1 and channel=$2',[device,scope])).rows;
|
|
33
|
+
assert.equal(rows.length,2,'Reinstall preserves the audience identity without merging installation analytics');
|
|
34
|
+
await db.query('select * from ota_replace_audience($1,$2,$3,$4,$5)',[privateId,'all','[]','[]',2]);
|
|
35
|
+
assert.equal(await latest(),privateId,'Widening exposes same artifact to everyone');
|
|
36
|
+
await db.query(`update ota_distribution_control set blocked=true,reason='targeting regression' where singleton_id=1`);
|
|
37
|
+
await fails('select * from ota_replace_audience($1,$2,$3,$4,$5)',[privateId,'devices',JSON.stringify([install]),'[]',3],/OTA_DISTRIBUTION_BLOCKED/);
|
|
38
|
+
console.log('PASS: selected/anonymous/fallback/scope/widening, audience CAS and global block, reinstall identity and duplicate heartbeat isolation.');
|
|
39
|
+
} finally { await db.query('rollback'); await db.end(); }
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// OTA_TEST_DATABASE_URL=... node scripts/tests/device-version-metadata.mjs
|
|
2
|
+
// Disposable/test database only. All fixtures and edits are rolled back.
|
|
3
|
+
import assert from 'node:assert/strict';
|
|
4
|
+
import { randomUUID } from 'node:crypto';
|
|
5
|
+
import { Client } from 'pg';
|
|
6
|
+
if (!process.env.OTA_TEST_DATABASE_URL) throw new Error('Set OTA_TEST_DATABASE_URL to a test database');
|
|
7
|
+
const db=new Client({connectionString:process.env.OTA_TEST_DATABASE_URL});
|
|
8
|
+
await db.connect();
|
|
9
|
+
const scope=`versions-${randomUUID()}`;
|
|
10
|
+
const updateId=randomUUID();
|
|
11
|
+
const event={installationId:randomUUID(),platform:'android',channel:scope,runtimeVersion:scope,eventId:randomUUID(),sequence:1,observedAt:new Date().toISOString(),updateId,nativeVersion:'9.9.9',nativeBuild:'1',isEmbeddedLaunch:false,isEmergencyLaunch:false};
|
|
12
|
+
async function report(){return (await db.query('select * from ota_devices_report_v2(7,null,$1)',[scope])).rows[0].report;}
|
|
13
|
+
async function send(value){await db.query('select * from ota_record_heartbeat($1::jsonb)',[JSON.stringify(value)]);}
|
|
14
|
+
try{
|
|
15
|
+
await db.query('begin');
|
|
16
|
+
await db.query(`insert into ota_updates(update_id,build_id,runtime_version,channel,platform,created_at,created_at_path,storage_bucket,storage_base_path,comment,manifest) values($1,$1,$2,$2,'android',now(),'fixture','fixture','fixture','NOT A VERSION',$3::jsonb)`,[updateId,scope,JSON.stringify({extra:{expoClient:{version:' 2.3.4 '}},expoClient:{version:'1.0.0'}})]);
|
|
17
|
+
await send(event);
|
|
18
|
+
let version=(await report()).versions[0];
|
|
19
|
+
assert.equal(version.app_version,'2.3.4');assert.equal(version.native_version,'9.9.9');assert.equal(version.update_label,'NOT A VERSION');
|
|
20
|
+
await db.query('update ota_updates set manifest=$1::jsonb where update_id=$2',[JSON.stringify({extra:{expoClient:{version:42}},expoClient:{version:' 1.2.3 '}}),updateId]);
|
|
21
|
+
assert.equal((await report()).versions[0].app_version,'1.2.3');
|
|
22
|
+
await db.query('update ota_updates set manifest=$1::jsonb where update_id=$2',[JSON.stringify({expoClient:{version:42}}),updateId]);
|
|
23
|
+
assert.equal((await report()).versions[0].app_version,null,'Do not substitute native version or message for missing OTA metadata');
|
|
24
|
+
await send({...event,installationId:randomUUID(),eventId:randomUUID(),runtimeVersion:scope+'-other'});
|
|
25
|
+
assert.equal((await report()).versions.find(v=>v.launch_kind==='unknown').app_version,null,'Never resolve version metadata across runtime scopes');
|
|
26
|
+
const embeddedId=randomUUID();
|
|
27
|
+
await db.query(`insert into ota_embedded_updates(embedded_update_id,app_version,created_at,channel,platform) values($1,'3.0.0',now(),$2,'android')`,[embeddedId,scope]);
|
|
28
|
+
await send({...event,installationId:randomUUID(),eventId:randomUUID(),updateId:embeddedId,isEmbeddedLaunch:true});
|
|
29
|
+
assert.equal((await report()).versions.find(v=>v.launch_kind==='embedded').app_version,'3.0.0');
|
|
30
|
+
console.log('PASS: manifest version precedence, malformed metadata fallback, no message/native substitution for OTA, runtime isolation, embedded version.');
|
|
31
|
+
}finally{await db.query('rollback');await db.end();}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
// Run against a disposable PostgreSQL database with the identity migration applied.
|
|
2
|
+
// OTA_TEST_DATABASE_URL=postgresql://... node scripts/tests/manifest-identity-concurrency.mjs
|
|
3
|
+
import assert from "node:assert/strict";
|
|
4
|
+
import { randomUUID } from "node:crypto";
|
|
5
|
+
import { setTimeout as delay } from "node:timers/promises";
|
|
6
|
+
import { Pool } from "pg";
|
|
7
|
+
|
|
8
|
+
if (!process.env.OTA_TEST_DATABASE_URL) {
|
|
9
|
+
throw new Error("OTA_TEST_DATABASE_URL must point to a disposable test database.");
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const pool = new Pool({
|
|
13
|
+
connectionString: process.env.OTA_TEST_DATABASE_URL,
|
|
14
|
+
max: 3,
|
|
15
|
+
connectionTimeoutMillis: 5000,
|
|
16
|
+
statement_timeout: 10000,
|
|
17
|
+
});
|
|
18
|
+
const scope = `identity-concurrency-${randomUUID()}`;
|
|
19
|
+
const updates = [randomUUID(), randomUUID()];
|
|
20
|
+
const firstSeen = "2026-01-01T00:00:00.000Z";
|
|
21
|
+
const upsert = `INSERT INTO public.ota_served_manifest_log
|
|
22
|
+
(served_manifest_id, update_id, runtime_version, channel, platform, created_at)
|
|
23
|
+
VALUES ($1, $2, $3, $3, 'android', $4)
|
|
24
|
+
ON CONFLICT (served_manifest_id) DO UPDATE SET
|
|
25
|
+
update_id = excluded.update_id, created_at = excluded.created_at`;
|
|
26
|
+
|
|
27
|
+
async function exerciseConcurrentRetry(winner, contender, conflicting) {
|
|
28
|
+
const manifestId = randomUUID();
|
|
29
|
+
const contenderPid = (await contender.query("select pg_backend_pid() as pid")).rows[0].pid;
|
|
30
|
+
let pending;
|
|
31
|
+
await winner.query("begin");
|
|
32
|
+
try {
|
|
33
|
+
await winner.query(upsert, [manifestId, updates[0], scope, firstSeen]);
|
|
34
|
+
pending = contender.query(upsert, [
|
|
35
|
+
manifestId,
|
|
36
|
+
updates[Number(conflicting)],
|
|
37
|
+
scope,
|
|
38
|
+
"2026-02-01T00:00:00.000Z",
|
|
39
|
+
]).then(() => ({ ok: true }), (error) => ({ ok: false, error }));
|
|
40
|
+
|
|
41
|
+
// Verify actual lock contention instead of relying on query launch order.
|
|
42
|
+
let waiting = false;
|
|
43
|
+
for (let attempt = 0; attempt < 100; attempt += 1) {
|
|
44
|
+
const { rows } = await pool.query(
|
|
45
|
+
"select wait_event_type from pg_stat_activity where pid = $1",
|
|
46
|
+
[contenderPid],
|
|
47
|
+
);
|
|
48
|
+
if (rows[0]?.wait_event_type === "Lock") {
|
|
49
|
+
waiting = true;
|
|
50
|
+
break;
|
|
51
|
+
}
|
|
52
|
+
await delay(20);
|
|
53
|
+
}
|
|
54
|
+
assert.ok(waiting, "Concurrent upsert must wait for the first writer");
|
|
55
|
+
await winner.query("commit");
|
|
56
|
+
const result = await pending;
|
|
57
|
+
if (conflicting) {
|
|
58
|
+
assert.equal(result.ok, false);
|
|
59
|
+
assert.equal(result.error.code, "23514");
|
|
60
|
+
assert.match(result.error.message, /^OTA_MANIFEST_IDENTITY_CONFLICT:/);
|
|
61
|
+
} else {
|
|
62
|
+
assert.equal(result.ok, true, result.error?.message);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const { rows } = await pool.query(
|
|
66
|
+
"select update_id, created_at from public.ota_served_manifest_log where served_manifest_id = $1",
|
|
67
|
+
[manifestId],
|
|
68
|
+
);
|
|
69
|
+
assert.equal(rows[0].update_id, updates[0]);
|
|
70
|
+
assert.equal(rows[0].created_at.toISOString(), firstSeen);
|
|
71
|
+
} finally {
|
|
72
|
+
await winner.query("rollback");
|
|
73
|
+
if (pending) await pending;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
try {
|
|
78
|
+
for (const id of updates) {
|
|
79
|
+
await pool.query(
|
|
80
|
+
`INSERT INTO public.ota_updates (
|
|
81
|
+
update_id, build_id, runtime_version, channel, platform, created_at,
|
|
82
|
+
created_at_path, storage_bucket, storage_base_path, manifest
|
|
83
|
+
) VALUES ($1, $1, $2, $2, 'android', now(), 'test', 'test', 'test', '{}')`,
|
|
84
|
+
[id, scope],
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
const winner = await pool.connect();
|
|
88
|
+
try {
|
|
89
|
+
const contender = await pool.connect();
|
|
90
|
+
try {
|
|
91
|
+
await exerciseConcurrentRetry(winner, contender, false);
|
|
92
|
+
await exerciseConcurrentRetry(winner, contender, true);
|
|
93
|
+
console.log("PASS: concurrent identical retry and conflicting upsert preserve identity and first-seen time.");
|
|
94
|
+
} finally {
|
|
95
|
+
contender.release();
|
|
96
|
+
}
|
|
97
|
+
} finally {
|
|
98
|
+
winner.release();
|
|
99
|
+
}
|
|
100
|
+
} finally {
|
|
101
|
+
try {
|
|
102
|
+
await pool.query("delete from public.ota_updates where update_id = any($1::uuid[])", [updates]);
|
|
103
|
+
} finally {
|
|
104
|
+
await pool.end();
|
|
105
|
+
}
|
|
106
|
+
}
|