|
| 1 | +import { describe, it, expect, vi } from 'vitest'; |
| 2 | +import { |
| 3 | + isFlexFlavorName, |
| 4 | + getFlexFlavorName, |
| 5 | + getBaseFlavorName, |
| 6 | + getFlexRegionalizedFlavorId, |
| 7 | + getBaseRegionalizedFlavorId, |
| 8 | + hasFlexVariant, |
| 9 | +} from '../flavor.service'; |
| 10 | +import { |
| 11 | + TInstancesCatalog, |
| 12 | + TRegionalizedFlavor, |
| 13 | +} from '@/domain/entities/instancesCatalog'; |
| 14 | +import { mockedInstancesCatalogEntity } from '@/__mocks__/instance/constants'; |
| 15 | + |
| 16 | +vi.mock('@/utils', () => ({ |
| 17 | + getRegionalizedFlavorId: (flavorName: string, regionName: string) => |
| 18 | + `${flavorName}_${regionName}`, |
| 19 | +})); |
| 20 | + |
| 21 | +function createMinimalCatalog( |
| 22 | + regionalizedFlavors: Array<{ |
| 23 | + id: string; |
| 24 | + flavorId: string; |
| 25 | + regionId: string; |
| 26 | + }>, |
| 27 | +): TInstancesCatalog { |
| 28 | + const byId = new Map<string, TRegionalizedFlavor>( |
| 29 | + regionalizedFlavors.map((rf) => [ |
| 30 | + rf.id, |
| 31 | + { |
| 32 | + id: rf.id, |
| 33 | + flavorId: rf.flavorId, |
| 34 | + regionId: rf.regionId, |
| 35 | + hasStock: true, |
| 36 | + quota: 0, |
| 37 | + osTypes: ['linux'], |
| 38 | + tags: null, |
| 39 | + }, |
| 40 | + ]), |
| 41 | + ); |
| 42 | + return { |
| 43 | + ...mockedInstancesCatalogEntity, |
| 44 | + entities: { |
| 45 | + ...mockedInstancesCatalogEntity.entities, |
| 46 | + regionalizedFlavors: { |
| 47 | + byId, |
| 48 | + allIds: regionalizedFlavors.map((rf) => rf.id), |
| 49 | + }, |
| 50 | + }, |
| 51 | + }; |
| 52 | +} |
| 53 | + |
| 54 | +describe('flavor.service – isFlexFlavorName', () => { |
| 55 | + it('returns true when name ends with -flex', () => { |
| 56 | + expect(isFlexFlavorName('b2-7-flex')).toBe(true); |
| 57 | + expect(isFlexFlavorName('d2-2-flex')).toBe(true); |
| 58 | + }); |
| 59 | + it('returns false when name does not end with -flex', () => { |
| 60 | + expect(isFlexFlavorName('b2-7')).toBe(false); |
| 61 | + expect(isFlexFlavorName('d2-2')).toBe(false); |
| 62 | + expect(isFlexFlavorName('flex-b2')).toBe(false); |
| 63 | + }); |
| 64 | +}); |
| 65 | + |
| 66 | +describe('flavor.service – getFlexFlavorName', () => { |
| 67 | + it('appends -flex to base flavor name', () => { |
| 68 | + expect(getFlexFlavorName('b2-7')).toBe('b2-7-flex'); |
| 69 | + expect(getFlexFlavorName('d2-2')).toBe('d2-2-flex'); |
| 70 | + }); |
| 71 | +}); |
| 72 | + |
| 73 | +describe('flavor.service – getBaseFlavorName', () => { |
| 74 | + it('strips -flex suffix when present', () => { |
| 75 | + expect(getBaseFlavorName('b2-7-flex')).toBe('b2-7'); |
| 76 | + expect(getBaseFlavorName('d2-2-flex')).toBe('d2-2'); |
| 77 | + }); |
| 78 | + it('leaves name unchanged when no -flex suffix', () => { |
| 79 | + expect(getBaseFlavorName('b2-7')).toBe('b2-7'); |
| 80 | + }); |
| 81 | +}); |
| 82 | + |
| 83 | +describe('flavor.service – getFlexRegionalizedFlavorId', () => { |
| 84 | + it('returns flex id when flex variant exists in catalog', () => { |
| 85 | + const catalog = createMinimalCatalog([ |
| 86 | + { id: 'b2-7_GRA11', flavorId: 'b2-7', regionId: 'GRA11' }, |
| 87 | + { id: 'b2-7-flex_GRA11', flavorId: 'b2-7-flex', regionId: 'GRA11' }, |
| 88 | + ]); |
| 89 | + expect(getFlexRegionalizedFlavorId(catalog, 'b2-7_GRA11')).toBe( |
| 90 | + 'b2-7-flex_GRA11', |
| 91 | + ); |
| 92 | + }); |
| 93 | + it('returns same id when flavor is already flex', () => { |
| 94 | + const catalog = createMinimalCatalog([ |
| 95 | + { id: 'b2-7-flex_GRA11', flavorId: 'b2-7-flex', regionId: 'GRA11' }, |
| 96 | + ]); |
| 97 | + expect(getFlexRegionalizedFlavorId(catalog, 'b2-7-flex_GRA11')).toBe( |
| 98 | + 'b2-7-flex_GRA11', |
| 99 | + ); |
| 100 | + }); |
| 101 | + it('returns null when regionalized flavor is not in catalog', () => { |
| 102 | + const catalog = createMinimalCatalog([ |
| 103 | + { id: 'b2-7_GRA11', flavorId: 'b2-7', regionId: 'GRA11' }, |
| 104 | + ]); |
| 105 | + expect(getFlexRegionalizedFlavorId(catalog, 'unknown-id')).toBeNull(); |
| 106 | + }); |
| 107 | + it('returns null when flex variant is not in catalog', () => { |
| 108 | + const catalog = createMinimalCatalog([ |
| 109 | + { id: 'b2-7_GRA11', flavorId: 'b2-7', regionId: 'GRA11' }, |
| 110 | + ]); |
| 111 | + expect(getFlexRegionalizedFlavorId(catalog, 'b2-7_GRA11')).toBeNull(); |
| 112 | + }); |
| 113 | +}); |
| 114 | + |
| 115 | +describe('flavor.service – getBaseRegionalizedFlavorId', () => { |
| 116 | + it('returns base id when flex variant exists in catalog', () => { |
| 117 | + const catalog = createMinimalCatalog([ |
| 118 | + { id: 'b2-7_GRA11', flavorId: 'b2-7', regionId: 'GRA11' }, |
| 119 | + { id: 'b2-7-flex_GRA11', flavorId: 'b2-7-flex', regionId: 'GRA11' }, |
| 120 | + ]); |
| 121 | + expect(getBaseRegionalizedFlavorId(catalog, 'b2-7-flex_GRA11')).toBe( |
| 122 | + 'b2-7_GRA11', |
| 123 | + ); |
| 124 | + }); |
| 125 | + it('returns same id when flavor is not flex', () => { |
| 126 | + const catalog = createMinimalCatalog([ |
| 127 | + { id: 'b2-7_GRA11', flavorId: 'b2-7', regionId: 'GRA11' }, |
| 128 | + ]); |
| 129 | + expect(getBaseRegionalizedFlavorId(catalog, 'b2-7_GRA11')).toBe( |
| 130 | + 'b2-7_GRA11', |
| 131 | + ); |
| 132 | + }); |
| 133 | + it('returns null when regionalized flavor is not in catalog', () => { |
| 134 | + const catalog = createMinimalCatalog([ |
| 135 | + { id: 'b2-7_GRA11', flavorId: 'b2-7', regionId: 'GRA11' }, |
| 136 | + ]); |
| 137 | + expect(getBaseRegionalizedFlavorId(catalog, 'unknown-id')).toBeNull(); |
| 138 | + }); |
| 139 | + it('returns null when base variant is not in catalog', () => { |
| 140 | + const catalog = createMinimalCatalog([ |
| 141 | + { id: 'b2-7-flex_GRA11', flavorId: 'b2-7-flex', regionId: 'GRA11' }, |
| 142 | + ]); |
| 143 | + expect(getBaseRegionalizedFlavorId(catalog, 'b2-7-flex_GRA11')).toBeNull(); |
| 144 | + }); |
| 145 | +}); |
| 146 | + |
| 147 | +describe('flavor.service – hasFlexVariant', () => { |
| 148 | + it('returns true when flex variant exists in catalog', () => { |
| 149 | + const catalog = createMinimalCatalog([ |
| 150 | + { id: 'b2-7_GRA11', flavorId: 'b2-7', regionId: 'GRA11' }, |
| 151 | + { id: 'b2-7-flex_GRA11', flavorId: 'b2-7-flex', regionId: 'GRA11' }, |
| 152 | + ]); |
| 153 | + expect(hasFlexVariant(catalog, 'b2-7_GRA11')).toBe(true); |
| 154 | + }); |
| 155 | + it('returns true when regionalized flavor is already flex', () => { |
| 156 | + const catalog = createMinimalCatalog([ |
| 157 | + { id: 'b2-7-flex_GRA11', flavorId: 'b2-7-flex', regionId: 'GRA11' }, |
| 158 | + ]); |
| 159 | + expect(hasFlexVariant(catalog, 'b2-7-flex_GRA11')).toBe(true); |
| 160 | + }); |
| 161 | + it('returns false when catalog is undefined', () => { |
| 162 | + expect(hasFlexVariant(undefined, 'b2-7_GRA11')).toBe(false); |
| 163 | + }); |
| 164 | + it('returns false when regionalizedFlavorId is null', () => { |
| 165 | + const catalog = createMinimalCatalog([ |
| 166 | + { id: 'b2-7_GRA11', flavorId: 'b2-7', regionId: 'GRA11' }, |
| 167 | + ]); |
| 168 | + expect(hasFlexVariant(catalog, null)).toBe(false); |
| 169 | + }); |
| 170 | + it('returns false when flex variant does not exist in catalog', () => { |
| 171 | + const catalog = createMinimalCatalog([ |
| 172 | + { id: 'b2-7_GRA11', flavorId: 'b2-7', regionId: 'GRA11' }, |
| 173 | + ]); |
| 174 | + expect(hasFlexVariant(catalog, 'b2-7_GRA11')).toBe(false); |
| 175 | + }); |
| 176 | +}); |
0 commit comments