create-expo-updates-server 0.1.4 → 0.1.6
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/.env.docker.example +1 -0
- package/dist/template/.env.example +2 -0
- package/dist/template/README.md +4 -1
- package/dist/template/app/api/admin/distribution-control/route.test.ts +153 -0
- package/dist/template/app/api/admin/distribution-control/route.ts +111 -0
- package/dist/template/app/api/admin/guard-actions/[id]/route.test.ts +60 -0
- package/dist/template/app/api/admin/guard-actions/[id]/route.ts +32 -0
- package/dist/template/app/api/admin/guard-actions/route.test.ts +123 -0
- package/dist/template/app/api/admin/guard-actions/route.ts +66 -0
- package/dist/template/app/api/admin/updates/[encodedKey]/deactivate/route.ts +4 -1
- package/dist/template/app/api/admin/updates/[encodedKey]/policy/route.test.ts +171 -0
- package/dist/template/app/api/admin/updates/[encodedKey]/policy/route.ts +75 -0
- package/dist/template/app/api/admin/updates/[encodedKey]/promote/route.ts +4 -1
- package/dist/template/app/api/admin/updates/[encodedKey]/rollback/route.ts +4 -1
- package/dist/template/app/api/admin/updates/[encodedKey]/route.ts +4 -1
- package/dist/template/app/updates/[encodedKey]/page.tsx +74 -6
- package/dist/template/app/updates/page.tsx +22 -1
- package/dist/template/docs/getting-started.md +1 -0
- package/dist/template/docs/getting-started.ru.md +2 -1
- package/dist/template/docs/migrations/2026-09-01-ota-update-policy.sql +108 -0
- package/dist/template/docs/migrations/2026-09-01-p1-guard-actions.sql +19 -0
- package/dist/template/docs/migrations/2026-09-01-p2-simplify-ota-guard-policy.sql +69 -0
- package/dist/template/docs/migrations/2026-09-01-policy-publication-correction.sql +44 -0
- package/dist/template/docs/migrations/2026-09-02-ota-distribution-control.sql +377 -0
- package/dist/template/docs/migrations/schema.sql +539 -2
- package/dist/template/docs/operations-guide.md +52 -1
- package/dist/template/docs/ota-emergency-recovery.md +22 -0
- package/dist/template/docs/ota-update-policy.md +89 -0
- package/dist/template/docs/supabase-setup.md +2 -1
- package/dist/template/package-lock.json +2 -2
- package/dist/template/package.json +1 -1
- package/dist/template/src/client/admin-api.ts +80 -1
- package/dist/template/src/client/admin-types.ts +50 -2
- package/dist/template/src/client/distribution-control.tsx +231 -0
- package/dist/template/src/client/guard-action-combobox.test.ts +26 -0
- package/dist/template/src/client/guard-action-combobox.tsx +267 -0
- package/dist/template/src/client/guard-action-options.test.ts +62 -0
- package/dist/template/src/client/guard-action-options.ts +70 -0
- package/dist/template/src/client/update-policy-editor.test.ts +36 -0
- package/dist/template/src/client/update-policy-editor.tsx +436 -0
- package/dist/template/src/server/controllers/assets.test.ts +87 -0
- package/dist/template/src/server/controllers/assets.ts +26 -0
- package/dist/template/src/server/controllers/manifest.test.ts +413 -2
- package/dist/template/src/server/controllers/manifest.ts +103 -38
- package/dist/template/src/server/lib/admin-distribution-guard.test.ts +61 -0
- package/dist/template/src/server/lib/admin-update-policy.test.ts +176 -0
- package/dist/template/src/server/lib/admin-updates.test.ts +72 -0
- package/dist/template/src/server/lib/admin-updates.ts +169 -24
- package/dist/template/src/server/lib/distribution-control-migration.test.ts +94 -0
- package/dist/template/src/server/lib/distribution-control.test.ts +181 -0
- package/dist/template/src/server/lib/distribution-control.ts +259 -0
- package/dist/template/src/server/lib/guard-actions-migration.test.ts +56 -0
- package/dist/template/src/server/lib/guard-actions.test.ts +142 -0
- package/dist/template/src/server/lib/guard-actions.ts +106 -0
- package/dist/template/src/server/lib/ota-policy-migrations.test.ts +64 -0
- package/dist/template/src/server/lib/simple-policy-migration.test.ts +97 -0
- package/dist/template/src/server/lib/supabase.js +95 -8
- package/dist/template/src/server/lib/supabase.test.ts +194 -0
- package/dist/template/src/server/lib/update-policy.test.ts +98 -0
- package/dist/template/src/server/lib/update-policy.ts +171 -0
- package/dist/template/src/server/manifest/manifest-repository.ts +10 -1
- package/dist/template/src/server/manifest/manifest-response.ts +0 -16
- package/dist/template/src/server/manifest/manifest-values.test.ts +54 -0
- package/dist/template/src/server/manifest/manifest-values.ts +145 -0
- package/dist/template/tsconfig.json +4 -1
- package/dist/template-manifest.json +239 -59
- package/package.json +1 -1
|
@@ -1,5 +1,13 @@
|
|
|
1
|
-
import { supabase, SUPABASE_BUCKET } from "./supabase.js";
|
|
1
|
+
import { jsonb, supabase, SUPABASE_BUCKET } from "./supabase.js";
|
|
2
2
|
import { toPosixPath } from "./manifest-helpers.js";
|
|
3
|
+
import {
|
|
4
|
+
UpdateDeliveryMode,
|
|
5
|
+
UpdatePolicyInput,
|
|
6
|
+
UpdatePolicyValidationError,
|
|
7
|
+
validateUpdatePolicy,
|
|
8
|
+
} from "./update-policy";
|
|
9
|
+
import { getManifestAppVersion } from "../manifest/manifest-values";
|
|
10
|
+
import { assertOtaDistributionAllowed } from "./distribution-control";
|
|
3
11
|
|
|
4
12
|
let hasServedManifestLogTable: boolean | null = null;
|
|
5
13
|
|
|
@@ -107,6 +115,11 @@ export type OtaUpdateRow = {
|
|
|
107
115
|
assets_count: number;
|
|
108
116
|
launch_asset_path: string | null;
|
|
109
117
|
rolled_back_from_update_id: string | null;
|
|
118
|
+
delivery_mode: UpdateDeliveryMode;
|
|
119
|
+
guard_action: string | null;
|
|
120
|
+
guard_payload: unknown | null;
|
|
121
|
+
policy_version: number;
|
|
122
|
+
policy_published_at: string | null;
|
|
110
123
|
manifest: Record<string, unknown>;
|
|
111
124
|
inserted_at: string;
|
|
112
125
|
modified_at: string;
|
|
@@ -152,6 +165,11 @@ export type UpdateRecord = {
|
|
|
152
165
|
isIgnoredByRollback: boolean;
|
|
153
166
|
launchAssetPath: string | null;
|
|
154
167
|
assetsCount: number;
|
|
168
|
+
deliveryMode: UpdateDeliveryMode;
|
|
169
|
+
hasGuard: boolean;
|
|
170
|
+
policyVersion: number;
|
|
171
|
+
policyPublishedAt: string | null;
|
|
172
|
+
policyEditable: boolean;
|
|
155
173
|
};
|
|
156
174
|
|
|
157
175
|
export type UpdateDetail = UpdateRecord & {
|
|
@@ -229,24 +247,6 @@ function mapUpdateMeta(row: OtaUpdateRow): UpdateMetaState {
|
|
|
229
247
|
};
|
|
230
248
|
}
|
|
231
249
|
|
|
232
|
-
function getManifestAppVersion(
|
|
233
|
-
manifest: Record<string, unknown>,
|
|
234
|
-
): string | null {
|
|
235
|
-
const expoClient = manifest.expoClient;
|
|
236
|
-
if (
|
|
237
|
-
!expoClient ||
|
|
238
|
-
typeof expoClient !== "object" ||
|
|
239
|
-
Array.isArray(expoClient)
|
|
240
|
-
) {
|
|
241
|
-
return null;
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
const version = (expoClient as Record<string, unknown>).version;
|
|
245
|
-
return typeof version === "string" && version.trim()
|
|
246
|
-
? version.trim()
|
|
247
|
-
: null;
|
|
248
|
-
}
|
|
249
|
-
|
|
250
250
|
function channelLatestToJson(
|
|
251
251
|
channel: OtaChannelRow | null,
|
|
252
252
|
newestUpdateId?: string | null,
|
|
@@ -332,9 +332,132 @@ export function mapRowToRecord(
|
|
|
332
332
|
isIgnoredByRollback,
|
|
333
333
|
launchAssetPath: row.launch_asset_path,
|
|
334
334
|
assetsCount: row.assets_count,
|
|
335
|
+
deliveryMode:
|
|
336
|
+
row.delivery_mode === "background" ? "background" : "manual",
|
|
337
|
+
hasGuard:
|
|
338
|
+
typeof row.guard_action === "string" && row.guard_action.length > 0,
|
|
339
|
+
policyVersion:
|
|
340
|
+
Number.isInteger(row.policy_version) && row.policy_version > 0
|
|
341
|
+
? row.policy_version
|
|
342
|
+
: 1,
|
|
343
|
+
policyPublishedAt: row.policy_published_at || null,
|
|
344
|
+
policyEditable: !row.is_active && !row.policy_published_at,
|
|
345
|
+
};
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
export type UpdatePolicyRecord = UpdatePolicyInput & {
|
|
349
|
+
policyVersion: number;
|
|
350
|
+
publishedAt: string | null;
|
|
351
|
+
editable: boolean;
|
|
352
|
+
};
|
|
353
|
+
|
|
354
|
+
export class UpdatePolicyConflictError extends Error {}
|
|
355
|
+
export class UpdatePolicyPublishedError extends Error {}
|
|
356
|
+
|
|
357
|
+
export function assertExpectedPolicyVersion(
|
|
358
|
+
expectedPolicyVersion: unknown,
|
|
359
|
+
currentPolicyVersion?: number,
|
|
360
|
+
): asserts expectedPolicyVersion is number {
|
|
361
|
+
if (
|
|
362
|
+
!Number.isInteger(expectedPolicyVersion) ||
|
|
363
|
+
(expectedPolicyVersion as number) < 1
|
|
364
|
+
) {
|
|
365
|
+
throw new UpdatePolicyValidationError(
|
|
366
|
+
"expectedPolicyVersion must be a positive integer.",
|
|
367
|
+
);
|
|
368
|
+
}
|
|
369
|
+
if (
|
|
370
|
+
currentPolicyVersion !== undefined &&
|
|
371
|
+
expectedPolicyVersion !== currentPolicyVersion
|
|
372
|
+
) {
|
|
373
|
+
throw new UpdatePolicyConflictError(
|
|
374
|
+
`Policy version conflict. Current version is ${currentPolicyVersion}.`,
|
|
375
|
+
);
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
function rowToPolicy(row: OtaUpdateRow): UpdatePolicyRecord {
|
|
380
|
+
const validated = validateUpdatePolicy({
|
|
381
|
+
delivery: row.delivery_mode,
|
|
382
|
+
guard: row.guard_action
|
|
383
|
+
? {
|
|
384
|
+
action: row.guard_action,
|
|
385
|
+
...(row.guard_payload !== null
|
|
386
|
+
? { payload: row.guard_payload }
|
|
387
|
+
: {}),
|
|
388
|
+
}
|
|
389
|
+
: null,
|
|
390
|
+
});
|
|
391
|
+
return {
|
|
392
|
+
...validated,
|
|
393
|
+
policyVersion: row.policy_version,
|
|
394
|
+
publishedAt: row.policy_published_at,
|
|
395
|
+
editable: !row.is_active && !row.policy_published_at,
|
|
335
396
|
};
|
|
336
397
|
}
|
|
337
398
|
|
|
399
|
+
export async function getUpdatePolicyByKey(
|
|
400
|
+
key: string,
|
|
401
|
+
): Promise<UpdatePolicyRecord> {
|
|
402
|
+
return rowToPolicy(await getUpdateRowById(extractUpdateIdFromKey(key)));
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
export async function replaceUpdatePolicyByKey(
|
|
406
|
+
key: string,
|
|
407
|
+
input: unknown,
|
|
408
|
+
expectedPolicyVersion: unknown,
|
|
409
|
+
): Promise<UpdatePolicyRecord> {
|
|
410
|
+
assertExpectedPolicyVersion(expectedPolicyVersion);
|
|
411
|
+
const updateId = extractUpdateIdFromKey(key);
|
|
412
|
+
const row = await getUpdateRowById(updateId);
|
|
413
|
+
if (row.is_active || row.policy_published_at) {
|
|
414
|
+
throw new UpdatePolicyPublishedError(
|
|
415
|
+
"Update policy is immutable after publication.",
|
|
416
|
+
);
|
|
417
|
+
}
|
|
418
|
+
assertExpectedPolicyVersion(expectedPolicyVersion, row.policy_version);
|
|
419
|
+
|
|
420
|
+
const policy = validateUpdatePolicy(input);
|
|
421
|
+
const { data, error } = await supabase
|
|
422
|
+
.from("ota_updates")
|
|
423
|
+
.update({
|
|
424
|
+
delivery_mode: policy.delivery,
|
|
425
|
+
guard_action: policy.guard?.action || null,
|
|
426
|
+
guard_payload:
|
|
427
|
+
policy.guard?.payload !== undefined
|
|
428
|
+
? jsonb(policy.guard.payload)
|
|
429
|
+
: null,
|
|
430
|
+
policy_version: row.policy_version + 1,
|
|
431
|
+
})
|
|
432
|
+
.eq("update_id", updateId)
|
|
433
|
+
.eq("is_active", false)
|
|
434
|
+
.eq("policy_version", row.policy_version)
|
|
435
|
+
.is("policy_published_at", null)
|
|
436
|
+
.select("*")
|
|
437
|
+
.maybeSingle();
|
|
438
|
+
|
|
439
|
+
if (error) {
|
|
440
|
+
if (error.message?.toLowerCase().includes("immutable")) {
|
|
441
|
+
throw new UpdatePolicyPublishedError(
|
|
442
|
+
"Update policy is immutable after publication.",
|
|
443
|
+
);
|
|
444
|
+
}
|
|
445
|
+
throw new Error(`Failed to update policy: ${error.message}`);
|
|
446
|
+
}
|
|
447
|
+
if (!data) {
|
|
448
|
+
const current = await getUpdateRowById(updateId);
|
|
449
|
+
if (current.is_active || current.policy_published_at) {
|
|
450
|
+
throw new UpdatePolicyPublishedError(
|
|
451
|
+
"Update policy is immutable after publication.",
|
|
452
|
+
);
|
|
453
|
+
}
|
|
454
|
+
throw new UpdatePolicyConflictError(
|
|
455
|
+
`Policy version conflict. Current version is ${current.policy_version}.`,
|
|
456
|
+
);
|
|
457
|
+
}
|
|
458
|
+
return rowToPolicy(data as OtaUpdateRow);
|
|
459
|
+
}
|
|
460
|
+
|
|
338
461
|
function compareValues(a: string | number, b: string | number): number {
|
|
339
462
|
if (a < b) {
|
|
340
463
|
return -1;
|
|
@@ -830,16 +953,23 @@ export async function updateJsonFileByKey(
|
|
|
830
953
|
| "channel-latest.json",
|
|
831
954
|
nextJson: unknown,
|
|
832
955
|
) {
|
|
833
|
-
const updateId = extractUpdateIdFromKey(key);
|
|
834
|
-
const row = await getUpdateRowById(updateId);
|
|
835
|
-
const scope = makeScopeFromRow(row);
|
|
836
|
-
|
|
837
956
|
if (!nextJson || typeof nextJson !== "object" || Array.isArray(nextJson)) {
|
|
838
957
|
throw new Error("content must be a JSON object");
|
|
839
958
|
}
|
|
840
959
|
|
|
841
960
|
const content = nextJson as Record<string, unknown>;
|
|
842
961
|
|
|
962
|
+
if (
|
|
963
|
+
fileName === "channel-latest.json" ||
|
|
964
|
+
(fileName === "update-meta.json" && content.isActive !== false)
|
|
965
|
+
) {
|
|
966
|
+
await assertOtaDistributionAllowed();
|
|
967
|
+
}
|
|
968
|
+
|
|
969
|
+
const updateId = extractUpdateIdFromKey(key);
|
|
970
|
+
const row = await getUpdateRowById(updateId);
|
|
971
|
+
const scope = makeScopeFromRow(row);
|
|
972
|
+
|
|
843
973
|
if (fileName === "update-info.json") {
|
|
844
974
|
const assets = Array.isArray(content.assets) ? content.assets : [];
|
|
845
975
|
const launchAssetPath =
|
|
@@ -897,7 +1027,12 @@ export async function updateJsonFileByKey(
|
|
|
897
1027
|
|
|
898
1028
|
const { error } = await supabase
|
|
899
1029
|
.from("ota_updates")
|
|
900
|
-
.update(
|
|
1030
|
+
.update({
|
|
1031
|
+
...parsedMeta,
|
|
1032
|
+
...(parsedMeta.is_active && !row.policy_published_at
|
|
1033
|
+
? { policy_published_at: parsedMeta.updated_at }
|
|
1034
|
+
: {}),
|
|
1035
|
+
})
|
|
901
1036
|
.eq("update_id", updateId)
|
|
902
1037
|
.execute();
|
|
903
1038
|
|
|
@@ -951,6 +1086,7 @@ export async function rollbackToUpdateByKey(
|
|
|
951
1086
|
key: string,
|
|
952
1087
|
options?: RollbackOptions,
|
|
953
1088
|
) {
|
|
1089
|
+
await assertOtaDistributionAllowed();
|
|
954
1090
|
const updateId = extractUpdateIdFromKey(key);
|
|
955
1091
|
const row = await getUpdateRowById(updateId);
|
|
956
1092
|
const scope = makeScopeFromRow(row);
|
|
@@ -1028,6 +1164,7 @@ export async function rollbackToUpdateByKey(
|
|
|
1028
1164
|
is_active: true,
|
|
1029
1165
|
updated_at: now,
|
|
1030
1166
|
disabled_at: null,
|
|
1167
|
+
policy_published_at: row.policy_published_at || now,
|
|
1031
1168
|
})
|
|
1032
1169
|
.eq("update_id", updateId)
|
|
1033
1170
|
.execute();
|
|
@@ -1089,6 +1226,7 @@ export async function rollbackToUpdateByKey(
|
|
|
1089
1226
|
}
|
|
1090
1227
|
|
|
1091
1228
|
export async function promoteLatestByKey(key: string) {
|
|
1229
|
+
await assertOtaDistributionAllowed();
|
|
1092
1230
|
const updateId = extractUpdateIdFromKey(key);
|
|
1093
1231
|
const row = await getUpdateRowById(updateId);
|
|
1094
1232
|
const scope = makeScopeFromRow(row);
|
|
@@ -1139,6 +1277,7 @@ export async function promoteLatestByKey(key: string) {
|
|
|
1139
1277
|
is_active: true,
|
|
1140
1278
|
updated_at: promoteNow,
|
|
1141
1279
|
disabled_at: null,
|
|
1280
|
+
policy_published_at: row.policy_published_at || promoteNow,
|
|
1142
1281
|
})
|
|
1143
1282
|
.eq("update_id", latestUpdateId)
|
|
1144
1283
|
.execute();
|
|
@@ -1180,6 +1319,9 @@ export async function promoteLatestByKey(key: string) {
|
|
|
1180
1319
|
}
|
|
1181
1320
|
|
|
1182
1321
|
export async function setUpdateDisabledByKey(key: string, disabled: boolean) {
|
|
1322
|
+
if (!disabled) {
|
|
1323
|
+
await assertOtaDistributionAllowed();
|
|
1324
|
+
}
|
|
1183
1325
|
const updateId = extractUpdateIdFromKey(key);
|
|
1184
1326
|
const row = await getUpdateRowById(updateId);
|
|
1185
1327
|
const scope = makeScopeFromRow(row);
|
|
@@ -1232,6 +1374,9 @@ export async function setUpdateDisabledByKey(key: string, disabled: boolean) {
|
|
|
1232
1374
|
is_active: !disabled,
|
|
1233
1375
|
updated_at: now,
|
|
1234
1376
|
disabled_at: disabled ? now : null,
|
|
1377
|
+
...(!disabled && !row.policy_published_at
|
|
1378
|
+
? { policy_published_at: now }
|
|
1379
|
+
: {}),
|
|
1235
1380
|
})
|
|
1236
1381
|
.eq("update_id", updateId)
|
|
1237
1382
|
.execute();
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { resolve } from "node:path";
|
|
3
|
+
import { describe, expect, it } from "vitest";
|
|
4
|
+
|
|
5
|
+
const migrationSql = readFileSync(
|
|
6
|
+
resolve(
|
|
7
|
+
process.cwd(),
|
|
8
|
+
"docs/migrations/2026-09-02-ota-distribution-control.sql",
|
|
9
|
+
),
|
|
10
|
+
"utf8",
|
|
11
|
+
);
|
|
12
|
+
|
|
13
|
+
const canonicalSchemaSql = readFileSync(
|
|
14
|
+
resolve(process.cwd(), "docs/migrations/schema.sql"),
|
|
15
|
+
"utf8",
|
|
16
|
+
);
|
|
17
|
+
|
|
18
|
+
describe.each([
|
|
19
|
+
["distribution-control migration", migrationSql],
|
|
20
|
+
["canonical schema", canonicalSchemaSql],
|
|
21
|
+
])("%s database guard", (_name, sql) => {
|
|
22
|
+
it("serializes switch writes and guarded mutations on one transaction lock", () => {
|
|
23
|
+
expect(sql).toContain(
|
|
24
|
+
"pg_catalog.pg_advisory_xact_lock(20260902, 1)",
|
|
25
|
+
);
|
|
26
|
+
expect(sql).toContain(
|
|
27
|
+
"before insert or update or delete on public.ota_distribution_control",
|
|
28
|
+
);
|
|
29
|
+
expect(sql).toContain("before insert on public.ota_updates");
|
|
30
|
+
expect(sql).toContain(
|
|
31
|
+
"before update of is_active on public.ota_updates",
|
|
32
|
+
);
|
|
33
|
+
expect(sql).toContain(
|
|
34
|
+
"on public.ota_update_channels\nfor each statement",
|
|
35
|
+
);
|
|
36
|
+
|
|
37
|
+
const rpc = sql.slice(
|
|
38
|
+
sql.indexOf(
|
|
39
|
+
"create or replace function public.set_ota_distribution_control(",
|
|
40
|
+
),
|
|
41
|
+
);
|
|
42
|
+
expect(rpc).toContain(
|
|
43
|
+
"perform public.lock_ota_distribution_control();",
|
|
44
|
+
);
|
|
45
|
+
expect(sql).toContain(
|
|
46
|
+
"perform public.assert_ota_distribution_mutation_allowed(",
|
|
47
|
+
);
|
|
48
|
+
expect(sql).toMatch(
|
|
49
|
+
/from public\.ota_distribution_control\s+where singleton_id = 1\s+for share;/,
|
|
50
|
+
);
|
|
51
|
+
expect(rpc).toMatch(
|
|
52
|
+
/from public\.ota_distribution_control\s+where singleton_id = 1\s+for update;/,
|
|
53
|
+
);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it("guards activation while leaving deactivation and inactive inserts alone", () => {
|
|
57
|
+
expect(sql).toContain("tg_op = 'INSERT' and not new.is_active");
|
|
58
|
+
expect(sql).toContain(
|
|
59
|
+
"new.is_active and not old.is_active",
|
|
60
|
+
);
|
|
61
|
+
expect(sql).toContain(
|
|
62
|
+
"before insert or update on public.ota_updates",
|
|
63
|
+
);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it("guards every channel field that can alter effective distribution", () => {
|
|
67
|
+
for (const field of [
|
|
68
|
+
"runtime_version",
|
|
69
|
+
"channel",
|
|
70
|
+
"platform",
|
|
71
|
+
"latest_update_id",
|
|
72
|
+
"latest_created_at",
|
|
73
|
+
"latest_created_at_path",
|
|
74
|
+
"active_update_id",
|
|
75
|
+
"active_changed_at",
|
|
76
|
+
"served_manifest_id",
|
|
77
|
+
"served_manifest_changed_at",
|
|
78
|
+
]) {
|
|
79
|
+
expect(sql).toMatch(
|
|
80
|
+
new RegExp(
|
|
81
|
+
`new\\.${field}\\s+is distinct from old\\.${field}`,
|
|
82
|
+
),
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
expect(sql).toContain(
|
|
86
|
+
"new.latest_update_id is null and new.active_update_id is null",
|
|
87
|
+
);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it("raises a stable database error for blocked distribution", () => {
|
|
91
|
+
expect(sql).toContain("errcode = 'P0OTA'");
|
|
92
|
+
expect(sql).toContain("OTA_DISTRIBUTION_BLOCKED:");
|
|
93
|
+
});
|
|
94
|
+
});
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
|
|
3
|
+
const database = vi.hoisted(() => ({
|
|
4
|
+
row: {
|
|
5
|
+
blocked: false,
|
|
6
|
+
version: 1,
|
|
7
|
+
reason: null as string | null,
|
|
8
|
+
changed_at: "2026-09-02T00:00:00.000Z" as string | Date,
|
|
9
|
+
changed_by: {
|
|
10
|
+
type: "system",
|
|
11
|
+
id: "migration",
|
|
12
|
+
label: "Database migration",
|
|
13
|
+
},
|
|
14
|
+
},
|
|
15
|
+
readError: null as { message: string } | null,
|
|
16
|
+
rpcError: null as { message: string } | null,
|
|
17
|
+
rpcParams: null as Record<string, unknown> | null,
|
|
18
|
+
}));
|
|
19
|
+
|
|
20
|
+
vi.mock("./supabase.js", () => ({
|
|
21
|
+
jsonb: (value: unknown) => value,
|
|
22
|
+
supabase: {
|
|
23
|
+
from: () => ({
|
|
24
|
+
select: () => ({
|
|
25
|
+
eq: () => ({
|
|
26
|
+
maybeSingle: async () => ({
|
|
27
|
+
data: database.readError ? null : database.row,
|
|
28
|
+
error: database.readError,
|
|
29
|
+
}),
|
|
30
|
+
}),
|
|
31
|
+
}),
|
|
32
|
+
}),
|
|
33
|
+
rpc: async (_name: string, params: Record<string, unknown>) => {
|
|
34
|
+
database.rpcParams = params;
|
|
35
|
+
if (database.rpcError) {
|
|
36
|
+
return { data: null, error: database.rpcError };
|
|
37
|
+
}
|
|
38
|
+
database.row = {
|
|
39
|
+
...database.row,
|
|
40
|
+
blocked: params.p_blocked === true,
|
|
41
|
+
version: database.row.version + 1,
|
|
42
|
+
reason:
|
|
43
|
+
typeof params.p_reason === "string" ? params.p_reason : null,
|
|
44
|
+
changed_at: new Date("2026-09-02T00:01:00.000Z"),
|
|
45
|
+
changed_by: params.p_changed_by as typeof database.row.changed_by,
|
|
46
|
+
};
|
|
47
|
+
return { data: [database.row], error: null };
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
}));
|
|
51
|
+
|
|
52
|
+
import {
|
|
53
|
+
DistributionControlConflictError,
|
|
54
|
+
DistributionControlValidationError,
|
|
55
|
+
getDistributionControlState,
|
|
56
|
+
isOtaDistributionBlocked,
|
|
57
|
+
setDistributionControlState,
|
|
58
|
+
} from "./distribution-control";
|
|
59
|
+
|
|
60
|
+
const principal = {
|
|
61
|
+
type: "session" as const,
|
|
62
|
+
id: "admin-id",
|
|
63
|
+
label: "operator",
|
|
64
|
+
role: "operator",
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
describe("OTA distribution control service", () => {
|
|
68
|
+
beforeEach(() => {
|
|
69
|
+
database.row = {
|
|
70
|
+
blocked: false,
|
|
71
|
+
version: 1,
|
|
72
|
+
reason: null,
|
|
73
|
+
changed_at: "2026-09-02T00:00:00.000Z",
|
|
74
|
+
changed_by: {
|
|
75
|
+
type: "system",
|
|
76
|
+
id: "migration",
|
|
77
|
+
label: "Database migration",
|
|
78
|
+
},
|
|
79
|
+
};
|
|
80
|
+
database.readError = null;
|
|
81
|
+
database.rpcError = null;
|
|
82
|
+
database.rpcParams = null;
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it("reads and maps the singleton without a process-local fallback", async () => {
|
|
86
|
+
await expect(getDistributionControlState()).resolves.toMatchObject({
|
|
87
|
+
blocked: false,
|
|
88
|
+
version: 1,
|
|
89
|
+
reason: null,
|
|
90
|
+
});
|
|
91
|
+
await expect(isOtaDistributionBlocked()).resolves.toBe(false);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it("normalizes a PostgreSQL timestamptz Date to an ISO string", async () => {
|
|
95
|
+
database.row.changed_at = new Date("2026-09-01T21:31:08.722Z");
|
|
96
|
+
|
|
97
|
+
await expect(getDistributionControlState()).resolves.toMatchObject({
|
|
98
|
+
changedAt: "2026-09-01T21:31:08.722Z",
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it("rejects invalid changed_at strings and Dates", async () => {
|
|
103
|
+
database.row.changed_at = "not-a-date";
|
|
104
|
+
await expect(getDistributionControlState()).rejects.toThrow(
|
|
105
|
+
"Invalid OTA distribution control changed_at.",
|
|
106
|
+
);
|
|
107
|
+
|
|
108
|
+
database.row.changed_at = new Date(Number.NaN);
|
|
109
|
+
await expect(getDistributionControlState()).rejects.toThrow(
|
|
110
|
+
"Invalid OTA distribution control changed_at.",
|
|
111
|
+
);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it("does not interpret a database error or missing singleton as active", async () => {
|
|
115
|
+
database.readError = { message: "relation does not exist" };
|
|
116
|
+
await expect(isOtaDistributionBlocked()).rejects.toThrow(
|
|
117
|
+
"Failed to read OTA distribution control",
|
|
118
|
+
);
|
|
119
|
+
|
|
120
|
+
database.readError = null;
|
|
121
|
+
const savedRow = database.row;
|
|
122
|
+
Object.assign(database, { row: null });
|
|
123
|
+
await expect(getDistributionControlState()).rejects.toThrow("missing");
|
|
124
|
+
database.row = savedRow;
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it("validates and atomically delegates state plus principal audit metadata", async () => {
|
|
128
|
+
const state = await setDistributionControlState({
|
|
129
|
+
blocked: true,
|
|
130
|
+
reason: " INC-42 bad rollout ",
|
|
131
|
+
expectedVersion: 1,
|
|
132
|
+
principal,
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
expect(state).toMatchObject({
|
|
136
|
+
blocked: true,
|
|
137
|
+
version: 2,
|
|
138
|
+
reason: "INC-42 bad rollout",
|
|
139
|
+
changedAt: "2026-09-02T00:01:00.000Z",
|
|
140
|
+
changedBy: principal,
|
|
141
|
+
});
|
|
142
|
+
expect(database.rpcParams).toMatchObject({
|
|
143
|
+
p_blocked: true,
|
|
144
|
+
p_reason: "INC-42 bad rollout",
|
|
145
|
+
p_expected_version: 1,
|
|
146
|
+
p_changed_by: principal,
|
|
147
|
+
});
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
it("requires a bounded reason and positive expectedVersion", async () => {
|
|
151
|
+
await expect(
|
|
152
|
+
setDistributionControlState({
|
|
153
|
+
blocked: true,
|
|
154
|
+
reason: " ",
|
|
155
|
+
expectedVersion: 1,
|
|
156
|
+
principal,
|
|
157
|
+
}),
|
|
158
|
+
).rejects.toBeInstanceOf(DistributionControlValidationError);
|
|
159
|
+
await expect(
|
|
160
|
+
setDistributionControlState({
|
|
161
|
+
blocked: false,
|
|
162
|
+
expectedVersion: 0,
|
|
163
|
+
principal,
|
|
164
|
+
}),
|
|
165
|
+
).rejects.toBeInstanceOf(DistributionControlValidationError);
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
it("maps optimistic database conflicts to a typed 409 error", async () => {
|
|
169
|
+
database.rpcError = {
|
|
170
|
+
message: "OTA distribution control version conflict",
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
await expect(
|
|
174
|
+
setDistributionControlState({
|
|
175
|
+
blocked: false,
|
|
176
|
+
expectedVersion: 1,
|
|
177
|
+
principal,
|
|
178
|
+
}),
|
|
179
|
+
).rejects.toBeInstanceOf(DistributionControlConflictError);
|
|
180
|
+
});
|
|
181
|
+
});
|