|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * PIN: `os generate permission NAME` and `os generate dataset NAME` write a |
| 5 | + * permission set and an analytics dataset into the directories the blank |
| 6 | + * starter wires, each bound to an object the stack declares. |
| 7 | + * |
| 8 | + * ## What was measured before the rows existed |
| 9 | + * |
| 10 | + * On `origin/main` 5fa9375e, `os generate permission` and |
| 11 | + * `os generate dataset` each printed `Unknown type:` and exited 1, and the |
| 12 | + * blank starter wired no `src/security` or `src/datasets` barrel. An app that |
| 13 | + * real users sign in to needs a permission set, and a dashboard widget binds |
| 14 | + * to a dataset; both could be written only by hand, in a directory the author |
| 15 | + * also had to wire by hand. |
| 16 | + * |
| 17 | + * ## What this file holds, and what it leaves to the derived pins |
| 18 | + * |
| 19 | + * The roster facts the starter depends on — the type key, the directory, the |
| 20 | + * stack key — and the scaffold's own shape: it parses under the schema the |
| 21 | + * stack key carries, under the name `os g` reports it reached, and it names |
| 22 | + * the object it was bound to, never one derived from its own name. That every |
| 23 | + * scaffold passes `os validate`, `os build` and `os lint` with zero findings, |
| 24 | + * type-checks, and reaches the stack is held for the whole roster by |
| 25 | + * `test/generate-scaffold-validates.test.ts`, |
| 26 | + * `test/scaffold-emission-typechecks.test.ts` and |
| 27 | + * `test/generate-scaffolds-reach-stack.e2e.test.ts`; the blank starter's |
| 28 | + * wiring by `test/create-objectstack-wiring-parity.test.ts`. |
| 29 | + * |
| 30 | + * Every other fact is READ off the roster (`GENERATOR_SCAFFOLD_TARGETS`) and |
| 31 | + * the registry (`metadataFileName`). |
| 32 | + */ |
| 33 | + |
| 34 | +import { afterAll, describe, expect, it } from 'vitest'; |
| 35 | +import fs from 'node:fs'; |
| 36 | +import path from 'node:path'; |
| 37 | +import { fileURLToPath } from 'node:url'; |
| 38 | +import { bundleRequire } from 'bundle-require'; |
| 39 | +import { PermissionSetSchema } from '@objectstack/spec/security'; |
| 40 | +import { DatasetSchema } from '@objectstack/spec/ui'; |
| 41 | +import { GENERATOR_SCAFFOLD_TARGETS, type ScaffoldObjectBinding } from './generate.js'; |
| 42 | +import { metadataFileName } from '../utils/metadata-file-name.js'; |
| 43 | +import { BUNDLE_REQUIRE_EXTERNALS } from '../utils/config.js'; |
| 44 | + |
| 45 | +const STEM = 'sales_user'; |
| 46 | + |
| 47 | +/** Deliberately NOT named like the item: a binding derived from the name would miss it. */ |
| 48 | +const TARGET: ScaffoldObjectBinding = { |
| 49 | + name: 'pin_account', |
| 50 | + label: 'Account', |
| 51 | + fields: ['name', 'industry'], |
| 52 | + displayField: 'name', |
| 53 | +}; |
| 54 | + |
| 55 | +const target = (type: string) => GENERATOR_SCAFFOLD_TARGETS.find((t) => t.type === type); |
| 56 | + |
| 57 | +/** |
| 58 | + * Inside this package's own `node_modules`, for the reason |
| 59 | + * `test/generate-scaffold-validates.test.ts` gives: git-ignored, and the |
| 60 | + * scaffold's `@objectstack/spec/*` import resolves from here exactly as it |
| 61 | + * would in an author's project. |
| 62 | + */ |
| 63 | +const TMP_ROOT = fs.mkdtempSync( |
| 64 | + path.join(path.dirname(fileURLToPath(import.meta.url)), '..', '..', 'node_modules', '.generate-permission-dataset-pin-'), |
| 65 | +); |
| 66 | + |
| 67 | +afterAll(() => { |
| 68 | + fs.rmSync(TMP_ROOT, { recursive: true, force: true }); |
| 69 | +}); |
| 70 | + |
| 71 | +/** Materialize a scaffold through the loader `os validate` uses. */ |
| 72 | +async function loadScaffold(type: string, object: ScaffoldObjectBinding): Promise<Record<string, unknown>> { |
| 73 | + const gen = target(type); |
| 74 | + if (!gen) throw new Error(`no \`${type}\` generator on the roster`); |
| 75 | + const dir = fs.mkdtempSync(path.join(TMP_ROOT, `${type}-`)); |
| 76 | + const file = path.join(dir, metadataFileName(type, STEM) ?? 'unnamed.ts'); |
| 77 | + fs.writeFileSync(file, gen.generate(STEM, undefined, { object }), 'utf8'); |
| 78 | + const { mod } = await bundleRequire({ filepath: file, external: BUNDLE_REQUIRE_EXTERNALS }); |
| 79 | + return ((mod as { default?: unknown }).default ?? mod) as Record<string, unknown>; |
| 80 | +} |
| 81 | + |
| 82 | +describe('`os generate permission` and `os generate dataset` are on the roster, where the starter wires them', () => { |
| 83 | + it.each([ |
| 84 | + ['permission', 'src/security', 'permissions'], |
| 85 | + ['dataset', 'src/datasets', 'datasets'], |
| 86 | + ])('%s scaffolds into %s, collected under the `%s` stack key', (type, dir, stackKey) => { |
| 87 | + const gen = target(type); |
| 88 | + expect(gen, `the \`${type}\` generator`).toBeDefined(); |
| 89 | + expect(gen!.defaultDir).toBe(dir); |
| 90 | + expect(gen!.stackKey).toBe(stackKey); |
| 91 | + // Both bind an object the stack declares, from `--object` or the only one. |
| 92 | + expect(gen!.binds).toEqual({ object: 'flag' }); |
| 93 | + expect(gen!.namesObject).toBe(true); |
| 94 | + expect(gen!.requires).toEqual([]); |
| 95 | + }); |
| 96 | + |
| 97 | + it.each(['permission', 'dataset'])('writes NAME.%s.ts, the registry\'s own pattern for the kind', (type) => { |
| 98 | + expect(metadataFileName(type, STEM)).toBe(`${STEM}.${type}.ts`); |
| 99 | + }); |
| 100 | +}); |
| 101 | + |
| 102 | +describe('the permission scaffold is a set that grants the bound object', () => { |
| 103 | + it('parses as a permission set named what `os g` reports it reached', async () => { |
| 104 | + const set = await loadScaffold('permission', TARGET); |
| 105 | + expect(PermissionSetSchema.safeParse(set).success).toBe(true); |
| 106 | + expect(set.name).toBe(target('permission')!.itemName(STEM)); |
| 107 | + }); |
| 108 | + |
| 109 | + it('grants the bound object, and only it, without delete', async () => { |
| 110 | + const set = await loadScaffold('permission', TARGET); |
| 111 | + expect(Object.keys(set.objects as object)).toEqual([TARGET.name]); |
| 112 | + expect((set.objects as Record<string, Record<string, unknown>>)[TARGET.name]).toMatchObject({ |
| 113 | + allowRead: true, |
| 114 | + allowCreate: true, |
| 115 | + allowEdit: true, |
| 116 | + allowDelete: false, |
| 117 | + // The read depth is declared, not left to the object's sharing model. |
| 118 | + readScope: 'own', |
| 119 | + }); |
| 120 | + // Never bound to every signed-in user by the scaffold. |
| 121 | + expect(set.isDefault).toBe(false); |
| 122 | + }); |
| 123 | +}); |
| 124 | + |
| 125 | +describe('the dataset scaffold is a dataset over the bound object', () => { |
| 126 | + it('parses as a dataset named what `os g` reports it reached, over the bound object', async () => { |
| 127 | + const dataset = await loadScaffold('dataset', TARGET); |
| 128 | + expect(DatasetSchema.safeParse(dataset).success).toBe(true); |
| 129 | + expect(dataset.name).toBe(target('dataset')!.itemName(STEM)); |
| 130 | + expect(dataset.object).toBe(TARGET.name); |
| 131 | + }); |
| 132 | + |
| 133 | + it('declares a record count, grouped by the record\'s title field', async () => { |
| 134 | + const dataset = await loadScaffold('dataset', TARGET); |
| 135 | + expect(dataset.measures).toEqual([{ name: 'record_count', label: 'Records', aggregate: 'count' }]); |
| 136 | + expect(dataset.dimensions).toEqual([{ name: TARGET.displayField, field: TARGET.displayField }]); |
| 137 | + }); |
| 138 | + |
| 139 | + it('an object with no title field gets no dimension, and the dataset still parses', async () => { |
| 140 | + const dataset = await loadScaffold('dataset', { ...TARGET, displayField: undefined }); |
| 141 | + expect(dataset.dimensions).toEqual([]); |
| 142 | + expect(DatasetSchema.safeParse(dataset).success).toBe(true); |
| 143 | + }); |
| 144 | +}); |
0 commit comments