|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import { describe, it, expect, vi } from 'vitest'; |
| 4 | +import { claimOrgSeedOwnership } from './claim-org-seed-ownership.js'; |
| 5 | + |
| 6 | +const ORG = 'org_1'; |
| 7 | +const OWNER = 'usr_admin'; |
| 8 | + |
| 9 | +function makeQL(schemas: any[], rowsByObject: Record<string, any[]>) { |
| 10 | + const updates: { object: string; data: any }[] = []; |
| 11 | + const ql: any = { |
| 12 | + registry: { getAllObjects: () => schemas }, |
| 13 | + find: vi.fn(async (object: string, query: any) => { |
| 14 | + const all = rowsByObject[object] ?? []; |
| 15 | + const w = query?.where ?? {}; |
| 16 | + return all.filter((r) => { |
| 17 | + if ('organization_id' in w && (r.organization_id ?? null) !== (w.organization_id ?? null)) return false; |
| 18 | + if ('owner_id' in w && (r.owner_id ?? null) !== (w.owner_id ?? null)) return false; |
| 19 | + return true; |
| 20 | + }); |
| 21 | + }), |
| 22 | + update: vi.fn(async (object: string, data: any) => { |
| 23 | + updates.push({ object, data }); |
| 24 | + const row = (rowsByObject[object] ?? []).find((r) => r.id === data.id); |
| 25 | + if (row) row.owner_id = data.owner_id; |
| 26 | + return row; |
| 27 | + }), |
| 28 | + }; |
| 29 | + return { ql, updates }; |
| 30 | +} |
| 31 | + |
| 32 | +describe('claimOrgSeedOwnership', () => { |
| 33 | + it('returns [] when registry is unavailable', async () => { |
| 34 | + const ql: any = { find: vi.fn(), update: vi.fn() }; |
| 35 | + expect(await claimOrgSeedOwnership(ql, ORG, OWNER)).toEqual([]); |
| 36 | + }); |
| 37 | + |
| 38 | + it('no-ops without an org or owner', async () => { |
| 39 | + const schemas = [{ name: 'crm_lead', fields: [{ name: 'owner_id' }, { name: 'organization_id' }] }]; |
| 40 | + const { ql, updates } = makeQL(schemas, { crm_lead: [{ id: 'l1', organization_id: ORG, owner_id: null }] }); |
| 41 | + expect(await claimOrgSeedOwnership(ql, '', OWNER)).toEqual([]); |
| 42 | + expect(await claimOrgSeedOwnership(ql, ORG, '')).toEqual([]); |
| 43 | + expect(updates).toHaveLength(0); |
| 44 | + }); |
| 45 | + |
| 46 | + it('skips managedBy / sys_* and objects missing owner_id or organization_id', async () => { |
| 47 | + const schemas = [ |
| 48 | + { name: 'sys_user', managedBy: 'better-auth', fields: [{ name: 'owner_id' }, { name: 'organization_id' }] }, |
| 49 | + { name: 'sys_widget', fields: [{ name: 'owner_id' }, { name: 'organization_id' }] }, |
| 50 | + { name: 'crm_pricebook', fields: [{ name: 'organization_id' }] }, // no owner_id |
| 51 | + { name: 'crm_global', fields: [{ name: 'owner_id' }] }, // no organization_id |
| 52 | + ]; |
| 53 | + const { ql, updates } = makeQL(schemas, { |
| 54 | + sys_user: [{ id: 'u1', organization_id: ORG, owner_id: null }], |
| 55 | + sys_widget: [{ id: 'w1', organization_id: ORG, owner_id: null }], |
| 56 | + crm_pricebook: [{ id: 'p1', organization_id: ORG }], |
| 57 | + crm_global: [{ id: 'g1', owner_id: null }], |
| 58 | + }); |
| 59 | + expect(await claimOrgSeedOwnership(ql, ORG, OWNER)).toEqual([]); |
| 60 | + expect(updates).toHaveLength(0); |
| 61 | + }); |
| 62 | + |
| 63 | + it('claims this org\'s NULL-owner rows only, leaving other orgs and human-owned rows untouched', async () => { |
| 64 | + const schemas = [{ name: 'crm_lead', fields: [{ name: 'owner_id' }, { name: 'organization_id' }] }]; |
| 65 | + const rows = [ |
| 66 | + { id: 'l1', organization_id: ORG, owner_id: null }, // claimed |
| 67 | + { id: 'l2', organization_id: ORG, owner_id: 'usr_someone' }, // already owned — untouched |
| 68 | + { id: 'l3', organization_id: 'org_2', owner_id: null }, // other org — untouched |
| 69 | + ]; |
| 70 | + const { ql, updates } = makeQL(schemas, { crm_lead: rows }); |
| 71 | + const result = await claimOrgSeedOwnership(ql, ORG, OWNER); |
| 72 | + |
| 73 | + expect(result).toEqual([{ object: 'crm_lead', count: 1 }]); |
| 74 | + expect(updates).toHaveLength(1); |
| 75 | + expect(updates[0].data).toMatchObject({ id: 'l1', owner_id: OWNER }); |
| 76 | + expect(rows.find((r) => r.id === 'l2')!.owner_id).toBe('usr_someone'); |
| 77 | + expect(rows.find((r) => r.id === 'l3')!.owner_id).toBeNull(); |
| 78 | + }); |
| 79 | + |
| 80 | + it('is idempotent — a second run claims nothing', async () => { |
| 81 | + const schemas = [{ name: 'crm_lead', fields: [{ name: 'owner_id' }, { name: 'organization_id' }] }]; |
| 82 | + const { ql } = makeQL(schemas, { crm_lead: [{ id: 'l1', organization_id: ORG, owner_id: null }] }); |
| 83 | + await claimOrgSeedOwnership(ql, ORG, OWNER); |
| 84 | + expect(await claimOrgSeedOwnership(ql, ORG, OWNER)).toEqual([]); |
| 85 | + }); |
| 86 | +}); |
0 commit comments