|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | +import { contractTestCloud } from '@profullstack/sh1pt-core/testing'; |
| 3 | +import cloud from './index.js'; |
| 4 | + |
| 5 | +const sampleTypes = { |
| 6 | + data: { |
| 7 | + gpu_1x_a10: { |
| 8 | + instance_type: { |
| 9 | + name: 'gpu_1x_a10', |
| 10 | + description: '1x A10 (24 GB PCIe)', |
| 11 | + gpu_description: 'A10 (24 GB PCIe)', |
| 12 | + price_cents_per_hour: 75, |
| 13 | + specs: { vcpus: 30, memory_gib: 200, storage_gib: 1400, gpus: 1 }, |
| 14 | + }, |
| 15 | + regions_with_capacity_available: [{ name: 'us-west-1', description: 'US West' }], |
| 16 | + }, |
| 17 | + gpu_1x_a100: { |
| 18 | + instance_type: { |
| 19 | + name: 'gpu_1x_a100', |
| 20 | + description: '1x A100 (40 GB PCIe)', |
| 21 | + gpu_description: 'A100 (40 GB PCIe)', |
| 22 | + price_cents_per_hour: 129, |
| 23 | + specs: { vcpus: 30, memory_gib: 200, storage_gib: 1400, gpus: 1 }, |
| 24 | + }, |
| 25 | + regions_with_capacity_available: [{ name: 'us-west-1', description: 'US West' }], |
| 26 | + }, |
| 27 | + }, |
| 28 | +}; |
| 29 | + |
| 30 | +const ctx = { |
| 31 | + secret: (key: string) => key === 'LAMBDA_CLOUD_API_KEY' ? 'test' : undefined, |
| 32 | + log: vi.fn(), |
| 33 | + dryRun: false, |
| 34 | +}; |
| 35 | + |
| 36 | +beforeEach(() => { |
| 37 | + vi.stubGlobal('fetch', vi.fn(async () => new Response(JSON.stringify(sampleTypes), { status: 200 }))); |
| 38 | +}); |
| 39 | + |
| 40 | +afterEach(() => { |
| 41 | + vi.restoreAllMocks(); |
| 42 | +}); |
| 43 | + |
| 44 | +contractTestCloud(cloud, { |
| 45 | + sampleConfig: { sshKeyNames: ['test-key'] }, |
| 46 | + sampleSpec: { kind: 'gpu', gpu: { model: 'A10', count: 1 }, region: 'us-west-1' }, |
| 47 | + requiredSecrets: ['LAMBDA_CLOUD_API_KEY'], |
| 48 | +}); |
| 49 | + |
| 50 | +describe('lambda-labs cloud adapter', () => { |
| 51 | + it('reports API errors with status and message', async () => { |
| 52 | + vi.stubGlobal('fetch', vi.fn(async () => new Response(JSON.stringify({ error: { code: 'auth_failed', message: 'invalid key' } }), { status: 401 }))); |
| 53 | + |
| 54 | + await expect(cloud.connect(ctx, {})).rejects.toThrow('401 auth_failed'); |
| 55 | + }); |
| 56 | + |
| 57 | + it('quotes the cheapest matching GPU type', async () => { |
| 58 | + vi.stubGlobal('fetch', vi.fn(async () => new Response(JSON.stringify(sampleTypes), { status: 200 }))); |
| 59 | + |
| 60 | + const quote = await cloud.quote(ctx, { kind: 'gpu', gpu: { model: 'A10', count: 1 }, region: 'us-west-1' }, {}); |
| 61 | + |
| 62 | + expect(quote).toMatchObject({ |
| 63 | + hourly: 0.75, |
| 64 | + monthly: 547.5, |
| 65 | + currency: 'USD', |
| 66 | + provider: 'lambda-labs', |
| 67 | + sku: 'gpu_1x_a10', |
| 68 | + }); |
| 69 | + }); |
| 70 | + |
| 71 | + it('does not match short GPU names inside larger model names', async () => { |
| 72 | + vi.stubGlobal('fetch', vi.fn(async () => new Response(JSON.stringify({ |
| 73 | + data: { gpu_1x_a100: sampleTypes.data.gpu_1x_a100 }, |
| 74 | + }), { status: 200 }))); |
| 75 | + |
| 76 | + const quote = await cloud.quote(ctx, { kind: 'gpu', gpu: { model: 'A10', count: 1 }, region: 'us-west-1' }, {}); |
| 77 | + |
| 78 | + expect(quote.sku).toBe('none'); |
| 79 | + expect(quote.hourly).toBe(0); |
| 80 | + }); |
| 81 | + |
| 82 | + it('dry-run provision never calls fetch', async () => { |
| 83 | + const fetchMock = vi.fn(); |
| 84 | + vi.stubGlobal('fetch', fetchMock); |
| 85 | + |
| 86 | + const instance = await cloud.provision({ ...ctx, dryRun: true }, { kind: 'gpu', gpu: { model: 'A10', count: 1 } }, {}); |
| 87 | + |
| 88 | + expect(fetchMock).not.toHaveBeenCalled(); |
| 89 | + expect(instance).toMatchObject({ id: 'dry-run', kind: 'gpu', status: 'provisioning' }); |
| 90 | + }); |
| 91 | + |
| 92 | + it('requires an SSH key name before billable launch', async () => { |
| 93 | + vi.stubGlobal('fetch', vi.fn(async () => new Response(JSON.stringify(sampleTypes), { status: 200 }))); |
| 94 | + |
| 95 | + await expect(cloud.provision(ctx, { kind: 'gpu', gpu: { model: 'A10', count: 1 } }, {})).rejects.toThrow('SSH key name'); |
| 96 | + }); |
| 97 | + |
| 98 | + it('rejects multiple SSH key names instead of silently truncating them', async () => { |
| 99 | + vi.stubGlobal('fetch', vi.fn(async () => new Response(JSON.stringify(sampleTypes), { status: 200 }))); |
| 100 | + |
| 101 | + await expect(cloud.provision( |
| 102 | + ctx, |
| 103 | + { kind: 'gpu', gpu: { model: 'A10', count: 1 } }, |
| 104 | + { sshKeyNames: ['key-one', 'key-two'] }, |
| 105 | + )).rejects.toThrow('exactly one SSH key name'); |
| 106 | + }); |
| 107 | + |
| 108 | + it('launches with the wrapped Lambda response shape', async () => { |
| 109 | + const fetchMock = vi.fn(async (url: string, _init?: RequestInit) => { |
| 110 | + if (url.endsWith('/instance-types')) { |
| 111 | + return new Response(JSON.stringify(sampleTypes), { status: 200 }); |
| 112 | + } |
| 113 | + return new Response(JSON.stringify({ data: { instance_ids: ['0920582c'] } }), { status: 200 }); |
| 114 | + }); |
| 115 | + vi.stubGlobal('fetch', fetchMock); |
| 116 | + |
| 117 | + const instance = await cloud.provision( |
| 118 | + ctx, |
| 119 | + { kind: 'gpu', gpu: { model: 'A10', count: 1 }, region: 'us-west-1', maxHourlyPrice: 1, tags: ['team:infra', 'sh1pt'] }, |
| 120 | + { sshKeyNames: ['default-key'], tags: { app: 'sh1pt' } }, |
| 121 | + ); |
| 122 | + |
| 123 | + expect(instance).toMatchObject({ |
| 124 | + id: '0920582c', |
| 125 | + kind: 'gpu', |
| 126 | + status: 'provisioning', |
| 127 | + hourlyRate: 0.75, |
| 128 | + sku: 'gpu_1x_a10', |
| 129 | + }); |
| 130 | + expect(fetchMock).toHaveBeenLastCalledWith( |
| 131 | + 'https://cloud.lambda.ai/api/v1/instance-operations/launch', |
| 132 | + expect.objectContaining({ |
| 133 | + method: 'POST', |
| 134 | + body: expect.stringContaining('"ssh_key_names":["default-key"]'), |
| 135 | + }), |
| 136 | + ); |
| 137 | + const request = fetchMock.mock.calls.at(-1)?.[1] as RequestInit; |
| 138 | + expect(JSON.parse(String(request.body))).toMatchObject({ |
| 139 | + tags: [ |
| 140 | + { key: 'app', value: 'sh1pt' }, |
| 141 | + { key: 'team', value: 'infra' }, |
| 142 | + { key: 'tag-2', value: 'sh1pt' }, |
| 143 | + ], |
| 144 | + }); |
| 145 | + }); |
| 146 | + |
| 147 | + it('throws when launch succeeds without an instance id', async () => { |
| 148 | + const fetchMock = vi.fn(async (url: string) => { |
| 149 | + if (url.endsWith('/instance-types')) { |
| 150 | + return new Response(JSON.stringify(sampleTypes), { status: 200 }); |
| 151 | + } |
| 152 | + return new Response(JSON.stringify({ data: { instance_ids: [] } }), { status: 200 }); |
| 153 | + }); |
| 154 | + vi.stubGlobal('fetch', fetchMock); |
| 155 | + |
| 156 | + await expect(cloud.provision( |
| 157 | + ctx, |
| 158 | + { kind: 'gpu', gpu: { model: 'A10', count: 1 }, region: 'us-west-1', maxHourlyPrice: 1 }, |
| 159 | + { sshKeyNames: ['default-key'] }, |
| 160 | + )).rejects.toThrow('returned no instance ID'); |
| 161 | + }); |
| 162 | + |
| 163 | + it('preserves Lambda instance timestamp fields in status responses', async () => { |
| 164 | + vi.stubGlobal('fetch', vi.fn(async () => new Response(JSON.stringify({ |
| 165 | + data: { |
| 166 | + id: '0920582c', |
| 167 | + status: 'active', |
| 168 | + ip: '198.51.100.2', |
| 169 | + private_ip: '10.0.2.100', |
| 170 | + created_at: '2026-06-13T19:30:00Z', |
| 171 | + ssh_key_names: ['default-key'], |
| 172 | + file_system_names: [], |
| 173 | + region: { name: 'us-west-1', description: 'US West' }, |
| 174 | + instance_type: sampleTypes.data.gpu_1x_a10.instance_type, |
| 175 | + actions: {}, |
| 176 | + tags: [{ key: 'team', value: 'infra' }], |
| 177 | + }, |
| 178 | + }), { status: 200 }))); |
| 179 | + |
| 180 | + const instance = await cloud.status(ctx, '0920582c', {}); |
| 181 | + |
| 182 | + expect(instance).toMatchObject({ |
| 183 | + id: '0920582c', |
| 184 | + status: 'running', |
| 185 | + createdAt: '2026-06-13T19:30:00Z', |
| 186 | + publicIp: '198.51.100.2', |
| 187 | + privateIp: '10.0.2.100', |
| 188 | + tags: ['team:infra'], |
| 189 | + }); |
| 190 | + }); |
| 191 | + |
| 192 | + it('uses a stable unknown timestamp when Lambda omits creation time', async () => { |
| 193 | + vi.stubGlobal('fetch', vi.fn(async () => new Response(JSON.stringify({ |
| 194 | + data: { |
| 195 | + id: 'terminating-id', |
| 196 | + status: 'terminating', |
| 197 | + ssh_key_names: ['default-key'], |
| 198 | + file_system_names: [], |
| 199 | + region: { name: 'us-west-1', description: 'US West' }, |
| 200 | + instance_type: sampleTypes.data.gpu_1x_a10.instance_type, |
| 201 | + actions: {}, |
| 202 | + }, |
| 203 | + }), { status: 200 }))); |
| 204 | + |
| 205 | + const instance = await cloud.status(ctx, 'terminating-id', {}); |
| 206 | + |
| 207 | + expect(instance).toMatchObject({ |
| 208 | + id: 'terminating-id', |
| 209 | + status: 'stopped', |
| 210 | + createdAt: '1970-01-01T00:00:00.000Z', |
| 211 | + }); |
| 212 | + }); |
| 213 | +}); |
0 commit comments