@marktiderman/genesis-cli 1.20.0 → 1.21.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.
Files changed (33) hide show
  1. package/README.md +28 -0
  2. package/dist/cli.js +89 -2
  3. package/dist/cli.js.map +1 -1
  4. package/dist/doctor.js +34 -0
  5. package/dist/doctor.js.map +1 -1
  6. package/dist/genesis-catalog.json +83 -14
  7. package/dist/index.js +34 -0
  8. package/dist/index.js.map +1 -1
  9. package/dist/starter/README.md +8 -5
  10. package/dist/starter/__tests__/components/setup.ts +11 -0
  11. package/dist/starter/__tests__/starter.test.ts +19 -0
  12. package/dist/starter/package.json +4 -3
  13. package/dist/starter/src/lib/app.ts +13 -5
  14. package/dist/starter/src/lib/define-starter.ts +1 -1
  15. package/dist/starter/src/lib/devtools-overlay.ts +3 -4
  16. package/dist/starter/src/lib/native-modules.ts +5 -1
  17. package/dist/starter/src/lib/offline.ts +14 -0
  18. package/dist/starter/supabase/migrations/20261024000750_support_default_tenant.sql +18 -0
  19. package/dist/starter/supabase/migrations/20261024000800_genesis_support_tickets.sql +800 -0
  20. package/dist/starter/supabase/migrations/20261025000070_starter_tenancy.sql +26 -0
  21. package/dist/starter/supabase/migrations/20261025000080_genesis_chat_tenants.sql +716 -0
  22. package/dist/starter/supabase/tests/database/chat_people.test.sql +1 -1
  23. package/dist/starter/supabase/tests/database/genesis_chat.test.sql +10 -2
  24. package/dist/starter/supabase/tests/database/genesis_chat_coaching.test.sql +8 -0
  25. package/dist/starter/supabase/tests/database/genesis_chat_search_threads.test.sql +8 -0
  26. package/dist/starter/supabase/tests/database/genesis_chat_tenants.test.sql +259 -0
  27. package/dist/starter/supabase/tests/database/genesis_support.test.sql +15 -3
  28. package/dist/starter/supabase/tests/database/genesis_support_findings.test.sql +10 -0
  29. package/dist/starter/supabase/tests/database/genesis_support_tickets.test.sql +327 -0
  30. package/dist/starter/supabase/tests/database/starter_tenancy.test.sql +19 -0
  31. package/dist/vite.js +34 -0
  32. package/dist/vite.js.map +1 -1
  33. package/package.json +2 -2
