|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [ADR-0131 D3/D5] The one definition of the Default Organization, pinned on |
| 5 | + * its own: `slug = 'default'`, else the only organization, else none. Every |
| 6 | + * reader (the resolver, plugin-auth's boot invariant, bootstrap, tenancy |
| 7 | + * service and last-admin guard, plugin-security's promotion, the ownership |
| 8 | + * ceremony) hands its read to this rule, so these cases are what all of them |
| 9 | + * answer. |
| 10 | + */ |
| 11 | + |
| 12 | +import { describe, expect, it } from 'vitest'; |
| 13 | + |
| 14 | +import { DEFAULT_ORGANIZATION_SLUG, findDefaultOrganization } from './default-organization.js'; |
| 15 | +import { readDefaultOrganizationId } from './resolve-authz-context.js'; |
| 16 | + |
| 17 | +type Row = { id?: unknown; slug?: string }; |
| 18 | + |
| 19 | +/** A read over `rows` that answers the two questions the rule asks, and records them. */ |
| 20 | +function readOver(rows: Row[]) { |
| 21 | + const asked: Array<{ where: Readonly<Record<string, unknown>>; limit: number }> = []; |
| 22 | + const read = async (where: Readonly<Record<string, unknown>>, limit: number): Promise<Row[]> => { |
| 23 | + asked.push({ where, limit }); |
| 24 | + return rows.filter((row) => !('slug' in where) || row.slug === where.slug).slice(0, limit); |
| 25 | + }; |
| 26 | + return { read, asked }; |
| 27 | +} |
| 28 | + |
| 29 | +describe('[ADR-0131 D3/D5] findDefaultOrganization — the one definition', () => { |
| 30 | + it('the organization slugged `default` wins, beside others, and the second read is never issued', async () => { |
| 31 | + const { read, asked } = readOver([{ id: 'org_acme', slug: 'acme' }, { id: 'org_def', slug: 'default' }]); |
| 32 | + expect(await findDefaultOrganization(read)).toEqual({ id: 'org_def', row: { id: 'org_def', slug: 'default' }, several: false }); |
| 33 | + expect(asked).toEqual([{ where: { slug: DEFAULT_ORGANIZATION_SLUG }, limit: 1 }]); |
| 34 | + }); |
| 35 | + |
| 36 | + it('else the only organization, whatever its slug', async () => { |
| 37 | + const { read, asked } = readOver([{ id: 'org_acme', slug: 'acme' }]); |
| 38 | + expect(await findDefaultOrganization(read)).toMatchObject({ id: 'org_acme', several: false }); |
| 39 | + expect(asked).toEqual([{ where: { slug: 'default' }, limit: 1 }, { where: {}, limit: 2 }]); |
| 40 | + }); |
| 41 | + |
| 42 | + it('several, none of them `default`, name none — and say so', async () => { |
| 43 | + const { read } = readOver([{ id: 'org_a', slug: 'a' }, { id: 'org_b', slug: 'b' }]); |
| 44 | + expect(await findDefaultOrganization(read)).toEqual({ id: undefined, row: undefined, several: true }); |
| 45 | + }); |
| 46 | + |
| 47 | + it('no organization names none, and is not "several"', async () => { |
| 48 | + const { read } = readOver([]); |
| 49 | + expect(await findDefaultOrganization(read)).toEqual({ id: undefined, row: undefined, several: false }); |
| 50 | + }); |
| 51 | + |
| 52 | + it('a row with no usable id names nothing: a blank-id `default` falls through to the only-organization reading', async () => { |
| 53 | + expect((await findDefaultOrganization(readOver([{ id: '', slug: 'default' }]).read)).id).toBeUndefined(); |
| 54 | + expect((await findDefaultOrganization(readOver([{ id: null, slug: 'default' }, { id: 'org_b', slug: 'b' }]).read)).id) |
| 55 | + .toBeUndefined(); |
| 56 | + }); |
| 57 | + |
| 58 | + it('a read that fails propagates: what an unreadable store means is the caller\'s read policy', async () => { |
| 59 | + const boom = new Error('store unreachable'); |
| 60 | + await expect(findDefaultOrganization(async () => { throw boom; })).rejects.toBe(boom); |
| 61 | + }); |
| 62 | +}); |
| 63 | + |
| 64 | +describe('[ADR-0131 D5] readDefaultOrganizationId — the resolver\'s read over the one definition', () => { |
| 65 | + const engineOver = (rows: Row[], registered = true) => ({ |
| 66 | + registry: { getObject: (name: string) => (registered && name === 'sys_organization' ? { name } : undefined) }, |
| 67 | + async find(_object: string, query: { where?: Record<string, unknown>; limit?: number }) { |
| 68 | + return readOver(rows).read(query.where ?? {}, query.limit ?? 100); |
| 69 | + }, |
| 70 | + }); |
| 71 | + |
| 72 | + it('answers what the definition answers', async () => { |
| 73 | + expect(await readDefaultOrganizationId(engineOver([{ id: 'org_acme', slug: 'acme' }]))).toBe('org_acme'); |
| 74 | + expect(await readDefaultOrganizationId(engineOver([{ id: 'org_acme', slug: 'acme' }, { id: 'org_b', slug: 'b' }]))).toBeUndefined(); |
| 75 | + }); |
| 76 | + |
| 77 | + it('a composition that registers no organization object holds none, and is not read', async () => { |
| 78 | + expect(await readDefaultOrganizationId(engineOver([{ id: 'org_acme', slug: 'acme' }], false))).toBeUndefined(); |
| 79 | + }); |
| 80 | +}); |
0 commit comments