import { describe, expect, it } from 'vitest'; import { AgentStatus } from '../types/organization/agent.js'; import { hasEditLifecycleCapability } from './edit-lifecycle-capability.js'; import { DATABASE_LIFECYCLE_TAG_OPERATIONS } from './index.js'; describe('hasEditLifecycleCapability', () => { it('is false when no agents are present', () => { expect(hasEditLifecycleCapability(undefined)).toBe(false); expect(hasEditLifecycleCapability([])).toBe(false); }); it('is false when agents lack databaseLifecycle operations', () => { expect( hasEditLifecycleCapability([ { status: AgentStatus.ACTIVE, tags: { profile: ['dev'] } } ]) ).toBe(false); }); it('is false when the agent registers other operations but not ensure_database', () => { expect( hasEditLifecycleCapability([ { status: AgentStatus.ACTIVE, tags: { [DATABASE_LIFECYCLE_TAG_OPERATIONS]: ['migrate_schema', 'retire_database'], profile: ['dev'] } } ]) ).toBe(false); }); it('is false when ensure_database is registered but the agent serves no profile', () => { expect( hasEditLifecycleCapability([ { status: AgentStatus.ACTIVE, tags: { [DATABASE_LIFECYCLE_TAG_OPERATIONS]: ['ensure_database', 'migrate_schema'] } } ]) ).toBe(false); expect( hasEditLifecycleCapability([ { status: AgentStatus.ACTIVE, tags: { [DATABASE_LIFECYCLE_TAG_OPERATIONS]: ['ensure_database'], profile: [] } } ]) ).toBe(false); }); it('is false when a disconnected agent has lifecycle tags', () => { expect( hasEditLifecycleCapability([ { status: AgentStatus.DISCONNECTED, tags: { [DATABASE_LIFECYCLE_TAG_OPERATIONS]: ['ensure_database'], profile: ['dev'] } } ]) ).toBe(false); }); it('is false when agent status is missing even if tags would otherwise qualify', () => { expect( hasEditLifecycleCapability([ { tags: { [DATABASE_LIFECYCLE_TAG_OPERATIONS]: ['ensure_database'], profile: ['dev'] } } ]) ).toBe(false); }); it('is true when an active agent registered ensure_database for any profile', () => { expect( hasEditLifecycleCapability([ { status: AgentStatus.ACTIVE, tags: { [DATABASE_LIFECYCLE_TAG_OPERATIONS]: ['ensure_database', 'migrate_schema'], profile: ['staging', 'production'] } } ]) ).toBe(true); }); // A production-only lifecycle agent still advertises Native DB. Editing is no // longer a separate lifecycle axis, so there is no `edit:` pair to require — // profile-key resolution stays on the server at ensure/dispatch time. it('is true for an agent serving only production', () => { expect( hasEditLifecycleCapability([ { status: AgentStatus.ACTIVE, tags: { [DATABASE_LIFECYCLE_TAG_OPERATIONS]: ['ensure_database'], profile: ['production'] } } ]) ).toBe(true); }); it('is true when status is the string Active (CLI wire form)', () => { expect( hasEditLifecycleCapability([ { status: 'Active', tags: { [DATABASE_LIFECYCLE_TAG_OPERATIONS]: ['ensure_database'], profile: ['dev'] } } ]) ).toBe(true); }); });