|
1 | | -import { describe, expect, it } from 'vitest' |
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { resetEnvMock, setEnv } from '@sim/testing/mocks/env.mock' |
| 5 | +import { afterAll, afterEach, describe, expect, it, vi } from 'vitest' |
| 6 | + |
| 7 | +vi.hoisted(() => { |
| 8 | + vi.stubEnv('NODE_ENV', 'production') |
| 9 | +}) |
| 10 | + |
| 11 | +vi.unmock('@/lib/core/config/env-flags') |
| 12 | + |
2 | 13 | import { createSandboxPricing, priceSandboxUsage } from '@/lib/billing/sandbox-pricing' |
3 | 14 |
|
| 15 | +afterEach(resetEnvMock) |
| 16 | +afterAll(() => vi.unstubAllEnvs()) |
| 17 | + |
4 | 18 | describe('sandbox pricing', () => { |
5 | 19 | it.each([ |
6 | 20 | ['e2b', 0.1656], |
@@ -33,4 +47,32 @@ describe('sandbox pricing', () => { |
33 | 47 | 'finite nonnegative' |
34 | 48 | ) |
35 | 49 | }) |
| 50 | + |
| 51 | + describe('default multiplier from the production environment', () => { |
| 52 | + it('coerces the string COST_MULTIPLIER that process.env delivers', () => { |
| 53 | + setEnv({ COST_MULTIPLIER: '1.1' }) |
| 54 | + |
| 55 | + const pricing = createSandboxPricing('e2b') |
| 56 | + |
| 57 | + expect(pricing.multiplier).toBe(1.1) |
| 58 | + expect(priceSandboxUsage(pricing, 1000, 1000).billedCost).toBeCloseTo(0.0000506, 8) |
| 59 | + }) |
| 60 | + |
| 61 | + it('falls back to 1 when COST_MULTIPLIER is unset', () => { |
| 62 | + setEnv({ COST_MULTIPLIER: undefined }) |
| 63 | + |
| 64 | + expect(createSandboxPricing('daytona').multiplier).toBe(1) |
| 65 | + }) |
| 66 | + |
| 67 | + it('falls back to 1 instead of throwing when COST_MULTIPLIER is not a nonnegative number', () => { |
| 68 | + setEnv({ COST_MULTIPLIER: 'abc' }) |
| 69 | + expect(createSandboxPricing('e2b').multiplier).toBe(1) |
| 70 | + |
| 71 | + setEnv({ COST_MULTIPLIER: '-2' }) |
| 72 | + expect(createSandboxPricing('e2b').multiplier).toBe(1) |
| 73 | + |
| 74 | + setEnv({ COST_MULTIPLIER: ' ' }) |
| 75 | + expect(createSandboxPricing('e2b').multiplier).toBe(1) |
| 76 | + }) |
| 77 | + }) |
36 | 78 | }) |
0 commit comments