@fabrika/check 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.
Files changed (2) hide show
  1. package/package.json +6 -6
  2. package/src/scaffold.ts +81 -16
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fabrika/check",
3
- "version": "0.9.0-dev.10",
3
+ "version": "0.9.0-dev.11",
4
4
  "type": "module",
5
5
  "repository": {
6
6
  "type": "git",
@@ -47,11 +47,11 @@
47
47
  },
48
48
  "dependencies": {
49
49
  "@babel/parser": "8.0.7",
50
- "@fabrika/app": "0.9.0-dev.10",
51
- "@fabrika/auth-core": "0.9.0-dev.10",
52
- "@fabrika/db": "0.9.0-dev.10",
53
- "@fabrika/operations-contract": "0.9.0-dev.10",
54
- "@fabrika/platform-node": "0.9.0-dev.10",
50
+ "@fabrika/app": "0.9.0-dev.11",
51
+ "@fabrika/auth-core": "0.9.0-dev.11",
52
+ "@fabrika/db": "0.9.0-dev.11",
53
+ "@fabrika/operations-contract": "0.9.0-dev.11",
54
+ "@fabrika/platform-node": "0.9.0-dev.11",
55
55
  "zod": "^3.25.76"
56
56
  }
57
57
  }
package/src/scaffold.ts CHANGED
@@ -9,9 +9,11 @@ import {
9
9
  GENERATED_INVITATION,
10
10
  GENERATED_LINK,
11
11
  GENERATED_MEMBER,
12
+ GENERATED_ROW_CAPABILITY,
12
13
  GENERATED_ROW_PREFIX,
13
14
  } from '@fabrika/app/contract'
