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
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { describe, expect, it } from "vitest";
|
|
3
|
+
|
|
4
|
+
const migration = readFileSync(
|
|
5
|
+
new URL(
|
|
6
|
+
"../../../docs/migrations/2026-09-01-p2-simplify-ota-guard-policy.sql",
|
|
7
|
+
import.meta.url,
|
|
8
|
+
),
|
|
9
|
+
"utf8",
|
|
10
|
+
);
|
|
11
|
+
const schema = readFileSync(
|
|
12
|
+
new URL("../../../docs/migrations/schema.sql", import.meta.url),
|
|
13
|
+
"utf8",
|
|
14
|
+
);
|
|
15
|
+
const runbook = readFileSync(
|
|
16
|
+
new URL("../../../docs/ota-update-policy.md", import.meta.url),
|
|
17
|
+
"utf8",
|
|
18
|
+
);
|
|
19
|
+
|
|
20
|
+
describe("simple update policy migration", () => {
|
|
21
|
+
it.each([
|
|
22
|
+
["production migration", migration],
|
|
23
|
+
["canonical schema", schema],
|
|
24
|
+
])("defines the simple model in the %s", (_name, sql) => {
|
|
25
|
+
expect(sql).toMatch(/guard_action text null/i);
|
|
26
|
+
expect(sql).toMatch(/guard_payload jsonb null/i);
|
|
27
|
+
expect(sql).toMatch(/guard_action = btrim\(guard_action\)/i);
|
|
28
|
+
expect(sql).toMatch(/char_length\(guard_action\) between 1 and 100/i);
|
|
29
|
+
expect(sql).toMatch(/guard_action !~ '\[\[:cntrl:\]\]'/i);
|
|
30
|
+
expect(sql).toMatch(
|
|
31
|
+
/guard_payload is null or guard_action is not null/i,
|
|
32
|
+
);
|
|
33
|
+
expect(sql).toMatch(/drop column if exists guard_rules/i);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it("replaces the immutability trigger with both simple Guard columns", () => {
|
|
37
|
+
expect(migration).toMatch(
|
|
38
|
+
/drop trigger if exists trg_ota_updates_lock_published_policy/i,
|
|
39
|
+
);
|
|
40
|
+
expect(migration).toMatch(
|
|
41
|
+
/new\.guard_action is distinct from old\.guard_action/i,
|
|
42
|
+
);
|
|
43
|
+
expect(migration).toMatch(
|
|
44
|
+
/new\.guard_payload is distinct from old\.guard_payload/i,
|
|
45
|
+
);
|
|
46
|
+
expect(migration).toMatch(
|
|
47
|
+
/create trigger trg_ota_updates_lock_published_policy/i,
|
|
48
|
+
);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it("does not migrate old rule data", () => {
|
|
52
|
+
expect(migration).not.toMatch(
|
|
53
|
+
/insert\s+into|jsonb_array|jsonb_path|guard_rules\s*->/i,
|
|
54
|
+
);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it("has no rule column in the fresh ota_updates definition", () => {
|
|
58
|
+
const table = schema.slice(
|
|
59
|
+
schema.indexOf("create table if not exists public.ota_updates"),
|
|
60
|
+
schema.indexOf("-- Keep schema.sql safe as an upgrade path"),
|
|
61
|
+
);
|
|
62
|
+
expect(table).not.toMatch(/guard_rules/);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it("documents the required existing-installation order and skip condition", () => {
|
|
66
|
+
const catalog = runbook.indexOf(
|
|
67
|
+
"2026-09-01-p1-guard-actions.sql",
|
|
68
|
+
);
|
|
69
|
+
const simplification = runbook.indexOf(
|
|
70
|
+
"2026-09-01-p2-simplify-ota-guard-policy.sql",
|
|
71
|
+
);
|
|
72
|
+
const correction = runbook.indexOf(
|
|
73
|
+
"2026-09-01-policy-publication-correction.sql",
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
expect(catalog).toBeGreaterThanOrEqual(0);
|
|
77
|
+
expect(simplification).toBeGreaterThan(catalog);
|
|
78
|
+
expect(correction).toBeGreaterThan(simplification);
|
|
79
|
+
expect(runbook).toMatch(
|
|
80
|
+
/already successfully ran[\s\S]*verified that no drafts[\s\S]*may skip the third step/i,
|
|
81
|
+
);
|
|
82
|
+
expect(runbook).toMatch(
|
|
83
|
+
/correction must run third[\s\S]*guard_action[\s\S]*guard_payload/i,
|
|
84
|
+
);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it("keeps relevant migration basenames in dependency-safe lexical order", () => {
|
|
88
|
+
const dependencyOrder = [
|
|
89
|
+
"2026-09-01-ota-update-policy.sql",
|
|
90
|
+
"2026-09-01-p1-guard-actions.sql",
|
|
91
|
+
"2026-09-01-p2-simplify-ota-guard-policy.sql",
|
|
92
|
+
"2026-09-01-policy-publication-correction.sql",
|
|
93
|
+
];
|
|
94
|
+
|
|
95
|
+
expect([...dependencyOrder].sort()).toEqual(dependencyOrder);
|
|
96
|
+
});
|
|
97
|
+
});
|
|
@@ -41,6 +41,34 @@ const SIGNED_URL_TTL = parseInt(
|
|
|
41
41
|
10,
|
|
42
42
|
);
|
|
43
43
|
|
|
44
|
+
const JSONB_VALUE = Symbol("jsonb-value");
|
|
45
|
+
|
|
46
|
+
function jsonb(value) {
|
|
47
|
+
return {
|
|
48
|
+
[JSONB_VALUE]: true,
|
|
49
|
+
value,
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function isJsonbValue(value) {
|
|
54
|
+
return Boolean(
|
|
55
|
+
value && typeof value === "object" && value[JSONB_VALUE] === true,
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function toPostgresWriteValue(value) {
|
|
60
|
+
return isJsonbValue(value) ? JSON.stringify(value.value) : value;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function unwrapJsonbPayload(payload) {
|
|
64
|
+
return Object.fromEntries(
|
|
65
|
+
Object.entries(payload || {}).map(([key, value]) => [
|
|
66
|
+
key,
|
|
67
|
+
isJsonbValue(value) ? value.value : value,
|
|
68
|
+
]),
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
|
|
44
72
|
if (DATABASE_PROVIDER !== "pg" && DATABASE_PROVIDER !== "supabase") {
|
|
45
73
|
throw new Error(
|
|
46
74
|
`Unsupported DATABASE_PROVIDER value: ${DATABASE_PROVIDER}. Expected \"pg\" or \"supabase\".`,
|
|
@@ -50,6 +78,13 @@ if (DATABASE_PROVIDER !== "pg" && DATABASE_PROVIDER !== "supabase") {
|
|
|
50
78
|
const shouldUseSsl =
|
|
51
79
|
DATABASE_SSL === "true" || DATABASE_SSL === "1" || DATABASE_SSL === "require";
|
|
52
80
|
|
|
81
|
+
function assertSqlIdentifier(value) {
|
|
82
|
+
if (!/^[a-z_][a-z0-9_]*$/i.test(value)) {
|
|
83
|
+
throw new Error(`Invalid SQL identifier: ${value}`);
|
|
84
|
+
}
|
|
85
|
+
return value;
|
|
86
|
+
}
|
|
87
|
+
|
|
53
88
|
let pool = null;
|
|
54
89
|
let s3 = null;
|
|
55
90
|
let supabaseClient = null;
|
|
@@ -182,6 +217,11 @@ class TableQuery {
|
|
|
182
217
|
return this;
|
|
183
218
|
}
|
|
184
219
|
|
|
220
|
+
is(column, value) {
|
|
221
|
+
this.filters.push({ type: "is", column, value });
|
|
222
|
+
return this;
|
|
223
|
+
}
|
|
224
|
+
|
|
185
225
|
neq(column, value) {
|
|
186
226
|
this.filters.push({ type: "neq", column, value });
|
|
187
227
|
return this;
|
|
@@ -266,6 +306,21 @@ class TableQuery {
|
|
|
266
306
|
continue;
|
|
267
307
|
}
|
|
268
308
|
|
|
309
|
+
if (filter.type === "is") {
|
|
310
|
+
if (isNullLike(filter.value)) {
|
|
311
|
+
conditions.push(`${filter.column} IS NULL`);
|
|
312
|
+
} else if (filter.value === true) {
|
|
313
|
+
conditions.push(`${filter.column} IS TRUE`);
|
|
314
|
+
} else if (filter.value === false) {
|
|
315
|
+
conditions.push(`${filter.column} IS FALSE`);
|
|
316
|
+
} else {
|
|
317
|
+
throw new Error(
|
|
318
|
+
`Unsupported filter value: is ${String(filter.value)}`,
|
|
319
|
+
);
|
|
320
|
+
}
|
|
321
|
+
continue;
|
|
322
|
+
}
|
|
323
|
+
|
|
269
324
|
if (filter.type === "not") {
|
|
270
325
|
if (filter.operator === "is") {
|
|
271
326
|
conditions.push(
|
|
@@ -391,7 +446,7 @@ class TableQuery {
|
|
|
391
446
|
|
|
392
447
|
for (const [key, value] of entries) {
|
|
393
448
|
setParts.push(`${key} = $${index}`);
|
|
394
|
-
params.push(value);
|
|
449
|
+
params.push(toPostgresWriteValue(value));
|
|
395
450
|
index += 1;
|
|
396
451
|
}
|
|
397
452
|
|
|
@@ -434,7 +489,7 @@ class TableQuery {
|
|
|
434
489
|
}
|
|
435
490
|
|
|
436
491
|
const columns = entries.map(([key]) => key);
|
|
437
|
-
const values = entries.map(([, value]) => value);
|
|
492
|
+
const values = entries.map(([, value]) => toPostgresWriteValue(value));
|
|
438
493
|
const placeholders = values.map((_, idx) => `$${idx + 1}`);
|
|
439
494
|
|
|
440
495
|
const conflictColumns = (this.upsertConflict || "")
|
|
@@ -655,6 +710,11 @@ class SupabaseTableQuery {
|
|
|
655
710
|
return this;
|
|
656
711
|
}
|
|
657
712
|
|
|
713
|
+
is(column, value) {
|
|
714
|
+
this.builder = this.builder.is(column, value);
|
|
715
|
+
return this;
|
|
716
|
+
}
|
|
717
|
+
|
|
658
718
|
neq(column, value) {
|
|
659
719
|
this.builder = this.builder.neq(column, value);
|
|
660
720
|
return this;
|
|
@@ -676,7 +736,7 @@ class SupabaseTableQuery {
|
|
|
676
736
|
}
|
|
677
737
|
|
|
678
738
|
update(payload) {
|
|
679
|
-
this.builder = this.builder.update(payload
|
|
739
|
+
this.builder = this.builder.update(unwrapJsonbPayload(payload));
|
|
680
740
|
return this;
|
|
681
741
|
}
|
|
682
742
|
|
|
@@ -686,7 +746,7 @@ class SupabaseTableQuery {
|
|
|
686
746
|
}
|
|
687
747
|
|
|
688
748
|
upsert(payload, options = {}) {
|
|
689
|
-
this.builder = this.builder.upsert(payload
|
|
749
|
+
this.builder = this.builder.upsert(unwrapJsonbPayload(payload), options);
|
|
690
750
|
return this;
|
|
691
751
|
}
|
|
692
752
|
|
|
@@ -736,9 +796,7 @@ const supabase =
|
|
|
736
796
|
DATABASE_PROVIDER === "supabase"
|
|
737
797
|
? {
|
|
738
798
|
from(tableName) {
|
|
739
|
-
return new SupabaseTableQuery(
|
|
740
|
-
getSupabaseClient().from(tableName),
|
|
741
|
-
);
|
|
799
|
+
return new SupabaseTableQuery(getSupabaseClient().from(tableName));
|
|
742
800
|
},
|
|
743
801
|
storage: {
|
|
744
802
|
from(bucket) {
|
|
@@ -748,6 +806,12 @@ const supabase =
|
|
|
748
806
|
);
|
|
749
807
|
},
|
|
750
808
|
},
|
|
809
|
+
rpc(functionName, params = {}) {
|
|
810
|
+
return getSupabaseClient().rpc(
|
|
811
|
+
functionName,
|
|
812
|
+
unwrapJsonbPayload(params),
|
|
813
|
+
);
|
|
814
|
+
},
|
|
751
815
|
}
|
|
752
816
|
: {
|
|
753
817
|
from(tableName) {
|
|
@@ -758,6 +822,29 @@ const supabase =
|
|
|
758
822
|
return new BucketStorageQuery(bucket || SUPABASE_BUCKET);
|
|
759
823
|
},
|
|
760
824
|
},
|
|
825
|
+
async rpc(functionName, params = {}) {
|
|
826
|
+
try {
|
|
827
|
+
const safeFunctionName = assertSqlIdentifier(functionName);
|
|
828
|
+
const entries = Object.entries(params);
|
|
829
|
+
const placeholders = entries.map(
|
|
830
|
+
([name], index) =>
|
|
831
|
+
`${assertSqlIdentifier(name)} => $${index + 1}`,
|
|
832
|
+
);
|
|
833
|
+
const sql = `SELECT * FROM public.${safeFunctionName}(${placeholders.join(", ")})`;
|
|
834
|
+
const values = entries.map(([, value]) =>
|
|
835
|
+
toPostgresWriteValue(value),
|
|
836
|
+
);
|
|
837
|
+
const result = await getPool().query(sql, values);
|
|
838
|
+
return { data: result.rows, error: null };
|
|
839
|
+
} catch (error) {
|
|
840
|
+
return {
|
|
841
|
+
data: null,
|
|
842
|
+
error: {
|
|
843
|
+
message: error instanceof Error ? error.message : String(error),
|
|
844
|
+
},
|
|
845
|
+
};
|
|
846
|
+
}
|
|
847
|
+
},
|
|
761
848
|
};
|
|
762
849
|
|
|
763
|
-
export { supabase, SUPABASE_BUCKET, SIGNED_URL_TTL, DATABASE_PROVIDER };
|
|
850
|
+
export { supabase, jsonb, SUPABASE_BUCKET, SIGNED_URL_TTL, DATABASE_PROVIDER };
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
|
|
3
|
+
const poolQuery = vi.hoisted(() =>
|
|
4
|
+
vi.fn(async (sql: string, params: unknown[]) => {
|
|
5
|
+
void sql;
|
|
6
|
+
void params;
|
|
7
|
+
return {
|
|
8
|
+
rows: [] as Array<Record<string, unknown>>,
|
|
9
|
+
};
|
|
10
|
+
}),
|
|
11
|
+
);
|
|
12
|
+
const supabaseUpdate = vi.hoisted(() => vi.fn());
|
|
13
|
+
const supabaseUpsert = vi.hoisted(() => vi.fn());
|
|
14
|
+
|
|
15
|
+
const supabaseBuilder = vi.hoisted(() => {
|
|
16
|
+
const builder = {
|
|
17
|
+
update(payload: unknown) {
|
|
18
|
+
supabaseUpdate(payload);
|
|
19
|
+
return builder;
|
|
20
|
+
},
|
|
21
|
+
upsert(payload: unknown) {
|
|
22
|
+
supabaseUpsert(payload);
|
|
23
|
+
return builder;
|
|
24
|
+
},
|
|
25
|
+
eq() {
|
|
26
|
+
return builder;
|
|
27
|
+
},
|
|
28
|
+
select() {
|
|
29
|
+
return builder;
|
|
30
|
+
},
|
|
31
|
+
maybeSingle() {
|
|
32
|
+
return Promise.resolve({ data: null, error: null });
|
|
33
|
+
},
|
|
34
|
+
then(resolve: (value: unknown) => unknown) {
|
|
35
|
+
return Promise.resolve({ data: null, error: null }).then(resolve);
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
return builder;
|
|
39
|
+
});
|
|
40
|
+
const supabaseRpcCalls = vi.hoisted(
|
|
41
|
+
() =>
|
|
42
|
+
[] as Array<{
|
|
43
|
+
functionName: string;
|
|
44
|
+
params: Record<string, unknown>;
|
|
45
|
+
}>,
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
vi.mock("pg", () => ({
|
|
49
|
+
Pool: class Pool {
|
|
50
|
+
query(sql: string, params: unknown[]) {
|
|
51
|
+
return poolQuery(sql, params);
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
}));
|
|
55
|
+
vi.mock("@supabase/supabase-js", () => ({
|
|
56
|
+
createClient: () => ({
|
|
57
|
+
from: () => supabaseBuilder,
|
|
58
|
+
rpc(functionName: string, params: Record<string, unknown>) {
|
|
59
|
+
supabaseRpcCalls.push({ functionName, params });
|
|
60
|
+
return Promise.resolve({ data: [], error: null });
|
|
61
|
+
},
|
|
62
|
+
}),
|
|
63
|
+
}));
|
|
64
|
+
|
|
65
|
+
describe("common database abstraction", () => {
|
|
66
|
+
const originalProvider = process.env.DATABASE_PROVIDER;
|
|
67
|
+
const originalDatabaseUrl = process.env.DATABASE_URL;
|
|
68
|
+
const originalSupabaseUrl = process.env.SUPABASE_URL;
|
|
69
|
+
const originalSupabaseKey = process.env.SUPABASE_SERVICE_ROLE_KEY;
|
|
70
|
+
|
|
71
|
+
beforeEach(() => {
|
|
72
|
+
vi.resetModules();
|
|
73
|
+
poolQuery.mockClear();
|
|
74
|
+
supabaseUpdate.mockClear();
|
|
75
|
+
supabaseUpsert.mockClear();
|
|
76
|
+
supabaseRpcCalls.length = 0;
|
|
77
|
+
process.env.DATABASE_PROVIDER = "pg";
|
|
78
|
+
process.env.DATABASE_URL = "postgres://local/test";
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
afterEach(() => {
|
|
82
|
+
if (originalProvider === undefined) {
|
|
83
|
+
delete process.env.DATABASE_PROVIDER;
|
|
84
|
+
} else {
|
|
85
|
+
process.env.DATABASE_PROVIDER = originalProvider;
|
|
86
|
+
}
|
|
87
|
+
if (originalDatabaseUrl === undefined) {
|
|
88
|
+
delete process.env.DATABASE_URL;
|
|
89
|
+
} else {
|
|
90
|
+
process.env.DATABASE_URL = originalDatabaseUrl;
|
|
91
|
+
}
|
|
92
|
+
if (originalSupabaseUrl === undefined) {
|
|
93
|
+
delete process.env.SUPABASE_URL;
|
|
94
|
+
} else {
|
|
95
|
+
process.env.SUPABASE_URL = originalSupabaseUrl;
|
|
96
|
+
}
|
|
97
|
+
if (originalSupabaseKey === undefined) {
|
|
98
|
+
delete process.env.SUPABASE_SERVICE_ROLE_KEY;
|
|
99
|
+
} else {
|
|
100
|
+
process.env.SUPABASE_SERVICE_ROLE_KEY = originalSupabaseKey;
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it("translates null and boolean values to PostgreSQL IS clauses", async () => {
|
|
105
|
+
const { supabase } = await import("./supabase.js");
|
|
106
|
+
|
|
107
|
+
await supabase
|
|
108
|
+
.from("ota_updates")
|
|
109
|
+
.select("*")
|
|
110
|
+
.is("policy_published_at", null)
|
|
111
|
+
.is("is_active", false)
|
|
112
|
+
.execute();
|
|
113
|
+
|
|
114
|
+
expect(poolQuery).toHaveBeenCalledWith(
|
|
115
|
+
"SELECT * FROM ota_updates WHERE policy_published_at IS NULL AND is_active IS FALSE",
|
|
116
|
+
[],
|
|
117
|
+
);
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it("serializes wrapped JSONB payloads but preserves PostgreSQL arrays", async () => {
|
|
121
|
+
const payload = { message: "Ready" };
|
|
122
|
+
const { jsonb, supabase } = await import("./supabase.js");
|
|
123
|
+
|
|
124
|
+
await supabase
|
|
125
|
+
.from("ota_updates")
|
|
126
|
+
.update({
|
|
127
|
+
guard_payload: jsonb(payload),
|
|
128
|
+
scopes: ["updates:read", "updates:write"],
|
|
129
|
+
})
|
|
130
|
+
.eq("update_id", "update-id")
|
|
131
|
+
.execute();
|
|
132
|
+
|
|
133
|
+
expect(poolQuery).toHaveBeenCalledWith(
|
|
134
|
+
expect.stringContaining(
|
|
135
|
+
"SET guard_payload = $1, scopes = $2 WHERE update_id = $3",
|
|
136
|
+
),
|
|
137
|
+
[JSON.stringify(payload), ["updates:read", "updates:write"], "update-id"],
|
|
138
|
+
);
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it("unwraps JSONB payloads for Supabase without changing plain arrays", async () => {
|
|
142
|
+
const payload = { message: "Ready" };
|
|
143
|
+
vi.resetModules();
|
|
144
|
+
process.env.DATABASE_PROVIDER = "supabase";
|
|
145
|
+
process.env.SUPABASE_URL = "https://example.supabase.co";
|
|
146
|
+
process.env.SUPABASE_SERVICE_ROLE_KEY = "test-key";
|
|
147
|
+
const { jsonb, supabase } = await import("./supabase.js");
|
|
148
|
+
|
|
149
|
+
await supabase
|
|
150
|
+
.from("ota_updates")
|
|
151
|
+
.update({
|
|
152
|
+
guard_payload: jsonb(payload),
|
|
153
|
+
scopes: ["updates:read"],
|
|
154
|
+
})
|
|
155
|
+
.execute();
|
|
156
|
+
|
|
157
|
+
expect(supabaseUpdate).toHaveBeenCalledWith({
|
|
158
|
+
guard_payload: payload,
|
|
159
|
+
scopes: ["updates:read"],
|
|
160
|
+
});
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
it("calls PostgreSQL functions with validated named parameters", async () => {
|
|
164
|
+
vi.stubEnv("DATABASE_PROVIDER", "pg");
|
|
165
|
+
vi.stubEnv("DATABASE_URL", "postgres://example");
|
|
166
|
+
const { jsonb, supabase } = await import("./supabase.js");
|
|
167
|
+
|
|
168
|
+
await supabase.rpc("set_ota_distribution_control", {
|
|
169
|
+
p_blocked: true,
|
|
170
|
+
p_changed_by: jsonb({ id: "operator" }),
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
expect(poolQuery).toHaveBeenCalledWith(
|
|
174
|
+
"SELECT * FROM public.set_ota_distribution_control(p_blocked => $1, p_changed_by => $2)",
|
|
175
|
+
[true, '{"id":"operator"}'],
|
|
176
|
+
);
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
it("unwraps JSON parameters for Supabase RPC", async () => {
|
|
180
|
+
vi.stubEnv("DATABASE_PROVIDER", "supabase");
|
|
181
|
+
vi.stubEnv("SUPABASE_URL", "https://example.supabase.co");
|
|
182
|
+
vi.stubEnv("SUPABASE_SERVICE_ROLE_KEY", "test-key");
|
|
183
|
+
const { jsonb, supabase } = await import("./supabase.js");
|
|
184
|
+
|
|
185
|
+
await supabase.rpc("set_ota_distribution_control", {
|
|
186
|
+
p_changed_by: jsonb({ id: "operator" }),
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
expect(supabaseRpcCalls[0]).toEqual({
|
|
190
|
+
functionName: "set_ota_distribution_control",
|
|
191
|
+
params: { p_changed_by: { id: "operator" } },
|
|
192
|
+
});
|
|
193
|
+
});
|
|
194
|
+
});
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from "vitest";
|
|
2
|
+
import {
|
|
3
|
+
createManifestUpdatePolicy,
|
|
4
|
+
validateUpdatePolicy,
|
|
5
|
+
} from "./update-policy";
|
|
6
|
+
|
|
7
|
+
describe("simple update policy", () => {
|
|
8
|
+
it("validates and directly projects an unconditional Guard", () => {
|
|
9
|
+
const guard = {
|
|
10
|
+
action: "require-confirmation",
|
|
11
|
+
payload: { message: "Ready" },
|
|
12
|
+
};
|
|
13
|
+
expect(
|
|
14
|
+
createManifestUpdatePolicy({
|
|
15
|
+
delivery: "background",
|
|
16
|
+
guard,
|
|
17
|
+
policyVersion: 7,
|
|
18
|
+
}),
|
|
19
|
+
).toEqual({
|
|
20
|
+
schemaVersion: 1,
|
|
21
|
+
policyVersion: 7,
|
|
22
|
+
delivery: "background",
|
|
23
|
+
guard,
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it.each([null, { action: "" }, { action: " ", payload: null }])(
|
|
28
|
+
"omits Guard for %j",
|
|
29
|
+
(guard) => {
|
|
30
|
+
expect(
|
|
31
|
+
createManifestUpdatePolicy({
|
|
32
|
+
delivery: "manual",
|
|
33
|
+
guard,
|
|
34
|
+
policyVersion: 1,
|
|
35
|
+
}),
|
|
36
|
+
).toEqual({
|
|
37
|
+
schemaVersion: 1,
|
|
38
|
+
policyVersion: 1,
|
|
39
|
+
delivery: "manual",
|
|
40
|
+
});
|
|
41
|
+
},
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
it("rejects payload without an action", () => {
|
|
45
|
+
expect(() =>
|
|
46
|
+
validateUpdatePolicy({
|
|
47
|
+
delivery: "manual",
|
|
48
|
+
guard: { action: "", payload: { invalid: true } },
|
|
49
|
+
}),
|
|
50
|
+
).toThrow(/payload.*absent/);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it.each([" action", "action ", "bad\naction", "x".repeat(101)])(
|
|
54
|
+
"rejects invalid action %j",
|
|
55
|
+
(action) => {
|
|
56
|
+
expect(() =>
|
|
57
|
+
validateUpdatePolicy({
|
|
58
|
+
delivery: "manual",
|
|
59
|
+
guard: { action },
|
|
60
|
+
}),
|
|
61
|
+
).toThrow(/trimmed.*1-100.*control/);
|
|
62
|
+
},
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
it("enforces payload depth and byte limits", () => {
|
|
66
|
+
let deep: unknown = "value";
|
|
67
|
+
for (let index = 0; index < 12; index += 1) deep = { deep };
|
|
68
|
+
expect(() =>
|
|
69
|
+
validateUpdatePolicy({
|
|
70
|
+
delivery: "manual",
|
|
71
|
+
guard: { action: "action", payload: deep },
|
|
72
|
+
}),
|
|
73
|
+
).toThrow(/depth/);
|
|
74
|
+
expect(() =>
|
|
75
|
+
validateUpdatePolicy({
|
|
76
|
+
delivery: "manual",
|
|
77
|
+
guard: { action: "action", payload: "x".repeat(17 * 1024) },
|
|
78
|
+
}),
|
|
79
|
+
).toThrow(/16384/);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it("falls back safely for corrupt persisted policy", () => {
|
|
83
|
+
const onCorrupt = vi.fn();
|
|
84
|
+
expect(
|
|
85
|
+
createManifestUpdatePolicy({
|
|
86
|
+
delivery: "invalid",
|
|
87
|
+
guard: null,
|
|
88
|
+
policyVersion: 3,
|
|
89
|
+
onCorrupt,
|
|
90
|
+
}),
|
|
91
|
+
).toEqual({
|
|
92
|
+
schemaVersion: 1,
|
|
93
|
+
policyVersion: 3,
|
|
94
|
+
delivery: "manual",
|
|
95
|
+
});
|
|
96
|
+
expect(onCorrupt).toHaveBeenCalledOnce();
|
|
97
|
+
});
|
|
98
|
+
});
|