@fabrika/app 0.9.0-dev.10 → 0.9.0-dev.11

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fabrika/app",
3
- "version": "0.9.0-dev.10",
3
+ "version": "0.9.0-dev.11",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -109,14 +109,14 @@
109
109
  "test": "bun test --path-ignore-patterns='src/react/**' && bun test --isolate src/react"
110
110
  },
111
111
  "dependencies": {
112
- "@fabrika/auth": "0.9.0-dev.10",
113
- "@fabrika/auth-core": "0.9.0-dev.10",
114
- "@fabrika/db": "0.9.0-dev.10",
115
- "@fabrika/email": "0.9.0-dev.10",
116
- "@fabrika/jwks": "0.9.0-dev.10",
117
- "@fabrika/platform": "0.9.0-dev.10",
118
- "@fabrika/platform-node": "0.9.0-dev.10",
119
- "@fabrika/proxy-core": "0.9.0-dev.10",
112
+ "@fabrika/auth": "0.9.0-dev.11",
113
+ "@fabrika/auth-core": "0.9.0-dev.11",
114
+ "@fabrika/db": "0.9.0-dev.11",
115
+ "@fabrika/email": "0.9.0-dev.11",
116
+ "@fabrika/jwks": "0.9.0-dev.11",
117
+ "@fabrika/platform": "0.9.0-dev.11",
118
+ "@fabrika/platform-node": "0.9.0-dev.11",
119
+ "@fabrika/proxy-core": "0.9.0-dev.11",
120
120
  "altcha-lib": "2.6.0",
121
121
  "jose": "^5.9.0"
122
122
  },
package/skill/SKILL.md CHANGED
@@ -1,6 +1,6 @@
1
1
  ---
2
2
  name: fabrika-app
3
- version: 0.9.0-dev.10
3
+ version: 0.9.0-dev.11
4
4
  description: >
5
5
  How to write and change a Fabrika application: src/vocabulary.ts, db/migrations/, src/db/tables.ts, the
6
6
  slices' endpoints.ts with their handlers, fabrika.contract.ts, jobs, files, webhooks, share links, connectors
@@ -19,20 +19,20 @@ the SPA is added with it.
19
19
  "test": "bun test --isolate"
20
20
  },
21
21
  "dependencies": {
22
- "@fabrika/app": "0.9.0-dev.10",
23
- "@fabrika/db": "0.9.0-dev.10",
24
- "@fabrika/provider-zerops": "0.9.0-dev.10",
25
- "@fabrika/proxy-core": "0.9.0-dev.10",
26
- "@fabrika/ui": "0.9.0-dev.10",
22
+ "@fabrika/app": "0.9.0-dev.11",
23
+ "@fabrika/db": "0.9.0-dev.11",
24
+ "@fabrika/provider-zerops": "0.9.0-dev.11",
25
+ "@fabrika/proxy-core": "0.9.0-dev.11",
26
+ "@fabrika/ui": "0.9.0-dev.11",
27
27
  "kysely": "0.29.6",
28
28
  "react": "^19.0.0",
29
29
  "react-dom": "^19.0.0",
30
30
  "zod": "^3.25.76"
31
31
  },