14
15
  import {
16
+ type AccessForm,
15
17
  type ContractArtifact,
16
18
  endpointDirectory,
17
19
  endpointMembers,
@@ -304,7 +306,8 @@ const isLinkProcedure = (row: EndpointRow): boolean =>
304
306
  * The input an endpoint's cases send: the author's fixture, else a sample of its schema; `undefined` when neither
305
307
  * exists. Each field its `members`, `invitations` or `links` name holds `GENERATED_MEMBER`, `GENERATED_INVITATION` or
306
308
  * `GENERATED_LINK`, which the kit replaces with a real member, an open invitation or a link it issued; each field its
307
- * `rows` name holds `GENERATED_ROW_PREFIX` and the table, which the kit replaces with the key of a row it seeded; each file
309
+ * `rows` name holds `GENERATED_ROW_PREFIX` and the table (and, under `<endpoint id>#<capability>`, the capability), which
310
+ * the kit replaces with the key of a row it seeded; each file
308
311
  * field its `files` name holds its kind's marker (in a list, for a list of files), even where the sample left an optional
309
312
  * one out, which the kit sends as the named file.
310
313
  */
@@ -320,6 +323,11 @@ const inputOf = (row: EndpointRow, fixtures: EndpointFixtures): Readonly<Record<
320
323
  ...generated(fixtures.invitations[row.id], GENERATED_INVITATION),
321
324
  ...generated(fixtures.links[row.id], GENERATED_LINK),
322
325
  ...Object.entries(fixtures.rows[row.id] ?? {}).map(([field, table]) => [field, `${GENERATED_ROW_PREFIX}${table}`]),
326
+ ...Object.keys(row.requirement?.kind === 'capabilities' ? row.requirement.capabilities : {}).flatMap((name) =>
327
+ Object.entries(fixtures.rows[`${row.id}${CAPABILITY_KEY}${name}`] ?? {}).map((
328
+ [field, table],
329
+ ) => [field, `${GENERATED_ROW_PREFIX}${table}${GENERATED_ROW_CAPABILITY}${name}`])
330
+ ),
323
331
  ...Object.keys(fixtures.files[row.id] ?? {}).flatMap((field) => {
324
332
  const file = fileFieldOf(row.input, field)
325
333
  if (file === undefined) return []
@@ -330,6 +338,9 @@ const inputOf = (row: EndpointRow, fixtures: EndpointFixtures): Readonly<Record<
330
338
  }
331
339
  }
332
340
 
341
+ /** What separates an endpoint id from a capability's name in a `rows` key: `api.tasks.copy#from`. */
342
+ const CAPABILITY_KEY = '#'
343
+
333
344
  /** The file kind of a top-level file field of `input` (a file, an optional or nullable one, or a list of them), or `undefined`. */
334
345
  const fileFieldOf = (input: SchemaShape | null, field: string): { readonly kind: string; readonly list: boolean } | undefined => {
335
346
  const kindOf = (shape: SchemaShape | undefined): string | undefined =>
@@ -421,6 +432,29 @@ const liesDirectlyIn = (policies: Policies, table: string, dimension: string): b
421
432
  return parent?.mode === 'dimension' && parent.dimension === dimension
422
433
  }
423
434
 
435
+ /**
436
+ * Whether a list of `dimension` (`over`) reaches the rows of `table`: a dimension table it contains or of its own when it
437
+ * has no container, or a child of one.
438
+ */
439
+ const listedBy = (policies: Policies, table: string, dimension: string): boolean => {
440
+ const listedIn = (scope: Policies[string]['scope'] | undefined): string | undefined =>
441
+ scope?.mode === 'dimension' ? (scope.container?.dimension ?? scope.dimension) : undefined
442
+ const scope = policies[table]?.scope
443
+ return (scope?.mode === 'child' ? listedIn(policies[scope.parent]?.scope) : listedIn(scope)) === dimension
444
+ }
445
+
446
+ /**
447
+ * Whether the kit seeds a row of `table` for an endpoint of `access`: in its object (`on`, `in` an object a table owns,
448
+ * the table lying directly in it) or in the scopes it lists (`over`).
449
+ */
450
+ const rowPlace = (policies: Policies, access: ScopedAccess, table: string): boolean =>
451
+ access.form === 'over'
452
+ ? listedBy(policies, table, access.dimension)
453
+ : ownerRead(policies, access.dimension) !== undefined && liesDirectlyIn(policies, table, access.dimension)
454
+
455
+ /** An access form that names a scope: an object (`on`), a value (`in`) or a list (`over`). */
456
+ type ScopedAccess = Extract<AccessForm, { readonly form: 'on' | 'in' | 'over' }>
457
+
424
458
  /** The read action of the table that owns `dimension`, or `undefined` when no table owns it. */
425
459
  const ownerRead = (policies: Policies, dimension: string): string | undefined =>
426
460
  tableActions(
@@ -714,6 +748,23 @@ const appRowAllowed = (refusal: ApplicationRowRefusal): string => {
714
748
  }
715
749
  }
716
750
 
751
+ /** The finding of a `rows` table the kit seeds no row of for an endpoint of `access`, and its allowed form. */
752
+ const unplacedRow = (policies: Policies, id: string, table: string, access: ScopedAccess): readonly [string, string] => {
753
+ if (access.form === 'over') {
754
+ return [
755
+ `${id}: a list of ${access.dimension} reaches no ${table} row, so the kit seeds none in the scopes it lists`,
756
+ `a dimension table whose container is ${access.dimension}, or a child table of one`,
757
+ ]
758
+ }
759
+ if (ownerRead(policies, access.dimension) === undefined) {
760
+ return [`${id}: no table owns ${access.dimension}, so the kit seeds no ${table} row in one`, `an endpoint on or in a dimension a table owns`]
761
+ }
762
+ return [
763
+ `${id}: ${table} is no dimension or child table that lies directly in a ${access.dimension}, so the kit seeds none in the endpoint's object (it seeds no container between)`,
764
+ `a child table of the table that owns ${access.dimension}, or a dimension table whose container is ${access.dimension}`,
765
+ ]
766
+ }
767
+
717
768
  /** What the scaffold reads from an application's files; throws `ContractSourceError` or `ContractError` as the reader does. */
718
769
  export const scaffoldInputs = (files: ReadonlyMap<string, string>): ScaffoldInputs => {
719
770
  const tables = readTablesDefinition(files)
@@ -803,29 +854,43 @@ export const missingEndpointFixtures = ({ tables, policies, artifact, fixtures }
803
854
  }
804
855
  }
805
856
  }
