feat(structures): add Region/Delegation/Structure schema + seed (ADR-0027 PR 1)
Schema (Prisma): three new public-schema models per ADR-0027 - Region (INSEE code PK), Delegation (dept code PK + regionCode FK), Structure (portal-internal code PK + kind discriminator + nullable unique finess/siret/codePaie + nullable delegationCode FK). Migration: hand-written, matches the style of init_audit_schema and users_directory. DDL + CHECK constraint on Structure.kind mirroring STRUCTURE_KINDS + indexes + inline seed (Region 75 Nouvelle-Aquitaine, Delegation 33 Gironde, structures 0330800013 + 0330800021 medico-social Bordeaux/Merignac + siege APF national). Seed is the minimum the 19 test-tenant personas reference; ADR-0029's cascade sync supersedes it when it ships. Catalogue (TypeScript): apps/portal-bff/src/structures/structure-kind.ts exports STRUCTURE_KINDS as const + StructureKind type union + isStructureKind type guard. Jest spec covers catalogue content, type guard, narrowing. Drift gate: extended to scan property-literal "kind: 'X'" expressions in files that import from structure-kind.ts. Three-layer defense in depth for Structure.kind = TS type union + Postgres CHECK + CI gate. 22 spec tests passing. No consumer code yet - PrismaScopeResolver that dereferences Structure.code from UserScope.value lives in ADR-0026 PR 2, which depends on this PR landing first. Reviewer heads-up: schema.prisma already has a User model (ADR-0020 user-directory cache); ADR-0026 PR 1 introduces a different User (UUID PK + Person FK). Naming collision flagged for that PR to resolve - not touched here.
This commit is contained in:
@@ -15,6 +15,8 @@ import { tmpdir } from 'node:os';
|
||||
import { join } from 'node:path';
|
||||
import {
|
||||
extractCatalogues,
|
||||
extractStructureKinds,
|
||||
findStructureKindViolationsInFile,
|
||||
findViolationsInFile,
|
||||
scanWorkspace,
|
||||
} from './check-catalogue-drift.mjs';
|
||||
@@ -46,6 +48,17 @@ function makeFixture({ files }) {
|
||||
] as const;
|
||||
`,
|
||||
);
|
||||
mkdirSync(join(root, 'apps/portal-bff/src/structures'), { recursive: true });
|
||||
writeFileSync(
|
||||
join(root, 'apps/portal-bff/src/structures/structure-kind.ts'),
|
||||
`export const STRUCTURE_KINDS = [
|
||||
'medico_social',
|
||||
'antenne',
|
||||
'siege',
|
||||
] as const;
|
||||
export type StructureKind = (typeof STRUCTURE_KINDS)[number];
|
||||
`,
|
||||
);
|
||||
for (const [relativePath, contents] of Object.entries(files)) {
|
||||
const full = join(root, relativePath);
|
||||
mkdirSync(join(full, '..'), { recursive: true });
|
||||
@@ -160,6 +173,95 @@ describe('findViolationsInFile', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('extractStructureKinds', () => {
|
||||
it('reads the STRUCTURE_KINDS catalogue from structure-kind.ts', () => {
|
||||
const root = makeFixture({ files: {} });
|
||||
const kinds = extractStructureKinds(
|
||||
join(root, 'apps/portal-bff/src/structures/structure-kind.ts'),
|
||||
);
|
||||
assert.deepEqual([...kinds].sort(), ['antenne', 'medico_social', 'siege']);
|
||||
});
|
||||
|
||||
it('throws when the constant is missing', () => {
|
||||
const root = mkdtempSync(join(tmpdir(), 'drift-fixture-'));
|
||||
mkdirSync(join(root, 'apps/portal-bff/src/structures'), { recursive: true });
|
||||
writeFileSync(
|
||||
join(root, 'apps/portal-bff/src/structures/structure-kind.ts'),
|
||||
`export const NOT_STRUCTURE_KINDS = ['foo'] as const;\n`,
|
||||
);
|
||||
assert.throws(
|
||||
() =>
|
||||
extractStructureKinds(join(root, 'apps/portal-bff/src/structures/structure-kind.ts')),
|
||||
/STRUCTURE_KINDS/,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('findStructureKindViolationsInFile', () => {
|
||||
const validKinds = new Set(['medico_social', 'antenne', 'siege']);
|
||||
|
||||
it('skips files that do not import structure-kind (would false-positive)', () => {
|
||||
// `kind: 'whatever'` is a common pattern on unrelated objects.
|
||||
// Without the import filter, every such literal would be flagged.
|
||||
const text = `
|
||||
const event = { kind: 'somenoise', payload: {} };
|
||||
`;
|
||||
const violations = findStructureKindViolationsInFile('virtual.ts', validKinds, text);
|
||||
assert.equal(violations.length, 0);
|
||||
});
|
||||
|
||||
it('returns no violations when every kind literal is in the catalogue', () => {
|
||||
const text = `
|
||||
import type { StructureKind } from '../structures/structure-kind';
|
||||
const a = { kind: 'medico_social' as StructureKind };
|
||||
const b = { kind: 'siege' as StructureKind };
|
||||
`;
|
||||
const violations = findStructureKindViolationsInFile('virtual.ts', validKinds, text);
|
||||
assert.equal(violations.length, 0);
|
||||
});
|
||||
|
||||
it('flags every kind literal not in the catalogue', () => {
|
||||
const text = `
|
||||
import { STRUCTURE_KINDS } from '../structures/structure-kind';
|
||||
const a = { kind: 'medico_social' };
|
||||
const b = { kind: 'medical_social' };
|
||||
const c = { kind: 'sanitaire' };
|
||||
`;
|
||||
const violations = findStructureKindViolationsInFile('virtual.ts', validKinds, text);
|
||||
assert.equal(violations.length, 2);
|
||||
const values = violations.map((v) => v.value).sort();
|
||||
assert.deepEqual(values, ['medical_social', 'sanitaire']);
|
||||
assert.equal(violations[0].callee, 'Structure.kind');
|
||||
assert.equal(violations[0].catalogue, 'STRUCTURE_KINDS');
|
||||
});
|
||||
|
||||
it('skips non-literal initialisers (variable indirection)', () => {
|
||||
// The TypeScript signature catches this at compile time when the
|
||||
// target type is `StructureKind` — the gate is best-effort here.
|
||||
const text = `
|
||||
import { STRUCTURE_KINDS } from '../structures/structure-kind';
|
||||
const k = 'something';
|
||||
const a = { kind: k };
|
||||
`;
|
||||
const violations = findStructureKindViolationsInFile('virtual.ts', validKinds, text);
|
||||
assert.equal(violations.length, 0);
|
||||
});
|
||||
|
||||
it('reports line and column of the offending literal', () => {
|
||||
const text = [
|
||||
`import { STRUCTURE_KINDS } from '../structures/structure-kind';`,
|
||||
`const x = {`,
|
||||
` kind: 'rogue-kind',`,
|
||||
`};`,
|
||||
].join('\n');
|
||||
const violations = findStructureKindViolationsInFile('virtual.ts', validKinds, text);
|
||||
assert.equal(violations.length, 1);
|
||||
assert.equal(violations[0].line, 3);
|
||||
// 'rogue-kind' literal starts at column 9 (0-indexed 8, +1 in the report).
|
||||
assert.equal(violations[0].column, 9);
|
||||
});
|
||||
});
|
||||
|
||||
describe('scanWorkspace', () => {
|
||||
it('returns 0 violations on a clean fixture', () => {
|
||||
const root = makeFixture({
|
||||
@@ -230,4 +332,26 @@ describe('scanWorkspace', () => {
|
||||
const { violations } = scanWorkspace(root);
|
||||
assert.equal(violations.length, 1);
|
||||
});
|
||||
|
||||
it('aggregates decorator + Structure.kind violations together', () => {
|
||||
const root = makeFixture({
|
||||
files: {
|
||||
'apps/portal-bff/src/foo.ts': `
|
||||
import { STRUCTURE_KINDS } from './structures/structure-kind';
|
||||
@RequirePrivilege('Portal.Ghost') class Bad {}
|
||||
const s = { kind: 'phantom_kind' };
|
||||
`,
|
||||
},
|
||||
});
|
||||
const { violations } = scanWorkspace(root);
|
||||
assert.equal(violations.length, 2);
|
||||
const values = violations.map((v) => v.value).sort();
|
||||
assert.deepEqual(values, ['Portal.Ghost', 'phantom_kind']);
|
||||
});
|
||||
|
||||
it('returns the structureKinds set alongside catalogues', () => {
|
||||
const root = makeFixture({ files: {} });
|
||||
const { structureKinds } = scanWorkspace(root);
|
||||
assert.deepEqual([...structureKinds].sort(), ['antenne', 'medico_social', 'siege']);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user