32
32
  "devDependencies": {
33
- "@fabrika/check": "0.9.0-dev.10",
34
- "@fabrika/cli": "0.9.0-dev.10",
35
- "@fabrika/platform": "0.9.0-dev.10",
33
+ "@fabrika/check": "0.9.0-dev.11",
34
+ "@fabrika/cli": "0.9.0-dev.11",
35
+ "@fabrika/platform": "0.9.0-dev.11",
36
36
  "@happy-dom/global-registrator": "^20.11.2",
37
37
  "@types/bun": "^1.4.3",
38
38
  "@types/react": "^19.0.0",
@@ -43,6 +43,11 @@ export const members = { 'api.tasks.assign': ['assignee'] }
43
43
  `{ 'api.integrations.connectGithub': { orgId: 'acme', account: 'installation:1' } }`. The checker names each missing one,
44
44
  and one of an endpoint that does not exist (`endpoint-fixture`). The access form's `from` field is
45
45
  replaced by the seeded scope.
46
+ - A `from` field that admits a fixed set of values (`z.enum(['eu', 'us', 'apac'])`, a literal or a union of them) gets
47
+ two of them, one per scope: an enum's first two in sorted order, a union's first two as written. A row the table that owns the dimension already holds
48
+ under that key (a migration wrote the regions) is used as it is; a row the case inserts is removed once it is over.
49
+ Such a stored row takes no `seeds` or `pins`, and a field of one value cannot be asked from two scopes: both fail the
50
+ case.
46
51
  - `members['<endpoint id>']` lists the input fields that name a picked person the handler checks with
47
52
  `ctx.members.get` (`assign`'s `assignee`). The generated cases send a real member of the object's org there; without
48
53
  it they send a sample, the handler answers 400, and the case fails.
@@ -55,12 +60,13 @@ export const members = { 'api.tasks.assign': ['assignee'] }
55
60
  endpoint's dimension in both scopes: `close` answers 409 for an open task, so the seed makes it `doing`. A seed
56
61
  sets neither a column that places the row nor a text column that holds a marker; a text column a CHECK lists the
57
62
  values of, and a foreign key, it may set.
58
- - `rows['<endpoint id>']` names the input fields that address a row lying in the endpoint's object, with that row's
59
- table: `export const rows = { 'api.tasks.editComment': { commentId: 'task_comments' } }`. The generated cases seed
60
- one row of the table in each scope's object and send its key there. The table lies directly in the object: a child
61
- table of the table that owns it, or a table it contains; the checker refuses a row further down
62
- (`endpoint-fixture`). Without it they send a sample, the handler
63
- finds no row (404), and the case fails.
63
+ - `rows['<endpoint id>']` names the input fields that address a row in the endpoint's scope, with that row's table:
64
+ `export const rows = { 'api.tasks.editComment': { commentId: 'task_comments' } }`. The generated cases seed one row
65
+ of the table in each scope and send its key there. Under `on` and `in` the table lies directly in the object or
66
+ target (a child table of the table that owns it, or a table it contains), and a table owns the dimension; under
67
+ `over` the dimension lists the table. The checker refuses anything else (`endpoint-fixture`). An endpoint with
68
+ `capabilities` names the capability whose scope the row lies in: `'api.tasks.copyToTask#from': { id:
69
+ 'task_attachments' }`. Without it they send a sample, the handler finds no row (404), and the case fails.
64
70
  - `appRows['<endpoint id>']` gives the rows of application tables (`scope: { app }`) that the endpoint's handler reads
65
71
  for the caller's own scope, by table, with the columns they need: `export const appRows = { 'portal.guide': { guides:
66
72
  { active: true } } }`. Each of its leak and own-object cases seeds one row of each before it asks and removes them
@@ -262,11 +262,14 @@ export const GENERATED_LINK = 'fabrika:link'
262
262
 
263
263
  /**
264
264
  * The prefix of the value a generated endpoint access case sends in an input field that names a row lying in its object
265
- * (`rows` of `endpoint-access.fixtures.ts`), followed by the row's table: `fabrika:row:note_comments`. The endpoint
266
- * access kit seeds one such row in each scope's object and sends that scope's key.
265
+ * or its list (`rows` of `endpoint-access.fixtures.ts`), followed by the row's table: `fabrika:row:note_comments`. The
266
+ * endpoint access kit seeds one such row in each scope and sends that scope's key.
267
267
  */
268
268
  export const GENERATED_ROW_PREFIX = 'fabrika:row:'
269
269
 
270
+ /** What follows a row's table when the row lies in the scope of one capability of an endpoint of several: `@<name>`. */
271
+ export const GENERATED_ROW_CAPABILITY = '@'
272
+
270
273
  /**
271
274
  * The prefix of the value a generated endpoint access case sends in a file field, followed by the field's file kind:
272
275
  * `fabrika:file:attachments`. The endpoint access kit sends a small file of the kind's first type it can write in its place.
@@ -19,7 +19,7 @@ import {
19
19
  } from '@fabrika/auth-core'
20
20
  import type { Tables } from '@fabrika/db'
21
21
  import { tablePolicies } from '@fabrika/db/tables'
22
- import { type IsolationWorlds, type SampleValue, SEEDED_OBJECT_VERSION, type TestDatabase, type WorldRow } from '@fabrika/db/testing'
22
+ import { type IsolationWorlds, type SampleValue, type ScopeValues, SEEDED_OBJECT_VERSION, type TestDatabase } from '@fabrika/db/testing'
23
23
  import { MemoryFileObjects } from '@fabrika/platform/testing'
24
24
  import { readFile } from 'node:fs/promises'
25
25
  import { basename } from 'node:path'
@@ -34,6 +34,7 @@ import {
34
34
  GENERATED_INVITATION,
35
35
  GENERATED_LINK,
36
36
  GENERATED_MEMBER,
37
+ GENERATED_ROW_CAPABILITY,
37
38
  GENERATED_ROW_PREFIX,
38
39
  } from '../contract/defaults'
39
40
  import type { AnyContract } from '../contract/define'
@@ -150,18 +151,17 @@ type Refusals = 'declared' | 'none'
150
151
 
151
152
  /**
152
153
  * The pins of a case's endpoint (column → the own scope's value of a text column), and the removals of the case: rows
153
- * seeded with pins are removed once the case is over, so the next case can pin the same values under a unique index.
154
+ * seeded with pins or known scope values are removed once the case is over, so the next case can seed the same values
155
+ * under a unique index.
154
156
  */
155
157
  interface Pinning {
156
158
  readonly pins: Readonly<Record<string, string>>
157
159
  readonly removals: (() => Promise<void>)[]
158
160
  }
159
161
 
160
- const NO_PINNING: Pinning = { pins: {}, removals: [] }
161
-
162
- /** Seeded worlds, their removal taken by the case when they hold pinned values. */
163
- const pinned = <W extends { readonly remove: () => Promise<void> }>(worlds: W, pinning: Pinning): W => {
164
- if (Object.keys(pinning.pins).length > 0) pinning.removals.push(worlds.remove)
162
+ /** Seeded worlds, their removal taken by the case when they hold pinned values or known scope values. */
163
+ const pinned = <W extends { readonly remove: () => Promise<void> }>(worlds: W, pinning: Pinning, scopes: ScopeValues): W => {
164
+ if (Object.keys(pinning.pins).length > 0 || Object.keys(scopes).length > 0) pinning.removals.push(worlds.remove)
165
165
  return worlds
166
166
  }
167
167
 
@@ -203,11 +203,19 @@ interface Scopes {
203
203
  readonly other: Side & { readonly values: readonly string[] }
204
204
  }
205
205
 
206
- /** The input fields that name a row the kit seeds in the case's object (`rows`), with the row's table. */
207
- function rowFields(input: Readonly<Record<string, unknown>>): readonly { readonly field: string; readonly table: string }[] {
208
- return Object.entries(input).flatMap(([field, value]) =>
209
- typeof value === 'string' && value.startsWith(GENERATED_ROW_PREFIX) ? [{ field, table: value.slice(GENERATED_ROW_PREFIX.length) }] : []
210
- )
206
+ /**
207
+ * The input fields that name a row the kit seeds in the case's scopes (`rows`), with the row's table and, on an endpoint
208
+ * of several capabilities, the capability whose scope it lies in.
209
+ */
210
+ function rowFields(
211
+ input: Readonly<Record<string, unknown>>,
212
+ ): readonly { readonly field: string; readonly table: string; readonly capability: string | null }[] {
213
+ return Object.entries(input).flatMap(([field, value]) => {
214
+ if (typeof value !== 'string' || !value.startsWith(GENERATED_ROW_PREFIX)) return []
215
+ const named = value.slice(GENERATED_ROW_PREFIX.length)
216
+ const at = named.indexOf(GENERATED_ROW_CAPABILITY)
217
+ return [at === -1 ? { field, table: named, capability: null } : { field, table: named.slice(0, at), capability: named.slice(at + 1) }]
218
+ })
211
219
  }
212
220
 
213
221
  /** A case's input, and the values it named that only its own scope may see in an answer: a generated member's id and label. */
@@ -216,12 +224,16 @@ interface Addressed {
216
224
  readonly marks: readonly string[]
217
225
  }
218
226
 
219
- /** One target of an `in` report, with its chain from the target outward. */
227
+ /** One target of an `in` report, with its chain from the target outward, and the key of each row `rows` names in it. */
220
228
  interface InTarget {
221
229
  readonly target: string
222
230
  readonly chain: Chain
231
+ readonly rows: Readonly<Record<string, string>>
223
232
  }
224
233
 
234
+ /** An input field that names a row the kit seeds (`rows`), with the row's table. */
235
+ type RowField = ReturnType<typeof rowFields>[number]
236
+
225
237
  export type Grant =
226
238
  | { readonly action: string; readonly scope: { readonly type: string; readonly value: string } }
227
239
  | { readonly role: string; readonly scope: { readonly type: string; readonly value: string } }
@@ -252,6 +264,40 @@ function errorType(text: string): string | undefined {
252
264
  }
253
265
  }
254
266
 
267
+ /** The values a field admits when they are a fixed set: an enum, a string literal or a union of them; else `undefined`. */
268
+ function fixedValues(field: SchemaShape | undefined): readonly string[] | undefined {
269
+ switch (field?.type) {
270
+ case 'enum':
271
+ return field.values
272
+ case 'literal':
273
+ return typeof field.value === 'string' ? [field.value] : undefined
274
+ case 'union': {
275
+ const options = field.options.map(fixedValues)
276
+ return options.every((values) => values !== undefined) ? [...new Set(options.flat())] : undefined
277
+ }
278
+ default:
279
+ return undefined
280
+ }
281
+ }
282
+
283
+ /**
284
+ * The values of `dimension` a case's two scopes take when the input field it is read from admits a fixed set: its first
285
+ * two, which a stored row of the table that owns it may already hold. None otherwise, so each scope gets a fresh value.
286
+ */
287
+ function knownScopes(dimension: string, field: SchemaShape | undefined): ScopeValues {
288
+ const values = fixedValues(field)
289
+ if (values === undefined) return {}
290
+ const [own, other] = values
291
+ if (own === undefined || other === undefined) {
292
+ throw new Error(
293
+ `the input field a ${dimension} is read from admits only ${
294
+ values.map((value) => `'${value}'`).join(', ')
295
+ }, so no case can ask from two scopes of it; admit two values or more`,
296
+ )
297
+ }
298
+ return { [dimension]: { own, other } }
299
+ }
300
+
255
301
  /** A scope value no table holds, in the format of the input field it is sent in. */
256
302
  function freshValue(dimension: string, world: 'own' | 'other', field: SchemaShape | undefined): string {
257
303
  const format = field?.type === 'string' ? field.format : null
@@ -265,10 +311,23 @@ function fieldOf(input: SchemaShape | null, from: string | null): SchemaShape |
265
311
  return input?.type === 'object' && from !== null ? input.fields[from] : undefined
266
312
  }
267
313
 
268
- /** Every value of the other world a leak shows: its key, parent, every dimension value it lies in, and its markers. */
269
- function worldValues(worlds: IsolationWorlds, world: 'own' | 'other'): string[] {
314
+ /**
315
+ * Every value of the other world a leak shows: its key, parent, every dimension value it lies in, and its markers. A
316
+ * known scope value, and a text of a row the table already held, other rows may share, and a short word is part of
317
+ * unrelated text: each counts only as a whole JSON string (`"eu"`). A stored text the own row holds too is no leak.
318
+ */
319
+ function worldValues(worlds: IsolationWorlds, world: 'own' | 'other', scopes: ScopeValues): string[] {
270
320
  const row = worlds[world]
271
- return [...new Set([row.key, row.scope, ...(row.parent === null ? [] : [row.parent]), ...row.chain.map((link) => link.value), ...row.markers])]
321
+ const shared = new Set(world === 'other' ? worlds.own.stored : worlds.other.stored)
322
+ const values = [row.key, row.scope, ...(row.parent === null ? [] : [row.parent]), ...row.chain.map((link) => link.value), ...row.markers]
323
+ const stored = row.stored.filter((value) => !shared.has(value))
324
+ return [...new Set([...values.map((value) => shownValue(value, scopes)), ...stored.map((value) => JSON.stringify(value))])]
325
+ }
326
+
327
+ /** A value of a scope as a leak shows it: a known scope value as a whole JSON string, any other as it is. */
328
+ function shownValue(value: string, scopes: ScopeValues): string {
329
+ const known = Object.values(scopes).some((values) => values.own === value || values.other === value)
330
+ return known ? JSON.stringify(value) : value
272
331
  }
273
332
 
274
333
  /** The cases' scanner: a file it reads in full is clean, so a seeded upload of a `scan: true` collection is served. */
@@ -584,39 +643,42 @@ export function endpointAccessKit<DB, P>(
584
643
  }
585
644
 
586
645
  /**
587
- * Two scopes of `dimension`: rows of the table that owns it when one does, with `seed`, otherwise two fresh scope
588
- * values. Each row `input` names in the object (`rows`) is seeded in each scope's row.
646
+ * Two scopes of `dimension`: rows of the table that owns it when one does, with `seed`, otherwise two scope values;
647
+ * each fresh, or the known values of a field that admits a fixed set (`knownScopes`). Each row `named` (`rows`) is
648
+ * seeded in each scope's object.
589
649
  */
590
650
  const scopesOf = async (
591
651
  action: string,
592
652
  dimension: string,
593
653
  field: SchemaShape | undefined,
594
654
  seed: Seed,
595
- input: Readonly<Record<string, unknown>>,
596
- pinning: Pinning = NO_PINNING,
655
+ named: readonly RowField[],
656
+ pinning: Pinning,
597
657
  ): Promise<Scopes> => {
598
- const named = rowFields(input)
599
658
  const owner = ownerOf(dimension)
600
659
  if (named.length > 0 && owner === undefined) {
601
660
  throw new Error(`no table owns ${dimension}, so the kit seeds no ${named.map((row) => row.table).join(', ')} row in one`)
602
661
  }
662
+ const scopes = knownScopes(dimension, field)
603
663
  if (owner === undefined) {
604
- const own = freshValue(dimension, 'own', field)
605
- const other = freshValue(dimension, 'other', field)
664
+ const own = scopes[dimension]?.own ?? freshValue(dimension, 'own', field)
665
+ const other = scopes[dimension]?.other ?? freshValue(dimension, 'other', field)
606
666
  return {
607
667
  own: { target: own, grants: [{ action, scope: { type: dimension, value: own } }], chain: [{ type: dimension, value: own }], rows: {} },
608
668
  other: {
609
669
  target: other,
610
670
  grants: [{ action, scope: { type: dimension, value: other } }],
611
- values: [other],
671
+ values: [shownValue(other, scopes)],
612
672
  chain: [{ type: dimension, value: other }],
613
673
  rows: {},
614
674
  },
615
675
  }
616
676
  }
617
- const worlds = pinned(await postgres.seedWorlds(tables, owner, seed, pinning.pins), pinning)
618
- const own = await rowsIn(worlds.own, named)
619
- const other = await rowsIn(worlds.other, named)
677
+ const worlds = pinned(await postgres.seedWorlds(tables, owner, seed, pinning.pins, scopes), pinning, scopes)
678
+ // A row under a stored scope value outlives the worlds' removal, so the case removes it itself.
679
+ const removals = Object.keys(scopes).length > 0 ? pinning.removals : []
680
+ const own = await rowsIn(worlds.own.chain, named, removals)
681
+ const other = await rowsIn(worlds.other.chain, named, removals)
620
682
  return {
621
683
  own: {
622
684
  target: worlds.own.key,
@@ -627,27 +689,34 @@ export function endpointAccessKit<DB, P>(
627
689
  other: {
628
690
  target: worlds.other.key,
629
691
  grants: [{ action, scope: { type: worlds.dimension, value: worlds.other.scope } }],
630
- values: [...worldValues(worlds, 'other'), ...other.values],
692
+ values: [...worldValues(worlds, 'other', scopes), ...other.values],
631
693
  chain: worlds.other.chain,
632
694
  rows: other.rows,
633
695
  },
634
696
  }
635
697
  }
636
698
 
637
- /** One row of each named table seeded in `object`: its key by input field, and every key and marker it holds. */
638
- const rowsIn = async (object: WorldRow, named: ReturnType<typeof rowFields>) => {
699
+ /**
700
+ * One row of each named table seeded in the object `chain` starts at: its key by input field, and every key and marker
701
+ * it holds. Each row's removal goes to `removals`.
702
+ */
703
+ const rowsIn = async (chain: Chain, named: readonly RowField[], removals: (() => Promise<void>)[]) => {
639
704
  const rows: Record<string, string> = {}
640
705
  const values: string[] = []
641
706
  for (const { field, table } of named) {
642
- const seeded = await postgres.seedUnder(tables, table, object)
707
+ const seeded = await postgres.seedUnder(tables, table, { chain })
708
+ removals.push(seeded.remove)
643
709
  rows[field] = seeded.key
644
710
  values.push(seeded.key, ...seeded.markers)
645
711
  }
646
712
  return { rows, values }
647
713
  }
648
714
 
649
- /** For a list of `dimension`: one row in each scope of every table listed by it; each caller holds every scope of its side. */
650
- const listScopesOf = async (action: string, dimension: string): Promise<Scopes> => {
715
+ /**
716
+ * For a list of `dimension`: one row in each scope of every table listed by it, and of each table `named` (`rows`),
717
+ * whose key goes in its field; each caller holds every scope of its side.
718
+ */
719
+ const listScopesOf = async (action: string, dimension: string, named: readonly RowField[], pinning: Pinning): Promise<Scopes> => {
651
720
  const own: Grant[] = []
652
721
  const other: Grant[] = []
653
722
  const values: string[] = []
@@ -656,42 +725,86 @@ export function endpointAccessKit<DB, P>(
656
725
  const worlds = await postgres.seedWorlds(tables, name)
657
726
  own.push({ action, scope: { type: dimension, value: worlds.own.scope } })
658
727
  other.push({ action, scope: { type: dimension, value: worlds.other.scope } })
659
- values.push(...worldValues(worlds, 'other'))
728
+ values.push(...worldValues(worlds, 'other', {}))
660
729
  }
661
730
  }
662
731
  if (own.length === 0) {
663
- return scopesOf(action, dimension, undefined, {}, {})
732
+ return scopesOf(action, dimension, undefined, {}, named, pinning)
733
+ }
734
+ const listed = await listedRows(action, dimension, named)
735
+ return {
736
+ own: { target: '', grants: [...own, ...listed.own.grants], chain: [], rows: listed.own.rows },
737
+ other: { target: '', grants: [...other, ...listed.other.grants], values: [...values, ...listed.values], chain: [], rows: listed.other.rows },
738
+ }
739
+ }
740
+
741
+ /**
742
+ * One row of each table `named` (`rows`) in each of two scopes of `dimension`, which lists it: the grants of `action`
743
+ * that reach it, its key by input field, and every value of the other scope's rows.
744
+ */
745
+ const listedRows = async (action: string, dimension: string, named: readonly RowField[]) => {
746
+ const ownGrants: Grant[] = []
747
+ const otherGrants: Grant[] = []
748
+ const ownRows: Record<string, string> = {}
749
+ const otherRows: Record<string, string> = {}
750
+ const values: string[] = []
751
+ for (const { field, table } of named) {
752
+ if (listDimensionOf(table) !== dimension) {
753
+ throw new Error(`a list of ${dimension} reaches no ${table} row, so the kit seeds none for ${field}`)
754
+ }
755
+ const worlds = await postgres.seedWorlds(tables, table)
756
+ ownGrants.push({ action, scope: { type: dimension, value: worlds.own.scope } })
757
+ otherGrants.push({ action, scope: { type: dimension, value: worlds.other.scope } })
758
+ ownRows[field] = worlds.own.key
759
+ otherRows[field] = worlds.other.key
760
+ values.push(...worldValues(worlds, 'other', {}))
664
761
  }
665
- return { own: { target: '', grants: own, chain: [], rows: {} }, other: { target: '', grants: other, values, chain: [], rows: {} } }
762
+ return { own: { grants: ownGrants, rows: ownRows }, other: { grants: otherGrants, rows: otherRows }, values }
666
763
  }
667
764
 
668
765
  /**
669
766
  * For a report `in` `dimension`: one row in each scope of every table that lies in it, the table that owns it with
670
767
  * `seed`. Each side's targets are its rows' values of `dimension`, and each caller holds all of them.
671
768
  */
672
- const inScopesOf = async (action: string, dimension: string, field: SchemaShape | undefined, seed: Seed, pinning: Pinning = NO_PINNING) => {
769
+ const inScopesOf = async (
770
+ action: string,
771
+ dimension: string,
772
+ field: SchemaShape | undefined,
773
+ seed: Seed,
774
+ named: readonly RowField[],
775
+ pinning: Pinning,
776
+ ) => {
673
777
  const own: InTarget[] = []
674
778
  const other: InTarget[] = []
675
779
  const values: string[] = []
676
- for (const name of tableNames) {
780
+ const scopes = knownScopes(dimension, field)
781
+ const unpinned = { pins: {}, removals: pinning.removals }
782
+ // The owner first: a row of a known value another table's seed inserted would pass for a stored one, which takes no seed.
783
+ const owner = ownerOf(dimension)
784
+ for (const name of [...(owner === undefined ? [] : [owner]), ...tableNames.filter((candidate) => candidate !== owner)]) {
677
785
  const mode = policies[name]?.scope.mode
678
786
  if (mode !== 'dimension' && mode !== 'child') continue
679
- const owned = name === ownerOf(dimension)
787
+ const owned = name === owner
680
788
  const worlds = owned
681
- ? pinned(await postgres.seedWorlds(tables, name, seed, pinning.pins), pinning)
682
- : await postgres.seedWorlds(tables, name)
789
+ ? pinned(await postgres.seedWorlds(tables, name, seed, pinning.pins, scopes), pinning, scopes)
790
+ : pinned(await postgres.seedWorlds(tables, name, {}, {}, scopes), unpinned, scopes)
683
791
  const ownAt = worlds.own.chain.findIndex((link) => link.type === dimension)
684
792
  const otherAt = worlds.other.chain.findIndex((link) => link.type === dimension)
685
793
  const ownValue = worlds.own.chain[ownAt]?.value
686
794
  const otherValue = worlds.other.chain[otherAt]?.value
687
795
  if (ownValue !== undefined && otherValue !== undefined) {
688
- own.push({ target: ownValue, chain: worlds.own.chain.slice(ownAt) })
689
- other.push({ target: otherValue, chain: worlds.other.chain.slice(otherAt) })
690
- values.push(...worldValues(worlds, 'other'))
796
+ const ownChain = worlds.own.chain.slice(ownAt)
797
+ const otherChain = worlds.other.chain.slice(otherAt)
798
+ const removals = Object.keys(scopes).length > 0 ? pinning.removals : []
799
+ const ownRows = await rowsIn(ownChain, named, removals)
800
+ const otherRows = await rowsIn(otherChain, named, removals)
801
+ own.push({ target: ownValue, chain: ownChain, rows: ownRows.rows })
802
+ other.push({ target: otherValue, chain: otherChain, rows: otherRows.rows })
803
+ values.push(...worldValues(worlds, 'other', scopes), ...otherRows.values)
691
804
  }
692
805
  }
693
806
  if (own.length === 0) {
694
- const scopes = await scopesOf(action, dimension, field, seed, {}, pinning)
807
+ const scopes = await scopesOf(action, dimension, field, seed, named, pinning)
695
808
  return { own: [scopes.own], other: [scopes.other], values: scopes.other.values }
696
809
  }
697
810
  return { own, other, values }
@@ -824,7 +937,7 @@ export function endpointAccessKit<DB, P>(
824
937
  if (owner === undefined) {
825
938
  throw new Error(`${row.id}: no table owns ${access.dimension}, so no link can be bound to one`)
826
939
  }
827
- const { own } = pinned(await postgres.seedWorlds(tables, owner, seed, pinning.pins), pinning)
940
+ const { own } = pinned(await postgres.seedWorlds(tables, owner, seed, pinning.pins), pinning, {})
828
941
  const holder = kit.testCaller([{ action: row.action, scope: { type: access.dimension, value: own.key } }], {
829
942
  kind: 'link',
830
943
  link: { mount, chain: own.chain },
@@ -834,7 +947,7 @@ export function endpointAccessKit<DB, P>(
834
947
  headers.set('Authorization', `Bearer ${holder.key ?? ''}`)
835
948
  return kit.fetch(request, { ...init, headers })
836
949
  }
837
- return { fetch, rows: (await rowsIn(own, rowFields(input))).rows }
950
+ return { fetch, rows: (await rowsIn(own.chain, rowFields(input), [])).rows }
838
951
  }
839
952
 
840
953
  // The cases of one call: `removals` takes the removal of the application rows it seeds, which `drained` runs once
@@ -843,6 +956,8 @@ export function endpointAccessKit<DB, P>(
843
956
  const accessKitOf = (removals: (() => Promise<void>)[]): EndpointAccessKit => {
844
957
  /** The pins of `endpoint`, whose seeded rows this call removes once it is over. */
845
958
  const pinningOf = (endpoint: string): Pinning => ({ pins: options.pins?.[endpoint] ?? {}, removals })
959
+ /** No pins, and the removals of this call: for a scope a case seeds beside its endpoint's own. */
960
+ const unpinned: Pinning = { pins: {}, removals }
846
961
 
847
962
  /** One row of each application table `appRows` names, for this call only. */
848
963
  const seedAppRows = async (appRows: AppRows): Promise<void> => {
@@ -857,7 +972,7 @@ export function endpointAccessKit<DB, P>(
857
972
  if (access.form !== 'on' && access.form !== 'in') {
858
973
  throw new Error(`${endpoint} has the access form ${access.form}; only on and in name an object or a scope value`)
859
974
  }
860
- const scopes = await scopesOf(action, access.dimension, fieldOf(row.input, access.from), seed, input, pinningOf(endpoint))
975
+ const scopes = await scopesOf(action, access.dimension, fieldOf(row.input, access.from), seed, rowFields(input), pinningOf(endpoint))
861
976
  const asked = addressing(input, access.from, scopes.other)
862
977
  const { status } = await ask(row, holding(row, input, scopes.own.grants, scopes.own.chain), asked, scopes.own.chain)
863
978
  return { status }
@@ -867,7 +982,7 @@ export function endpointAccessKit<DB, P>(
867
982
  const { action, access } = row
868
983
  if (access.form === 'in') {
869
984
  await seedAppRows(appRows)
870
- const scopes = await inScopesOf(action, access.dimension, fieldOf(row.input, access.from), seed, pinningOf(endpoint))
985
+ const scopes = await inScopesOf(action, access.dimension, fieldOf(row.input, access.from), seed, rowFields(input), pinningOf(endpoint))
871
986
  const grants = (targets: readonly InTarget[]): Grant[] =>
872
987
  targets.flatMap(({ target, chain }) => holding(row, input, [{ action, scope: { type: access.dimension, value: target } }], chain))
873
988
  const marks: string[] = []
@@ -886,8 +1001,8 @@ export function endpointAccessKit<DB, P>(
886
1001
  }
887
1002
  await seedAppRows(appRows)
888
1003
  const scopes = access.form === 'on'
889
- ? await scopesOf(action, access.dimension, fieldOf(row.input, access.from), seed, input, pinningOf(endpoint))
890
- : await listScopesOf(action, access.dimension)
1004
+ ? await scopesOf(action, access.dimension, fieldOf(row.input, access.from), seed, rowFields(input), pinningOf(endpoint))
1005
+ : await listScopesOf(action, access.dimension, rowFields(input), unpinned)
891
1006
  const from = access.form === 'on' ? access.from : null
892
1007
  // The other scope asks first, to fill whatever a handler keeps between requests; its pins differ from the own
893
1008
  // scope's, so a refusal the fixtures declare is its answer too. The own scope's answer is the one checked.
@@ -918,7 +1033,7 @@ export function endpointAccessKit<DB, P>(
918
1033
  const { status } = await askAs(row, holder.fetch, { ...input, ...holder.rows }, 'declared')
919
1034
  return { status }
920
1035
  }
921
- const scopes = await scopesOf(action, access.dimension, fieldOf(row.input, access.from), seed, input, pinningOf(endpoint))
1036
+ const scopes = await scopesOf(action, access.dimension, fieldOf(row.input, access.from), seed, rowFields(input), pinningOf(endpoint))
922
1037
  const { status } = await ask(
923
1038
  row,
924
1039
  holding(row, input, scopes.own.grants, scopes.own.chain),
@@ -967,25 +1082,39 @@ export function endpointAccessKit<DB, P>(
967
1082
  `${endpoint}: capability '${capability}' has no denied case: '${ruledOutBy}' needs its action too, ${named.action}, in a related dimension, so every caller that passes it is answered the list`,
968
1083
  )
969
1084
  }
1085
+ const fields = rowFields(input)
1086
+ for (const { field, capability: placed } of fields) {
1087
+ if (placed === null || !capabilities.has(placed)) {
1088
+ throw new Error(`${endpoint}: ${field} names a row in the scope of no capability of it, so the kit does not know where to seed it`)
1089
+ }
1090
+ }
1091
+ const rowsOf = (name: string): RowField[] => fields.filter((field) => field.capability === name)
970
1092
  const grants: Grant[] = []
971
1093
  let asked = input
1094
+ // The named one first: a known value another one's seed inserted would pass for a stored row, which takes no seed.
1095
+ if (access.form === 'on' || access.form === 'in') {
1096
+ const scopes = await scopesOf(named.action, access.dimension, fieldOf(named.input, access.from), seed, rowsOf(capability), pinningOf(endpoint))
1097
+ asked = addressing(asked, access.from, scopes.other)
1098
+ grants.push(...scopes.own.grants)
1099
+ }
972
1100
  for (const [name, other] of capabilities) {
973
1101
  if (name === capability) continue
974
1102
  // Each other one in a scope of its own, apart from the named one's: a global grant would also meet the named
975
1103
  // capability when both share an action. An `over` one lists from any value it is held in, so a fresh uuid does,
976
- // a valid key whether the dimension's is text or uuid, and no row of it is seeded.
1104
+ // a valid key whether the dimension's is text or uuid; it also holds the scope of each row it names.
977
1105
  if (other.access.form === 'over') {
978
- grants.push({ action: other.action, scope: { type: other.access.dimension, value: crypto.randomUUID() } })
1106
+ const listed = await listedRows(other.action, other.access.dimension, rowsOf(name))
1107
+ grants.push({ action: other.action, scope: { type: other.access.dimension, value: crypto.randomUUID() } }, ...listed.own.grants)
1108
+ asked = { ...asked, ...listed.own.rows }
979
1109
  } else if (other.access.form === 'on' || other.access.form === 'in') {
980
- const { own } = await scopesOf(other.action, other.access.dimension, fieldOf(other.input, other.access.from), {}, input)
1110
+ const { own } = await scopesOf(other.action, other.access.dimension, fieldOf(other.input, other.access.from), {}, rowsOf(name), unpinned)
981
1111
  asked = addressing(asked, other.access.from, own)
982
1112
  grants.push(...own.grants)
983
1113
  }
984
1114
  }
985
- if (access.form === 'on' || access.form === 'in') {
986
- const scopes = await scopesOf(named.action, access.dimension, fieldOf(named.input, access.from), seed, input, pinningOf(endpoint))
987
- asked = addressing(asked, access.from, scopes.other)
988
- grants.push(...scopes.own.grants)
1115
+ if (access.form === 'over') {
1116
+ // The caller holds the named action nowhere it lists, so its rows lie in a scope of their own.
1117
+ asked = { ...asked, ...(await listedRows(named.action, access.dimension, rowsOf(capability))).other.rows }
989
1118
  }
990
1119
  const { status } = await askAs(named, callerFor(named, grants).fetch, asked)
991
1120
  return { status }
@@ -207,10 +207,12 @@ export declare const GENERATED_INVITATION = "fabrika:invitation";
207
207
  export declare const GENERATED_LINK = "fabrika:link";
208
208
  /**
209
209
  * The prefix of the value a generated endpoint access case sends in an input field that names a row lying in its object
210
- * (`rows` of `endpoint-access.fixtures.ts`), followed by the row's table: `fabrika:row:note_comments`. The endpoint
211
- * access kit seeds one such row in each scope's object and sends that scope's key.
210
+ * or its list (`rows` of `endpoint-access.fixtures.ts`), followed by the row's table: `fabrika:row:note_comments`. The
211
+ * endpoint access kit seeds one such row in each scope and sends that scope's key.
212
212
  */
213
213
  export declare const GENERATED_ROW_PREFIX = "fabrika:row:";
214
+ /** What follows a row's table when the row lies in the scope of one capability of an endpoint of several: `@<name>`. */
215
+ export declare const GENERATED_ROW_CAPABILITY = "@";
214
216
  /**
215
217
  * The prefix of the value a generated endpoint access case sends in a file field, followed by the field's file kind:
216
218
  * `fabrika:file:attachments`. The endpoint access kit sends a small file of the kind's first type it can write in its place.