|
| 1 | +import { describe, it, expect, beforeEach, vi, afterEach } from 'vitest'; |
| 2 | +import { TeamsCapabilityService } from './teams-capability.service'; |
| 3 | +import type { ConfigService } from '../config/config.service'; |
| 4 | + |
| 5 | +function createMockConfigService(experimental?: { teamsCoordination?: boolean }): ConfigService { |
| 6 | + return { |
| 7 | + getSettings: vi.fn().mockResolvedValue({ experimental }), |
| 8 | + } as unknown as ConfigService; |
| 9 | +} |
| 10 | + |
| 11 | +describe('TeamsCapabilityService', () => { |
| 12 | + let originalEnv: string | undefined; |
| 13 | + |
| 14 | + beforeEach(() => { |
| 15 | + originalEnv = process.env.CODINGBUDDY_TEAMS_ENABLED; |
| 16 | + delete process.env.CODINGBUDDY_TEAMS_ENABLED; |
| 17 | + }); |
| 18 | + |
| 19 | + afterEach(() => { |
| 20 | + if (originalEnv !== undefined) { |
| 21 | + process.env.CODINGBUDDY_TEAMS_ENABLED = originalEnv; |
| 22 | + } else { |
| 23 | + delete process.env.CODINGBUDDY_TEAMS_ENABLED; |
| 24 | + } |
| 25 | + }); |
| 26 | + |
| 27 | + describe('getStatus', () => { |
| 28 | + describe('default (no config, no env)', () => { |
| 29 | + it('should be disabled by default', async () => { |
| 30 | + const service = new TeamsCapabilityService(createMockConfigService()); |
| 31 | + const status = await service.getStatus(); |
| 32 | + |
| 33 | + expect(status.available).toBe(false); |
| 34 | + expect(status.source).toBe('default'); |
| 35 | + expect(status.reason).toContain('disabled by default'); |
| 36 | + }); |
| 37 | + }); |
| 38 | + |
| 39 | + describe('config-based gating', () => { |
| 40 | + it('should enable when experimental.teamsCoordination is true', async () => { |
| 41 | + const service = new TeamsCapabilityService( |
| 42 | + createMockConfigService({ teamsCoordination: true }), |
| 43 | + ); |
| 44 | + const status = await service.getStatus(); |
| 45 | + |
| 46 | + expect(status.available).toBe(true); |
| 47 | + expect(status.source).toBe('config'); |
| 48 | + expect(status.reason).toContain('Enabled via experimental.teamsCoordination'); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should disable when experimental.teamsCoordination is false', async () => { |
| 52 | + const service = new TeamsCapabilityService( |
| 53 | + createMockConfigService({ teamsCoordination: false }), |
| 54 | + ); |
| 55 | + const status = await service.getStatus(); |
| 56 | + |
| 57 | + expect(status.available).toBe(false); |
| 58 | + expect(status.source).toBe('config'); |
| 59 | + expect(status.reason).toContain('Disabled via experimental.teamsCoordination'); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should fall through to default when experimental exists but teamsCoordination is undefined', async () => { |
| 63 | + const service = new TeamsCapabilityService(createMockConfigService({})); |
| 64 | + const status = await service.getStatus(); |
| 65 | + |
| 66 | + expect(status.available).toBe(false); |
| 67 | + expect(status.source).toBe('default'); |
| 68 | + }); |
| 69 | + }); |
| 70 | + |
| 71 | + describe('environment variable override', () => { |
| 72 | + it('should enable when CODINGBUDDY_TEAMS_ENABLED=true', async () => { |
| 73 | + process.env.CODINGBUDDY_TEAMS_ENABLED = 'true'; |
| 74 | + const service = new TeamsCapabilityService(createMockConfigService()); |
| 75 | + const status = await service.getStatus(); |
| 76 | + |
| 77 | + expect(status.available).toBe(true); |
| 78 | + expect(status.source).toBe('environment'); |
| 79 | + expect(status.reason).toContain('Enabled via CODINGBUDDY_TEAMS_ENABLED'); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should enable when CODINGBUDDY_TEAMS_ENABLED=1', async () => { |
| 83 | + process.env.CODINGBUDDY_TEAMS_ENABLED = '1'; |
| 84 | + const service = new TeamsCapabilityService(createMockConfigService()); |
| 85 | + const status = await service.getStatus(); |
| 86 | + |
| 87 | + expect(status.available).toBe(true); |
| 88 | + expect(status.source).toBe('environment'); |
| 89 | + }); |
| 90 | + |
| 91 | + it('should disable when CODINGBUDDY_TEAMS_ENABLED=false', async () => { |
| 92 | + process.env.CODINGBUDDY_TEAMS_ENABLED = 'false'; |
| 93 | + const service = new TeamsCapabilityService(createMockConfigService()); |
| 94 | + const status = await service.getStatus(); |
| 95 | + |
| 96 | + expect(status.available).toBe(false); |
| 97 | + expect(status.source).toBe('environment'); |
| 98 | + expect(status.reason).toContain('Disabled via CODINGBUDDY_TEAMS_ENABLED'); |
| 99 | + }); |
| 100 | + |
| 101 | + it('should take precedence over config when both are set', async () => { |
| 102 | + process.env.CODINGBUDDY_TEAMS_ENABLED = 'false'; |
| 103 | + const service = new TeamsCapabilityService( |
| 104 | + createMockConfigService({ teamsCoordination: true }), |
| 105 | + ); |
| 106 | + const status = await service.getStatus(); |
| 107 | + |
| 108 | + expect(status.available).toBe(false); |
| 109 | + expect(status.source).toBe('environment'); |
| 110 | + }); |
| 111 | + }); |
| 112 | + }); |
| 113 | + |
| 114 | + describe('isAvailable', () => { |
| 115 | + it('should return true when enabled', async () => { |
| 116 | + const service = new TeamsCapabilityService( |
| 117 | + createMockConfigService({ teamsCoordination: true }), |
| 118 | + ); |
| 119 | + expect(await service.isAvailable()).toBe(true); |
| 120 | + }); |
| 121 | + |
| 122 | + it('should return false when disabled', async () => { |
| 123 | + const service = new TeamsCapabilityService(createMockConfigService()); |
| 124 | + expect(await service.isAvailable()).toBe(false); |
| 125 | + }); |
| 126 | + }); |
| 127 | + |
| 128 | + describe('readEnvFlag', () => { |
| 129 | + it('should return undefined when env var is not set', () => { |
| 130 | + const service = new TeamsCapabilityService(createMockConfigService()); |
| 131 | + expect(service.readEnvFlag()).toBeUndefined(); |
| 132 | + }); |
| 133 | + |
| 134 | + it('should return undefined when env var is empty string', () => { |
| 135 | + process.env.CODINGBUDDY_TEAMS_ENABLED = ''; |
| 136 | + const service = new TeamsCapabilityService(createMockConfigService()); |
| 137 | + expect(service.readEnvFlag()).toBeUndefined(); |
| 138 | + }); |
| 139 | + |
| 140 | + it('should return true for "true"', () => { |
| 141 | + process.env.CODINGBUDDY_TEAMS_ENABLED = 'true'; |
| 142 | + const service = new TeamsCapabilityService(createMockConfigService()); |
| 143 | + expect(service.readEnvFlag()).toBe(true); |
| 144 | + }); |
| 145 | + |
| 146 | + it('should return true for "1"', () => { |
| 147 | + process.env.CODINGBUDDY_TEAMS_ENABLED = '1'; |
| 148 | + const service = new TeamsCapabilityService(createMockConfigService()); |
| 149 | + expect(service.readEnvFlag()).toBe(true); |
| 150 | + }); |
| 151 | + |
| 152 | + it('should return false for any other value', () => { |
| 153 | + process.env.CODINGBUDDY_TEAMS_ENABLED = 'no'; |
| 154 | + const service = new TeamsCapabilityService(createMockConfigService()); |
| 155 | + expect(service.readEnvFlag()).toBe(false); |
| 156 | + }); |
| 157 | + }); |
| 158 | + |
| 159 | + describe('status object shape', () => { |
| 160 | + it('should have readonly-compatible properties', async () => { |
| 161 | + const service = new TeamsCapabilityService(createMockConfigService()); |
| 162 | + const status = await service.getStatus(); |
| 163 | + |
| 164 | + expect(status).toHaveProperty('available'); |
| 165 | + expect(status).toHaveProperty('reason'); |
| 166 | + expect(status).toHaveProperty('source'); |
| 167 | + expect(typeof status.available).toBe('boolean'); |
| 168 | + expect(typeof status.reason).toBe('string'); |
| 169 | + expect(['config', 'environment', 'default']).toContain(status.source); |
| 170 | + }); |
| 171 | + }); |
| 172 | +}); |
0 commit comments