|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import { |
| 3 | + SEEDBOX_TERMINAL_STATUSES, |
| 4 | + canTransitionSeedboxStatus, |
| 5 | + getAllowedSeedboxStatusTargets, |
| 6 | + isSeedboxTerminalStatus, |
| 7 | + requireSeedboxStatusTransition, |
| 8 | + transitionSeedboxResource, |
| 9 | + type SeedboxResource, |
| 10 | +} from './lifecycle'; |
| 11 | + |
| 12 | +function resource(status: SeedboxResource['status']): SeedboxResource { |
| 13 | + return { |
| 14 | + id: 'resource-1', |
| 15 | + userId: 'user-1', |
| 16 | + kind: 'managed', |
| 17 | + provider: 'digitalocean', |
| 18 | + providerRef: 'droplet-1', |
| 19 | + plan: 'starter', |
| 20 | + status, |
| 21 | + host: null, |
| 22 | + createdAt: new Date('2026-06-15T00:00:00.000Z'), |
| 23 | + activatedAt: null, |
| 24 | + terminatedAt: null, |
| 25 | + metadata: {}, |
| 26 | + }; |
| 27 | +} |
| 28 | + |
| 29 | +describe('seedbox lifecycle', () => { |
| 30 | + it('allows the managed seedbox happy path from order to teardown', () => { |
| 31 | + expect(canTransitionSeedboxStatus('pending', 'provisioning')).toBe(true); |
| 32 | + expect(canTransitionSeedboxStatus('provisioning', 'active')).toBe(true); |
| 33 | + expect(canTransitionSeedboxStatus('active', 'suspended')).toBe(true); |
| 34 | + expect(canTransitionSeedboxStatus('suspended', 'active')).toBe(true); |
| 35 | + expect(canTransitionSeedboxStatus('active', 'terminating')).toBe(true); |
| 36 | + expect(canTransitionSeedboxStatus('terminating', 'terminated')).toBe(true); |
| 37 | + }); |
| 38 | + |
| 39 | + it('rejects skipped or backwards lifecycle jumps', () => { |
| 40 | + expect(canTransitionSeedboxStatus('pending', 'active')).toBe(false); |
| 41 | + expect(canTransitionSeedboxStatus('provisioning', 'suspended')).toBe(false); |
| 42 | + expect(canTransitionSeedboxStatus('terminating', 'active')).toBe(false); |
| 43 | + }); |
| 44 | + |
| 45 | + it('keeps terminal states final', () => { |
| 46 | + expect(SEEDBOX_TERMINAL_STATUSES).toEqual(['terminated']); |
| 47 | + expect(isSeedboxTerminalStatus('terminated')).toBe(true); |
| 48 | + expect(getAllowedSeedboxStatusTargets('terminated')).toEqual([]); |
| 49 | + expect(canTransitionSeedboxStatus('terminated', 'active')).toBe(false); |
| 50 | + }); |
| 51 | + |
| 52 | + it('allows failed resources to retry provisioning or proceed to teardown', () => { |
| 53 | + expect(getAllowedSeedboxStatusTargets('failed')).toEqual(['provisioning', 'terminating']); |
| 54 | + expect(canTransitionSeedboxStatus('failed', 'provisioning')).toBe(true); |
| 55 | + expect(canTransitionSeedboxStatus('failed', 'terminating')).toBe(true); |
| 56 | + expect(canTransitionSeedboxStatus('failed', 'active')).toBe(false); |
| 57 | + }); |
| 58 | + |
| 59 | + it('throws a descriptive error for invalid transitions', () => { |
| 60 | + expect(() => requireSeedboxStatusTransition('pending', 'active')).toThrow( |
| 61 | + 'Invalid seedbox lifecycle transition: pending -> active' |
| 62 | + ); |
| 63 | + }); |
| 64 | + |
| 65 | + it('stamps activation and termination timestamps when transitioning resources', () => { |
| 66 | + const now = new Date('2026-06-16T12:00:00.000Z'); |
| 67 | + const activated = transitionSeedboxResource(resource('provisioning'), 'active', now); |
| 68 | + const terminated = transitionSeedboxResource(resource('terminating'), 'terminated', now); |
| 69 | + |
| 70 | + expect(activated.status).toBe('active'); |
| 71 | + expect(activated.activatedAt).toEqual(now); |
| 72 | + expect(activated.terminatedAt).toBeNull(); |
| 73 | + expect(terminated.status).toBe('terminated'); |
| 74 | + expect(terminated.terminatedAt).toEqual(now); |
| 75 | + }); |
| 76 | +}); |
0 commit comments