@@ -0,0 +1,327 @@
1
+ -- @feat:support-feedback
2
+ -- pgTAP test for 0004_genesis_support_tickets.sql (after 0006_genesis_support.sql and 0003). Copy
3
+ -- into your app's `supabase/tests/database/` and run `supabase test db`. It writes only its own
4
+ -- users, tickets and agents and rolls everything back. The app's hooks
5
+ -- (private.genesis_support_default_tenant, private.genesis_viewer_people,
6
+ -- private.genesis_tenant_admin, private.genesis_support_staff,
7
+ -- private.genesis_support_report_handler) are replaced inside this transaction only.
8
+ --
9
+ -- Tenants: d (the app's own, the default), ta and tb (two businesses).
10
+ -- People: a1 is a client of ta; b2 a client of tb; c3 an agent of ta; e5 an agent of tb;
11
+ -- f6 the app's staff; d7 a client of both ta and tb; c9 the admin of ta.
12
+ begin;
13
+ create extension if not exists pgtap with schema extensions;
14
+ select plan(57);
15
+
16
+ create function pg_temp.tenant(n text) returns uuid language sql immutable as $$
17
+ select case n when 'd' then '00000000-0000-4000-8000-00000000000f'::uuid
18
+ when 'ta' then '00000000-0000-4000-8000-0000000000a0'::uuid
19
+ else '00000000-0000-4000-8000-0000000000b0'::uuid end $$;
20
+ create function pg_temp.person(n text) returns uuid language sql immutable as $$
21
+ select ('00000000-0000-4000-8000-0000000004' || n)::uuid $$;
22
+ create function pg_temp.record(n text) returns uuid language sql immutable as $$
23
+ select ('00000000-0000-4000-8000-0000000005' || n)::uuid $$;
24
+ create function pg_temp.sign_in(n text) returns void language plpgsql as $$
25
+ begin
26
+ perform set_config('request.jwt.claims',
27
+ json_build_object('sub', pg_temp.person(n), 'role', 'authenticated')::text, true);
28
+ perform set_config('request.jwt.claim.sub', pg_temp.person(n)::text, true);
29
+ end $$;
30
+ create function pg_temp.sign_out() returns void language plpgsql as $$
31
+ begin
32
+ perform set_config('request.jwt.claims', '', true);
33
+ perform set_config('request.jwt.claim.sub', '', true);
34
+ end $$;
35
+ create temp table ids (name text primary key, id uuid);
36
+ grant select on ids to authenticated;
37
+ create function pg_temp.id(n text) returns uuid language sql stable as $$ select id from ids where name = n $$;
38
+
39
+ insert into auth.users (id, aud, role, email, instance_id)
40
+ select pg_temp.person(n), 'authenticated', 'authenticated', 'genesis-tickets-' || n || '@example.test',
41
+ '00000000-0000-0000-0000-000000000000'
42
+ from unnest(array['a1', 'b2', 'c3', 'e5', 'f6', 'd7', 'c9']) n;
43
+
44
+ -- The app's hooks, for this transaction.
45
+ create or replace function private.genesis_support_default_tenant() returns uuid
46
+ language sql stable set search_path = '' as $$ select '00000000-0000-4000-8000-00000000000f'::uuid $$;
47
+ -- Person records are not logins: a login's person id here is its id with ...0005... for ...0004....
48
+ create or replace function private.genesis_viewer_people(viewer uuid)
49
+ returns table (tenant_id uuid, person_id uuid) language sql stable set search_path = '' as $$
50
+ select t.tenant_id, replace(viewer::text, '-0000000004', '-0000000005')::uuid from (values
51
+ ('00000000-0000-4000-8000-0000000004a1'::uuid, '00000000-0000-4000-8000-0000000000a0'::uuid),
52
+ ('00000000-0000-4000-8000-0000000004c3', '00000000-0000-4000-8000-0000000000a0'),
53
+ ('00000000-0000-4000-8000-0000000004c9', '00000000-0000-4000-8000-0000000000a0'),
54
+ ('00000000-0000-4000-8000-0000000004b2', '00000000-0000-4000-8000-0000000000b0'),
55
+ ('00000000-0000-4000-8000-0000000004e5', '00000000-0000-4000-8000-0000000000b0'),
56
+ ('00000000-0000-4000-8000-0000000004d7', '00000000-0000-4000-8000-0000000000a0'),
57
+ ('00000000-0000-4000-8000-0000000004d7', '00000000-0000-4000-8000-0000000000b0')
58
+ ) as t(login, tenant_id) where t.login = viewer $$;
59
+ create or replace function private.genesis_tenant_admin(tenant uuid, person uuid) returns boolean
60
+ language sql stable set search_path = '' as $$
61
+ select person = '00000000-0000-4000-8000-0000000004c9'::uuid and tenant = '00000000-0000-4000-8000-0000000000a0'::uuid $$;
62
+ create or replace function private.genesis_support_staff(person uuid) returns boolean
63
+ language sql stable set search_path = '' as $$ select person = '00000000-0000-4000-8000-0000000004f6'::uuid $$;
64
+ drop function if exists private.genesis_support_report_handler(uuid, uuid);
65
+ revoke execute on function private.genesis_support_default_tenant() from public, anon, authenticated;
66
+ revoke execute on function private.genesis_viewer_people(uuid) from public, anon, authenticated;
67
+ revoke execute on function private.genesis_tenant_admin(uuid, uuid) from public, anon, authenticated;
68
+ revoke execute on function private.genesis_support_staff(uuid) from public, anon, authenticated;
69
+
70
+ insert into public.ticket_agents (tenant_id, person_id, role) values
71
+ (pg_temp.tenant('ta'), pg_temp.record('c3'), 'agent'),
72
+ (pg_temp.tenant('tb'), pg_temp.record('e5'), 'agent');
73
+ -- Both businesses turn the help desk on (it is opt-in). An app without genesis-supabase 0004 gets
74
+ -- a stand-in table for this transaction.
75
+ create table if not exists public.tenant_features (
76
+ tenant_id uuid not null, feature_id text not null, enabled boolean not null, reason text not null,
77
+ primary key (tenant_id, feature_id));
78
+ insert into public.tenant_features (tenant_id, feature_id, enabled, reason) values
79
+ (pg_temp.tenant('ta'), 'support-helpdesk', true, 'test'),
80
+ (pg_temp.tenant('tb'), 'support-helpdesk', true, 'test');
81
+ -- A row keyed by a login instead of a person record grants nothing.
82
+ insert into public.ticket_agents (tenant_id, person_id, role) values (pg_temp.tenant('tb'), pg_temp.person('b2'), 'agent');
83
+
84
+ -- Shape -------------------------------------------------------------------------------------
85
+ select has_table('public', 'tickets', 'tickets exists');
86
+ select has_table('public', 'ticket_attachments', 'ticket_attachments exists');
87
+ select has_table('public', 'ticket_agents', 'ticket_agents exists');
88
+ select has_view('public', 'support_reports', 'support_reports is a view for one minor');
89
+ select ok((select 'security_invoker=true' = any(reloptions) from pg_class where oid = 'public.support_reports'::regclass),
90
+ 'the old name runs as the caller (row security applies)');
91
+ select ok(exists (select 1 from pg_constraint where conname = 'tickets_tenant_required' and convalidated),
92
+ 'every ticket has a tenant (a validated check)');
93
+ select ok((select relrowsecurity from pg_class where oid = 'public.ticket_agents'::regclass), 'RLS is on for agents');
94
+ select ok(not has_table_privilege('authenticated', 'public.ticket_agents', 'insert,update,delete'),
95
+ 'nobody signed in writes agents directly');
96
+
97
+ -- Sending: the client names the tenant; the database checks it ------------------------------
98
+ select pg_temp.sign_in('a1');
99
+ set local role authenticated;
100
+ insert into public.tickets (type, title, message, tenant_id) values ('question', 'A1 asks', 'Where is it?', pg_temp.tenant('ta'));
101
+ reset role;
102
+ insert into ids select 'ta1', id from public.tickets where title = 'A1 asks';
103
+ select is((select tenant_id from public.tickets where id = pg_temp.id('ta1')), pg_temp.tenant('ta'),
104
+ 'a client names their business: the ticket goes there');
105
+
106
+ select pg_temp.sign_in('a1');
107
+ set local role authenticated;
108
+ select throws_ok($$ insert into public.tickets (type, title, message, tenant_id) values ('question', 'Wrong', 'x', '00000000-0000-4000-8000-0000000000b0') $$,
109
+ '42501', 'support: not your tenant', 'nobody writes into a business they are not in');
110
+ insert into public.tickets (type, title, message, tenant_id) values ('bug', 'A1 to the app', 'Broken', pg_temp.tenant('d'));
111
+ reset role;
112
+ insert into ids select 'td1', id from public.tickets where title = 'A1 to the app';
113
+ select is((select tenant_id from public.tickets where id = pg_temp.id('td1')), pg_temp.tenant('d'),
114
+ 'anyone may write to the app''s own support');
115
+
116
+ select pg_temp.sign_in('d7');
117
+ set local role authenticated;
118
+ insert into public.tickets (type, title, message) values ('question', 'G7 asks', 'Which one?');
119
+ insert into public.tickets (type, title, message, tenant_id) values ('question', 'G7 to tb', 'Hello', pg_temp.tenant('tb'));
120
+ reset role;
121
+ insert into ids select 'tg', id from public.tickets where title = 'G7 asks';
122
+ insert into ids select 'tgb', id from public.tickets where title = 'G7 to tb';
123
+ select is((select tenant_id from public.tickets where id = pg_temp.id('tg')), pg_temp.tenant('d'),
124
+ 'naming none (even a client of exactly one business): the app''s own support; nothing is guessed');
125
+ select is((select tenant_id from public.tickets where id = pg_temp.id('tgb')), pg_temp.tenant('tb'),
126
+ '... or the business they name');
127
+
128
+ select pg_temp.sign_in('b2');
129
+ set local role authenticated;
130
+ insert into public.tickets (type, title, message, tenant_id) values ('bug', 'B2 bug', 'Crash', pg_temp.tenant('tb'));
131
+ reset role;
132
+ insert into ids select 'tb2', id from public.tickets where title = 'B2 bug';
133
+
134
+ select pg_temp.sign_in('b2');
135
+ set local role authenticated;
136
+ select ok(not public.support_is_agent(pg_temp.tenant('tb')), 'an agent row keyed by a login, not a person record, grants nothing');
137
+ reset role;
138
+
139
+ -- Reading and handling ------------------------------------------------------------------------
140
+ select pg_temp.sign_in('c3');
141
+ set local role authenticated;
142
+ select ok(exists (select 1 from public.tickets where id = pg_temp.id('ta1')), 'an agent reads their business''s tickets');
143
+ select ok(not exists (select 1 from public.tickets where id in (pg_temp.id('tb2'), pg_temp.id('tgb'))),
144
+ '... and not another business''s');
145
+ select ok(not exists (select 1 from public.tickets where id = pg_temp.id('td1')), '... nor the app''s own');
146
+ select ok(public.support_handles(pg_temp.id('ta1')), 'an agent handles their business''s ticket');
147
+ select ok(not public.support_handles(pg_temp.id('tb2')), '... and not another''s');
148
+ select ok(not public.support_is_staff(), 'an agent of a business is not the app''s staff');
149
+ reset role;
150
+
151
+ select pg_temp.sign_in('f6');
152
+ set local role authenticated;
153
+ select ok(exists (select 1 from public.tickets where id = pg_temp.id('td1')), 'the app''s staff read the app''s own tickets');
154
+ select ok(not exists (select 1 from public.tickets where id in (pg_temp.id('ta1'), pg_temp.id('tb2'))),
155
+ '... and no business''s');
156
+ select ok(public.support_is_staff(), 'staff means: an agent of the app''s own tenant');
157
+ reset role;
158
+
159
+ select pg_temp.sign_in('c9');
160
+ set local role authenticated;
161
+ select ok(public.support_handles(pg_temp.id('ta1')), 'a business''s admin handles its tickets');
162
+ reset role;
163
+
164
+ select pg_temp.sign_in('a1');
165
+ set local role authenticated;
166
+ select ok(exists (select 1 from public.tickets where id = pg_temp.id('ta1')), 'the requester reads their ticket');
167
+ select ok(not public.support_handles(pg_temp.id('ta1')), '... but never handles it');
168
+ select throws_ok($$ select public.support_set_report_status(pg_temp.id('ta1'), 'addressed') $$, '42501', null,
169
+ '... nor sets its status');
170
+ select is((select count(*)::int from public.support_reports), 2, 'the old name shows only the requester''s own tickets');
171
+ select throws_ok($$ update public.tickets set tenant_id = '00000000-0000-4000-8000-0000000000b0' where id = pg_temp.id('ta1') $$,
172
+ '42501', null, 'nobody moves a ticket to another business');
173
+ reset role;
174
+ select throws_ok($$ update public.tickets set tenant_id = '00000000-0000-4000-8000-0000000000b0' where id = pg_temp.id('ta1') $$,
175
+ '42501', 'support: a ticket keeps its tenant', '... not even server code');
176
+
177
+ -- Replies and files stay inside the business --------------------------------------------------
178
+ -- a1 uploads a screenshot to their ta ticket and records it.
179
+ insert into storage.objects (bucket_id, name, owner_id)
180
+ values ('genesis-support', pg_temp.person('a1') || '/' || pg_temp.id('ta1') || '/shot.png', pg_temp.person('a1'));
181
+ select pg_temp.sign_in('a1');
182
+ set local role authenticated;
183
+ insert into public.ticket_attachments (report_id, path, content_type, byte_size, width, height)
184
+ values (pg_temp.id('ta1'), pg_temp.person('a1') || '/' || pg_temp.id('ta1') || '/shot.png', 'image/png', 1024, 390, 844);
185
+ reset role;
186
+ select pg_temp.sign_in('c3');
187
+ set local role authenticated;
188
+ select lives_ok($$ insert into public.support_replies (report_id, body) values (pg_temp.id('ta1'), 'An agent replies') $$,
189
+ 'an agent of the business replies on its ticket');
190
+ select ok(public.support_can_read(pg_temp.person('a1') || '/' || pg_temp.id('ta1') || '/shot.png', now()),
191
+ '... and opens its screenshots');
192
+ reset role;
193
+ select pg_temp.sign_in('e5');
194
+ set local role authenticated;
195
+ select is((select count(*)::int from public.support_replies where report_id = pg_temp.id('ta1')), 0,
196
+ 'an agent of another business reads none of its replies');
197
+ select throws_ok($$ insert into public.support_replies (report_id, body) values (pg_temp.id('ta1'), 'Butting in') $$,
198
+ '42501', null, '... and cannot reply on it');
199
+ select ok(not public.support_can_read(pg_temp.person('a1') || '/' || pg_temp.id('ta1') || '/shot.png', now()),
200
+ '... nor open its screenshots');
201
+ reset role;
202
+ select pg_temp.sign_in('f6');
203
+ set local role authenticated;
204
+ select ok(not public.support_can_read(pg_temp.person('a1') || '/' || pg_temp.id('ta1') || '/shot.png', now()),
205
+ 'the app''s staff open no business''s screenshots');
206
+ reset role;
207
+
208
+ -- The old name still takes inserts, with the same checks.
209
+ select pg_temp.sign_in('b2');
210
+ set local role authenticated;
211
+ insert into public.support_reports (type, title, message, tenant_id) values ('idea', 'Via the old name', 'Still works', pg_temp.tenant('tb'));
212
+ reset role;
213
+ select is((select tenant_id from public.tickets where title = 'Via the old name'), pg_temp.tenant('tb'),
214
+ 'a ticket sent through the old name gets its tenant too');
215
+
216
+ -- Agents ----------------------------------------------------------------------------------------
217
+ select pg_temp.sign_in('a1');
218
+ set local role authenticated;
219
+ select throws_ok($$ select public.support_set_agent('00000000-0000-4000-8000-0000000000a0', '00000000-0000-4000-8000-0000000004a1', 'agent') $$,
220
+ '42501', null, 'a client cannot make themselves an agent');
221
+ reset role;
222
+ select pg_temp.sign_in('c9');
223
+ set local role authenticated;
224
+ select lives_ok($$ select public.support_set_agent('00000000-0000-4000-8000-0000000000a0', '00000000-0000-4000-8000-0000000005a1', 'agent') $$,
225
+ 'a business''s admin adds an agent');
226
+ select throws_ok($$ select public.support_set_agent('00000000-0000-4000-8000-0000000000b0', '00000000-0000-4000-8000-0000000004b2', 'agent') $$,
227
+ '42501', null, '... but not in another business');
228
+ reset role;
229
+ select pg_temp.sign_in('e5');
230
+ set local role authenticated;
231
+ select is((select count(*)::int from public.ticket_agents where tenant_id = pg_temp.tenant('tb')), 2, 'an agent sees their own business''s agents');
232
+ select is((select count(*)::int from public.ticket_agents where tenant_id <> pg_temp.tenant('tb')), 0, '... and no other business''s');
233
+ reset role;
234
+ -- An agent who leaves the business stops being one, whatever the agent row says.
235
+ create or replace function private.genesis_viewer_people(viewer uuid)
236
+ returns table (tenant_id uuid, person_id uuid) language sql stable set search_path = '' as $$
237
+ select null::uuid, null::uuid where false $$;
238
+ select pg_temp.sign_in('c3');
239
+ set local role authenticated;
240
+ select ok(not public.support_handles(pg_temp.id('ta1')), 'an agent removed from the business no longer handles its tickets');
241
+ reset role;
242
+
243
+ -- The app's handler hook stays inside the business -------------------------------------------
244
+ create or replace function private.genesis_viewer_people(viewer uuid)
245
+ returns table (tenant_id uuid, person_id uuid) language sql stable set search_path = '' as $$
246
+ select '00000000-0000-4000-8000-0000000000a0'::uuid, viewer where viewer = '00000000-0000-4000-8000-0000000004b2'::uuid $$;
247
+ create function private.genesis_support_report_handler(report uuid, person uuid) returns boolean
248
+ language sql stable set search_path = '' as $$ select person = '00000000-0000-4000-8000-0000000004b2'::uuid $$;
249
+ revoke execute on function private.genesis_support_report_handler(uuid, uuid) from public, anon, authenticated;
250
+ select pg_temp.sign_in('b2');
251
+ set local role authenticated;
252
+ select ok(not public.support_handles(pg_temp.id('ta1')), 'the app''s hook cannot hand a business''s ticket to anyone, even a member of it');
253
+ select ok(public.support_handles(pg_temp.id('td1')), '... but may for the app''s own tickets');
254
+ reset role;
255
+
256
+ -- Ideas ------------------------------------------------------------------------------------------
257
+ select pg_temp.sign_out();
258
+ insert into public.support_ideas (author_id, title, body, tenant_id) values
259
+ (pg_temp.person('a1'), 'An idea for ta', 'Please', pg_temp.tenant('ta')),
260
+ (pg_temp.person('a1'), 'An idea for the app', 'Please', pg_temp.tenant('d'));
261
+ select pg_temp.sign_in('e5');
262
+ set local role authenticated;
263
+ select ok(not exists (select 1 from public.support_ideas where title = 'An idea for ta'), 'another business''s idea board is closed');
264
+ select ok(exists (select 1 from public.support_ideas where title = 'An idea for the app'), 'the app''s own board is open to everyone signed in');
265
+ reset role;
266
+
267
+ -- Turning the help desk off ----------------------------------------------------------------------
268
+ create or replace function private.genesis_viewer_people(viewer uuid)
269
+ returns table (tenant_id uuid, person_id uuid) language sql stable set search_path = '' as $$
270
+ select t.tenant_id, t.login from (values
271
+ ('00000000-0000-4000-8000-0000000004b2'::uuid, '00000000-0000-4000-8000-0000000000b0'::uuid),
272
+ ('00000000-0000-4000-8000-0000000004a1', '00000000-0000-4000-8000-0000000000a0')
273
+ ) as t(login, tenant_id) where t.login = viewer $$;
274
+ select pg_temp.sign_out();
275
+ update public.tenant_features set enabled = false where tenant_id = pg_temp.tenant('tb') and feature_id = 'support-helpdesk';
276
+ delete from public.tenant_features where tenant_id = pg_temp.tenant('ta') and feature_id = 'support-helpdesk';
277
+ select pg_temp.sign_in('b2');
278
+ set local role authenticated;
279
+ insert into public.tickets (type, title, message) values ('question', 'Off', 'x');
280
+ select throws_ok($$ insert into public.tickets (type, title, message, tenant_id) values ('question', 'Off 2', 'x', '00000000-0000-4000-8000-0000000000b0') $$,
281
+ '42501', 'support: the help desk is off here', 'a business that turned the help desk off takes no tickets');
282
+ insert into public.support_ideas (title, body) values ('An idea from b2', 'Please');
283
+ reset role;
284
+ select pg_temp.sign_in('a1');
285
+ set local role authenticated;
286
+ select throws_ok($$ insert into public.tickets (type, title, message, tenant_id) values ('question', 'Never on', 'x', '00000000-0000-4000-8000-0000000000a0') $$,
287
+ '42501', 'support: the help desk is off here', 'a business that never turned the help desk on takes no tickets (opt-in)');
288
+ select lives_ok($$ insert into public.tickets (type, title, message, tenant_id) values ('question', 'Always on', 'x', '00000000-0000-4000-8000-00000000000f') $$,
289
+ 'the app''s own support always takes tickets, with no switch row');
290
+ reset role;
291
+
292
+ select is((select tenant_id from public.tickets where title = 'Off'), pg_temp.tenant('d'),
293
+ '... its clients'' tickets go to the app''s own support instead');
294
+ select is((select tenant_id from public.support_ideas where title = 'An idea from b2'), pg_temp.tenant('d'),
295
+ 'an idea goes to the app''s own board unless a business is named');
296
+ select is(public.support_default_tenant(), pg_temp.tenant('d'), 'clients can ask for the app''s own tenant');
297
+
298
+ -- An agent who files a ticket doesn't handle it -------------------------------------------------
299
+ create or replace function private.genesis_viewer_people(viewer uuid)
300
+ returns table (tenant_id uuid, person_id uuid) language sql stable set search_path = '' as $$
301
+ select '00000000-0000-4000-8000-0000000000b0'::uuid, replace(viewer::text, '-0000000004', '-0000000005')::uuid
302
+ where viewer in ('00000000-0000-4000-8000-0000000004e5'::uuid, '00000000-0000-4000-8000-0000000004b2'::uuid) $$;
303
+ update public.tenant_features set enabled = true where tenant_id = pg_temp.tenant('tb') and feature_id = 'support-helpdesk';
304
+ select pg_temp.sign_in('e5');
305
+ set local role authenticated;
306
+ insert into public.tickets (type, title, message, tenant_id) values ('bug', 'E5 files one', 'Broken', pg_temp.tenant('tb'));
307
+ select ok(not public.support_handles((select id from public.tickets where title = 'E5 files one')),
308
+ 'an agent who files a ticket in their own business does not handle it');
309
+ select throws_ok($$ select public.support_set_report_status((select id from public.tickets where title = 'E5 files one'), 'addressed') $$,
310
+ '42501', null, '... nor sets its status');
311
+ reset role;
312
+
313
+ -- A solo user with only a personal tenant -------------------------------------------------------
314
+ create or replace function private.genesis_viewer_people(viewer uuid)
315
+ returns table (tenant_id uuid, person_id uuid) language sql stable set search_path = '' as $$
316
+ select '00000000-0000-4000-8000-0000000000c0'::uuid, viewer where viewer = '00000000-0000-4000-8000-0000000004d7'::uuid $$;
317
+ select pg_temp.sign_in('d7');
318
+ set local role authenticated;
319
+ insert into public.tickets (type, title, message) values ('question', 'Solo asks', 'Hello');
320
+ select throws_ok($$ insert into public.tickets (type, title, message, tenant_id) values ('question', 'Solo to self', 'x', '00000000-0000-4000-8000-0000000000c0') $$,
321
+ '42501', 'support: the help desk is off here', 'a personal tenant has no help desk, so it takes no tickets');
322
+ reset role;
323
+ select is((select tenant_id from public.tickets where title = 'Solo asks'), pg_temp.tenant('d'),
324
+ 'a solo user''s ticket goes to the app''s own support, never their personal tenant');
325
+
326
+ select * from finish();
327
+ rollback;
@@ -0,0 +1,19 @@
1
+ -- @feat:chat
2
+ -- pgTAP test for 20261025000070_starter_tenancy.sql: every login is in the starter's one tenant.
3
+ begin;
4
+ create extension if not exists pgtap with schema extensions;
5
+ select plan(4);
6
+
7
+ select is((select count(*)::integer from private.genesis_viewer_people('00000000-0000-4000-8000-0000000000aa')), 1,
8
+ 'a login is in one tenant');
9
+ select is((select tenant_id from private.genesis_viewer_people('00000000-0000-4000-8000-0000000000aa')),
10
+ (select tenant_id from private.genesis_viewer_people('00000000-0000-4000-8000-0000000000bb')),
11
+ 'everyone''s is the same one');
12
+ select is((select person_id from private.genesis_viewer_people('00000000-0000-4000-8000-0000000000aa')),
13
+ '00000000-0000-4000-8000-0000000000aa'::uuid, 'and is their own person record');
14
+ select ok(not has_function_privilege('authenticated', 'private.genesis_viewer_people(uuid)', 'execute')
15
+ and not has_function_privilege('anon', 'private.genesis_viewer_people(uuid)', 'execute'),
16
+ 'nobody calls the hook directly');
17
+
18
+ select * from finish();
19
+ rollback;
package/dist/vite.js CHANGED
@@ -1455,6 +1455,7 @@ var FUNCTION_NAME = /^[a-z0-9][a-z0-9_-]*$/;
1455
1455
  var ROUTE = /^\/(?:[A-Za-z0-9_\-[\]().]+\/?)*$/;
1456
1456
  var INSTALL_SPEC = /^(@[a-z0-9][a-z0-9._-]*\/)?[a-z0-9][a-z0-9._-]*(@[A-Za-z0-9.^~<>=|* -]+)?$/;
1457
1457
  var SECRET = /^[A-Z][A-Z0-9_]*$/;
1458
+ var SQL_FUNCTION = /^[a-z_][a-z0-9_]*\.[a-z_][a-z0-9_]*$/;
1458
1459
  var ManifestError = class extends Error {
1459
1460
  constructor(problems) {
1460
1461
  super(`Invalid block manifest:
@@ -1549,6 +1550,39 @@ function parseManifest(home, id, raw, version, options = {}) {
1549
1550
  });
1550
1551
  }
1551
1552
  }
1553
+ if (m["needs"] !== void 0) {
1554
+ if (!Array.isArray(m["needs"])) problems.push(`${where2}.needs: must be a list of { function, how }`);
1555
+ else {
1556
+ out.needs = m["needs"].flatMap((n, j) => {
1557
+ const at2 = `${where2}.needs[${j}]`;
1558
+ if (!isObject2(n) || typeof n["function"] !== "string" || typeof n["how"] !== "string" || !n["how"].trim()) {
1559
+ problems.push(`${at2}: must be { function: "schema.name", how: "what to add" }`);
1560
+ return [];
1561
+ }
1562
+ if (!SQL_FUNCTION.test(n["function"])) {
1563
+ problems.push(`${at2}.function: "${n["function"]}" must be a schema-qualified function name, like private.genesis_viewer_people`);
1564
+ return [];
1565
+ }
1566
+ return [{ function: n["function"], how: n["how"].trim() }];
1567
+ });
1568
+ }
1569
+ }
1570
+ if (m["runsBefore"] !== void 0) {
1571
+ if (!Array.isArray(m["runsBefore"])) problems.push(`${where2}.runsBefore: must be a list of { package, file }`);
1572
+ else {
1573
+ out.runsBefore = m["runsBefore"].map((r, j) => {
1574
+ const at2 = `${where2}.runsBefore[${j}]`;
1575
+ if (!isObject2(r)) {
1576
+ problems.push(`${at2}: must be an object`);
1577
+ return { package: home, file: "" };
1578
+ }
1579
+ const before = { package: pkgOf(r["package"], at2), file: pathOf(r["file"], `${at2}.file`) };
1580
+ if (before.file && !MIGRATION_FILE.test(before.file.split("/").pop())) problems.push(`${at2}.file: "${before.file}" must be named NNNN_name.sql`);
1581
+ if (before.package === out.package && before.file === file) problems.push(`${at2}: a migration can't run before itself`);
1582
+ return before;
1583
+ });
1584
+ }
1585
+ }
1552
1586
  if (m["test"] !== void 0) {
1553
1587
  out.test = pathOf(m["test"], `${where2}.test`);
1554
1588
  if (out.test && !/^[a-z0-9_]+\.test\.sql$/.test(out.test.split("/").pop().replace(/^\d{4}_/, ""))) problems.push(`${where2}.test: "${out.test}" must be named [NNNN_]name.test.sql`);