import { describe, expect, test } from 'vitest'; import type { PhysicalDatabaseCapacityPolicy } from './capacityPolicy.js'; import { PhysicalPoolIdentityError, parsePhysicalPoolAgentNameTag } from './physicalPoolIdentity.js'; const policy = (agentName: string): PhysicalDatabaseCapacityPolicy => ({ agentName, engine: 'postgres', thresholdType: 'minAvailableCapacityPercent', thresholdValue: 20 }); describe('parsePhysicalPoolAgentNameTag', () => { test('reads the pool a deployment named', () => { expect(parsePhysicalPoolAgentNameTag(['customer-opa'])).toBe('customer-opa'); expect(parsePhysicalPoolAgentNameTag([' customer-opa '])).toBe('customer-opa'); }); test('reports no identity for a deployment that named no pool', () => { expect(parsePhysicalPoolAgentNameTag(undefined)).toBeUndefined(); expect(parsePhysicalPoolAgentNameTag([])).toBeUndefined(); }); test('rejects a tag that is not one non-empty name', () => { expect(() => parsePhysicalPoolAgentNameTag([''])).toThrow(PhysicalPoolIdentityError); expect(() => parsePhysicalPoolAgentNameTag([' '])).toThrow(PhysicalPoolIdentityError); expect(() => parsePhysicalPoolAgentNameTag(['customer-opa', 'other-opa'])).toThrow(/must name exactly one/); }); // Replicas of one deployment publish the same registration. test('accepts a name repeated across replicas', () => { expect(parsePhysicalPoolAgentNameTag(['customer-opa', 'customer-opa'])).toBe('customer-opa'); }); describe('agreement with capacity policies', () => { test('accepts a policy for the pool the deployment named', () => { expect(parsePhysicalPoolAgentNameTag(['customer-opa'], [policy('customer-opa')])).toBe('customer-opa'); }); test('rejects a policy for a pool the deployment does not own', () => { expect(() => parsePhysicalPoolAgentNameTag(['customer-opa'], [policy('other-opa')])).toThrow(/must name the same pool/); }); // A deployment scales the pool it owns and nothing else. Accepting a // foreign policy alongside a matching one would let proactive evaluation // key off a pool the wizard would never resolve to. test('rejects a foreign policy published alongside the deployments own', () => { expect(() => parsePhysicalPoolAgentNameTag(['customer-opa'], [policy('customer-opa'), policy('other-opa')])).toThrow( /must name the same pool/ ); }); // Older workers publish identity only inside the policy. test('falls back to the policy name when the deployment published no tag', () => { expect(parsePhysicalPoolAgentNameTag(undefined, [policy('customer-opa')])).toBe('customer-opa'); }); test('refuses to guess when policies alone name two pools', () => { expect(() => parsePhysicalPoolAgentNameTag(undefined, [policy('customer-opa'), policy('other-opa')])).toThrow( /publish databaseLifecycle:agentName/ ); }); }); });