806
- for (const [id, fields] of Object.entries(fixtures.rows).sort(byKey)) {
857
+ for (const [key, fields] of Object.entries(fixtures.rows).sort(byKey)) {
858
+ const split = key.indexOf(CAPABILITY_KEY)
859
+ const id = split === -1 ? key : key.slice(0, split)
860
+ const named = split === -1 ? null : key.slice(split + 1)
807
861
  const row = tested.find((candidate) => candidate.id === id)
808
- const access = row?.requirement?.kind === 'action' ? row.requirement.access : undefined
809
- const owned = access?.form === 'on' && ownerRead(policies, access.dimension) !== undefined
810
- if (row === undefined || access?.form !== 'on' || !owned) {
862
+ const requirement = row?.requirement
863
+ const severalCapabilities = requirement?.kind === 'capabilities'
864
+ const access = requirement?.kind === 'action' && named === null
865
+ ? requirement.access
866
+ : requirement?.kind === 'capabilities' && named !== null && Object.hasOwn(requirement.capabilities, named)
867
+ ? requirement.capabilities[named]?.access
868
+ : undefined
869
+ const scope = access?.form === 'on' || access?.form === 'in' || access?.form === 'over' ? access : undefined
870
+ if (row === undefined || scope === undefined) {
811
871
  findings.push(
812
- at(
813
- `${id} is no tested endpoint on an object a table owns, in which the kit could seed a row`,
814
- "export const rows = { '<endpoint id>': { <input field>: '<table>' } }, for an endpoint on a dimension a table owns",
815
- ),
872
+ severalCapabilities && named === null
873
+ ? at(
874
+ `${id} has several capabilities, so its rows name the one whose scope they lie in`,
875
+ `export const rows = { '${id}${CAPABILITY_KEY}<capability>': { <input field>: '<table>' } }`,
876
+ )
877
+ : named !== null
878
+ ? at(
879
+ `${key} names no capability of a tested endpoint with several, in whose scope the kit could seed a row`,
880
+ `export const rows = { '<endpoint id>${CAPABILITY_KEY}<capability>': { <input field>: '<table>' } }, a capability of the endpoint`,
881
+ )
882
+ : at(
883
+ `${id} is no tested endpoint on, in or over a scope, in which the kit could seed a row`,
884
+ "export const rows = { '<endpoint id>': { <input field>: '<table>' } }, for an endpoint on, in or over a dimension",
885
+ ),
816
886
  )
817
887
  continue
818
888
  }
819
889
  for (const [field, table] of Object.entries(fields).sort(byKey)) {
820
890
  if (row.input?.type !== 'object' || !admitsString(row.input.fields[field])) {
821
891
  findings.push(at(`${id}: ${field} is no string field of its input, so it cannot name a row`, 'a top-level string field of the input'))
822
- } else if (!liesDirectlyIn(policies, table, access.dimension)) {
823
- findings.push(
824
- at(
825
- `${id}: ${table} is no dimension or child table that lies directly in a ${access.dimension}, so the kit seeds none in the endpoint's object (it seeds no container between)`,
826
- `a child table of the table that owns ${access.dimension}, or a dimension table whose container is ${access.dimension}`,
827
- ),
828
- )
892
+ } else if (!rowPlace(policies, scope, table)) {
893
+ findings.push(at(...unplacedRow(policies, id, table, scope)))
829
894
  }
830
895
  }
831
896
  }