|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 4 | +import { AppPlugin } from './app-plugin'; |
| 5 | +import type { PluginContext } from '@objectstack/core'; |
| 6 | + |
| 7 | +/** |
| 8 | + * #2996 — AppPlugin emits `app:seeded` when its inline seed settles, so |
| 9 | + * reconcilers that read seeded rows (plugin-auth's ADR-0093 D6 membership |
| 10 | + * backfill) can re-run for users inserted by a seed that overran the inline |
| 11 | + * budget and finished in the background AFTER `kernel:ready`. |
| 12 | + * |
| 13 | + * These tests force the basic-insert fallback path (no `metadata` service) so |
| 14 | + * the SeedLoaderService plumbing is unnecessary — the settle signalling is the |
| 15 | + * same on both branches. |
| 16 | + */ |
| 17 | +describe('AppPlugin inline-seed settle signal (app:seeded, #2996)', () => { |
| 18 | + const OLD_BUDGET = process.env.OS_INLINE_SEED_BUDGET_MS; |
| 19 | + const OLD_MULTI = process.env.OS_MULTI_ORG_ENABLED; |
| 20 | + |
| 21 | + let trigger: ReturnType<typeof vi.fn>; |
| 22 | + let insert: ReturnType<typeof vi.fn>; |
| 23 | + |
| 24 | + const makeContext = (overrides: Partial<PluginContext> = {}): PluginContext => |
| 25 | + ({ |
| 26 | + logger: { info: vi.fn(), error: vi.fn(), warn: vi.fn(), debug: vi.fn() }, |
| 27 | + registerService: vi.fn(), |
| 28 | + getService: vi.fn((name: string) => { |
| 29 | + if (name === 'objectql') return { insert }; |
| 30 | + // `metadata` absent → basic-insert fallback; all others absent too. |
| 31 | + return undefined; |
| 32 | + }), |
| 33 | + getServices: vi.fn(() => new Map()), |
| 34 | + hook: vi.fn(), |
| 35 | + trigger, |
| 36 | + ...overrides, |
| 37 | + }) as unknown as PluginContext; |
| 38 | + |
| 39 | + const bundleWithUser = () => ({ |
| 40 | + id: 'seed-test-app', |
| 41 | + data: [{ object: 'sys_user', records: [{ id: 'seeded_u1' }] }], |
| 42 | + }); |
| 43 | + |
| 44 | + beforeEach(() => { |
| 45 | + delete process.env.OS_MULTI_ORG_ENABLED; |
| 46 | + trigger = vi.fn(); |
| 47 | + }); |
| 48 | + |
| 49 | + afterEach(() => { |
| 50 | + if (OLD_BUDGET === undefined) delete process.env.OS_INLINE_SEED_BUDGET_MS; |
| 51 | + else process.env.OS_INLINE_SEED_BUDGET_MS = OLD_BUDGET; |
| 52 | + if (OLD_MULTI === undefined) delete process.env.OS_MULTI_ORG_ENABLED; |
| 53 | + else process.env.OS_MULTI_ORG_ENABLED = OLD_MULTI; |
| 54 | + }); |
| 55 | + |
| 56 | + it('emits app:seeded with overBudget=true after a background seed finishes past the budget', async () => { |
| 57 | + process.env.OS_INLINE_SEED_BUDGET_MS = '10'; |
| 58 | + // Insert resolves well after the 10ms budget, so the race yields to the |
| 59 | + // budget and the seed continues in the background. |
| 60 | + insert = vi.fn(() => new Promise((resolve) => setTimeout(resolve, 60))); |
| 61 | + const ctx = makeContext(); |
| 62 | + const plugin = new AppPlugin(bundleWithUser()); |
| 63 | + |
| 64 | + await plugin.start(ctx); |
| 65 | + |
| 66 | + // start() returned once the budget elapsed — the background seed (and |
| 67 | + // thus the settle signal) has NOT fired yet. |
| 68 | + expect(trigger).not.toHaveBeenCalledWith('app:seeded', expect.anything()); |
| 69 | + |
| 70 | + await vi.waitFor(() => { |
| 71 | + expect(trigger).toHaveBeenCalledWith('app:seeded', { |
| 72 | + appId: 'seed-test-app', |
| 73 | + overBudget: true, |
| 74 | + }); |
| 75 | + }); |
| 76 | + // The user was still inserted by the background seed. |
| 77 | + expect(insert).toHaveBeenCalledWith('sys_user', { id: 'seeded_u1' }, expect.anything()); |
| 78 | + }); |
| 79 | + |
| 80 | + it('emits app:seeded with overBudget=false when the seed completes within budget', async () => { |
| 81 | + process.env.OS_INLINE_SEED_BUDGET_MS = '8000'; |
| 82 | + insert = vi.fn(async () => undefined); // resolves immediately |
| 83 | + const ctx = makeContext(); |
| 84 | + const plugin = new AppPlugin(bundleWithUser()); |
| 85 | + |
| 86 | + await plugin.start(ctx); |
| 87 | + |
| 88 | + expect(trigger).toHaveBeenCalledWith('app:seeded', { |
| 89 | + appId: 'seed-test-app', |
| 90 | + overBudget: false, |
| 91 | + }); |
| 92 | + }); |
| 93 | + |
| 94 | + it('does not emit app:seeded when the app has no seed datasets', async () => { |
| 95 | + insert = vi.fn(async () => undefined); |
| 96 | + const ctx = makeContext(); |
| 97 | + const plugin = new AppPlugin({ id: 'seed-test-app' }); |
| 98 | + |
| 99 | + await plugin.start(ctx); |
| 100 | + |
| 101 | + expect(trigger).not.toHaveBeenCalledWith('app:seeded', expect.anything()); |
| 102 | + }); |
| 103 | + |
| 104 | + it('does not throw when the kernel context has no trigger() function', async () => { |
| 105 | + process.env.OS_INLINE_SEED_BUDGET_MS = '8000'; |
| 106 | + insert = vi.fn(async () => undefined); |
| 107 | + const ctx = makeContext({ trigger: undefined as any }); |
| 108 | + const plugin = new AppPlugin(bundleWithUser()); |
| 109 | + |
| 110 | + await expect(plugin.start(ctx)).resolves.toBeUndefined(); |
| 111 | + expect(insert).toHaveBeenCalledWith('sys_user', { id: 'seeded_u1' }, expect.anything()); |
| 112 | + }); |
| 113 | + |
| 114 | + it('does not run the inline seed (or emit) in multi-tenant mode', async () => { |
| 115 | + process.env.OS_MULTI_ORG_ENABLED = 'true'; |
| 116 | + insert = vi.fn(async () => undefined); |
| 117 | + const ctx = makeContext(); |
| 118 | + const plugin = new AppPlugin(bundleWithUser()); |
| 119 | + |
| 120 | + await plugin.start(ctx); |
| 121 | + |
| 122 | + expect(insert).not.toHaveBeenCalled(); |
| 123 | + expect(trigger).not.toHaveBeenCalledWith('app:seeded', expect.anything()); |
| 124 | + }); |
| 125 | +}); |
0 commit comments