|
| 1 | +import * as fs from 'fs' |
| 2 | +import * as path from 'path' |
| 3 | + |
| 4 | +import * as pulumi from '@pulumi/pulumi' |
| 5 | + |
| 6 | +import { MyMocks, getTempDir, promiseOf } from './utils' |
| 7 | +import * as resources from '../stacks/server/resources' |
| 8 | + |
| 9 | +vi.mock('../stacks/server/resources') |
| 10 | + |
| 11 | +describe('stacks/server/index.ts', () => { |
| 12 | + let envOrig: string |
| 13 | + let mocks: MyMocks |
| 14 | + let infra: typeof import('../stacks/server') |
| 15 | + |
| 16 | + beforeEach(async () => { |
| 17 | + vi.resetModules() |
| 18 | + envOrig = JSON.stringify(process.env) |
| 19 | + mocks = new MyMocks() |
| 20 | + pulumi.runtime.setMocks(mocks) |
| 21 | + }) |
| 22 | + |
| 23 | + afterEach(() => { |
| 24 | + process.env = JSON.parse(envOrig) |
| 25 | + vi.resetAllMocks() |
| 26 | + }) |
| 27 | + |
| 28 | + it('main', async () => { |
| 29 | + const tmpDir = getTempDir() |
| 30 | + const envPath = path.join(tmpDir, '.env') |
| 31 | + const envContent = 'MOCK=\n' |
| 32 | + fs.writeFileSync(envPath, envContent) |
| 33 | + ;(resources.getLambdaRole as any).mockImplementation(() => { |
| 34 | + return 'mock' |
| 35 | + }) |
| 36 | + |
| 37 | + const mockBuildLambda = resources.buildLambda as any |
| 38 | + mockBuildLambda.mockImplementation(() => { |
| 39 | + return { |
| 40 | + functionArn: pulumi.interpolate`arn`, |
| 41 | + functionUrl: pulumi.interpolate`https://www.example.com/`, |
| 42 | + } |
| 43 | + }) |
| 44 | + |
| 45 | + // @ts-ignore |
| 46 | + pulumi.Config = vi.fn(() => { |
| 47 | + return { |
| 48 | + get: vi.fn((x) => { |
| 49 | + if (x === 'projectPath') { |
| 50 | + return tmpDir |
| 51 | + } |
| 52 | + if (x === 'allowedOrigins') { |
| 53 | + return '[example.com]' |
| 54 | + } |
| 55 | + if (x === 'memorySize') { |
| 56 | + return '256' |
| 57 | + } |
| 58 | + return '' |
| 59 | + }), |
| 60 | + } |
| 61 | + }) |
| 62 | + |
| 63 | + infra = await import('../stacks/server') |
| 64 | + |
| 65 | + expect(resources.getLambdaRole).toHaveBeenCalledTimes(1) |
| 66 | + expect(resources.buildLambda).toHaveBeenCalledTimes(2) |
| 67 | + |
| 68 | + expect(mockBuildLambda.calls[0][3]).toStrictEqual({ MOCK: '' }) |
| 69 | + expect(mockBuildLambda.calls[0][4]).toStrictEqual(256) |
| 70 | + expect(mockBuildLambda.calls[1][3]).toStrictEqual({ |
| 71 | + ALLOWED_ORIGINS: '[example.com]', |
| 72 | + }) |
| 73 | + |
| 74 | + const serverArn = await promiseOf(infra.serverArn) |
| 75 | + const optionsArn = await promiseOf(infra.optionsArn) |
| 76 | + |
| 77 | + expect(serverArn).toMatch('arn') |
| 78 | + expect(optionsArn).toMatch('arn') |
| 79 | + |
| 80 | + const serverDomain = await promiseOf(infra.serverDomain) |
| 81 | + const optionsDomain = await promiseOf(infra.optionsDomain) |
| 82 | + |
| 83 | + expect(serverDomain).toMatch('www.example.com') |
| 84 | + expect(optionsDomain).toMatch('www.example.com') |
| 85 | + |
| 86 | + fs.rmSync(tmpDir, { recursive: true }) |
| 87 | + }) |
| 88 | + |
| 89 | + it('main (defaults)', async () => { |
| 90 | + const tmpDir = getTempDir() |
| 91 | + const envPath = path.join(tmpDir, '.env') |
| 92 | + const envContent = 'MOCK=\n' |
| 93 | + fs.writeFileSync(envPath, envContent) |
| 94 | + ;(resources.getLambdaRole as any).mockImplementation(() => { |
| 95 | + return 'mock' |
| 96 | + }) |
| 97 | + |
| 98 | + const mockBuildLambda = resources.buildLambda as any |
| 99 | + mockBuildLambda.mockImplementation(() => { |
| 100 | + return { |
| 101 | + functionArn: pulumi.interpolate`arn`, |
| 102 | + functionUrl: pulumi.interpolate`https://www.example.com/`, |
| 103 | + } |
| 104 | + }) |
| 105 | + |
| 106 | + // @ts-ignore |
| 107 | + pulumi.Config = vi.fn(() => { |
| 108 | + return { |
| 109 | + get: vi.fn((x) => { |
| 110 | + if (x === 'projectPath') { |
| 111 | + return tmpDir |
| 112 | + } |
| 113 | + return '' |
| 114 | + }), |
| 115 | + } |
| 116 | + }) |
| 117 | + |
| 118 | + infra = await import('../stacks/server') |
| 119 | + |
| 120 | + expect(mockBuildLambda.calls[0][4]).toStrictEqual(128) |
| 121 | + expect(mockBuildLambda.calls[1][3]).toStrictEqual({}) |
| 122 | + |
| 123 | + fs.rmSync(tmpDir, { recursive: true }) |
| 124 | + }) |
| 125 | +}) |
0 commit comments