|
1 |
| -import { describe, expect, it } from 'bun:test' |
| 1 | +import { afterAll, beforeAll, describe, expect, it, mock } from 'bun:test' |
| 2 | +import * as fs from 'node:fs' |
| 3 | +import * as path from 'node:path' |
| 4 | +import { config } from '../src/config' |
| 5 | +import { dynamoDb, runningProcesses } from '../src/dynamodb' |
| 6 | +import { exists } from '../src/utils' |
| 7 | + |
| 8 | +// Mock external dependencies |
| 9 | +mock.module('node:https', () => ({ |
| 10 | + get: mock(() => ({ |
| 11 | + on: mock(() => {}), |
| 12 | + pipe: mock(() => ({ |
| 13 | + pipe: mock(() => ({ |
| 14 | + on: mock(() => {}), |
| 15 | + })), |
| 16 | + })), |
| 17 | + })), |
| 18 | +})) |
2 | 19 |
|
3 | 20 | describe('dynamodb-tooling', () => {
|
4 |
| - it('exported', () => { |
5 |
| - expect(1).toEqual(1) |
| 21 | + describe('Config', () => { |
| 22 | + it('should load default config', () => { |
| 23 | + expect(config).toBeDefined() |
| 24 | + expect(config.port).toBe(8000) |
| 25 | + expect(config.installPath).toBe('dynamodb-local') |
| 26 | + }) |
| 27 | + }) |
| 28 | + |
| 29 | + describe('DynamoDB Local', () => { |
| 30 | + const testPort = 8001 |
| 31 | + let process: any |
| 32 | + const originalInstall = dynamoDb.install |
| 33 | + |
| 34 | + beforeAll(async () => { |
| 35 | + // Ensure the install directory exists for testing |
| 36 | + await fs.promises.mkdir(config.installPath, { recursive: true }) |
| 37 | + // Create a mock JAR file |
| 38 | + await fs.promises.writeFile(path.join(config.installPath, 'DynamoDBLocal.jar'), '') |
| 39 | + |
| 40 | + // Mock the install method |
| 41 | + dynamoDb.install = mock(async () => { |
| 42 | + await fs.promises.writeFile(path.join(config.installPath, 'DynamoDBLocal.jar'), '') |
| 43 | + }) |
| 44 | + }) |
| 45 | + |
| 46 | + afterAll(async () => { |
| 47 | + // Clean up the test install directory |
| 48 | + await fs.promises.rm(config.installPath, { recursive: true, force: true }) |
| 49 | + // Restore the original install method |
| 50 | + dynamoDb.install = originalInstall |
| 51 | + }) |
| 52 | + |
| 53 | + it('should launch DynamoDB Local', async () => { |
| 54 | + process = await dynamoDb.launch({ port: testPort }) |
| 55 | + expect(process).toBeDefined() |
| 56 | + expect(process.pid).toBeDefined() |
| 57 | + }) |
| 58 | + |
| 59 | + it('should not launch a second instance on the same port', async () => { |
| 60 | + const secondProcess = await dynamoDb.launch({ port: testPort }) |
| 61 | + expect(secondProcess).toBe(process) |
| 62 | + }) |
| 63 | + |
| 64 | + it('should stop DynamoDB Local', () => { |
| 65 | + dynamoDb.stop(testPort) |
| 66 | + expect(runningProcesses[testPort]).toBeUndefined() |
| 67 | + }) |
| 68 | + |
| 69 | + it('should install DynamoDB Local', async () => { |
| 70 | + // Remove the mock JAR file to test installation |
| 71 | + await fs.promises.unlink(path.join(config.installPath, 'DynamoDBLocal.jar')) |
| 72 | + |
| 73 | + await dynamoDb.install() |
| 74 | + const jarExists = await exists(path.join(config.installPath, 'DynamoDBLocal.jar')) |
| 75 | + expect(jarExists).toBe(true) |
| 76 | + expect(dynamoDb.install).toHaveBeenCalled() |
| 77 | + }) |
| 78 | + }) |
| 79 | + |
| 80 | + describe('Utils', () => { |
| 81 | + const testDir = 'test-utils-dir' |
| 82 | + |
| 83 | + beforeAll(async () => { |
| 84 | + await fs.promises.mkdir(testDir, { recursive: true }) |
| 85 | + }) |
| 86 | + |
| 87 | + afterAll(async () => { |
| 88 | + await fs.promises.rm(testDir, { recursive: true, force: true }) |
| 89 | + }) |
| 90 | + |
| 91 | + it('should check if a file exists', async () => { |
| 92 | + const existingFile = path.join(testDir, 'test-file.txt') |
| 93 | + await fs.promises.writeFile(existingFile, '') |
| 94 | + |
| 95 | + expect(await exists(existingFile)).toBe(true) |
| 96 | + expect(await exists(path.join(testDir, 'non-existent-file.txt'))).toBe(false) |
| 97 | + |
| 98 | + await fs.promises.unlink(existingFile) |
| 99 | + }) |
6 | 100 | })
|
7 | 101 | })
|
0 commit comments