|
| 1 | +/** |
| 2 | + * Tests for the EFS seeder Lambda handler. |
| 3 | + * |
| 4 | + * The handler receives a list of file seeds and writes them to the EFS |
| 5 | + * access point mounted at /mnt/efs, resolving in-container paths by stripping |
| 6 | + * the first volume's container_path prefix. |
| 7 | + */ |
| 8 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 9 | + |
| 10 | +const mkdirSyncMock = vi.fn(); |
| 11 | +const writeFileSyncMock = vi.fn(); |
| 12 | + |
| 13 | +vi.mock('fs', () => ({ |
| 14 | + mkdirSync: (...args: unknown[]) => mkdirSyncMock(...args), |
| 15 | + writeFileSync: (...args: unknown[]) => writeFileSyncMock(...args), |
| 16 | +})); |
| 17 | + |
| 18 | +/** Import after mocks are registered so the handler uses the mocked fs. */ |
| 19 | +const { handler } = await import('./handler.js'); |
| 20 | + |
| 21 | +const GAME = 'palworld'; |
| 22 | +const CONTAINER_PATH = '/palworld'; |
| 23 | + |
| 24 | +describe('efs-seeder handler', () => { |
| 25 | + beforeEach(() => { |
| 26 | + vi.clearAllMocks(); |
| 27 | + }); |
| 28 | + |
| 29 | + afterEach(() => { |
| 30 | + vi.restoreAllMocks(); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should write a UTF-8 text seed to the correct EFS path', async () => { |
| 34 | + await handler({ |
| 35 | + game: GAME, |
| 36 | + seeds: [{ path: '/palworld/Pal/Saved/Config/settings.ini', content: '[Settings]\nkey=value' }], |
| 37 | + container_path: CONTAINER_PATH, |
| 38 | + }); |
| 39 | + |
| 40 | + expect(mkdirSyncMock).toHaveBeenCalledWith( |
| 41 | + '/mnt/efs/Pal/Saved/Config', |
| 42 | + { recursive: true }, |
| 43 | + ); |
| 44 | + expect(writeFileSyncMock).toHaveBeenCalledWith( |
| 45 | + '/mnt/efs/Pal/Saved/Config/settings.ini', |
| 46 | + Buffer.from('[Settings]\nkey=value', 'utf8'), |
| 47 | + { flag: 'w', mode: 0o644 }, |
| 48 | + ); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should write a binary seed decoded from base64', async () => { |
| 52 | + const binaryContent = Buffer.from([0x50, 0x4b, 0x03, 0x04]); |
| 53 | + const b64 = binaryContent.toString('base64'); |
| 54 | + |
| 55 | + await handler({ |
| 56 | + game: GAME, |
| 57 | + seeds: [{ path: '/palworld/Pal/Content/Paks/MyMod.pak', content_base64: b64 }], |
| 58 | + container_path: CONTAINER_PATH, |
| 59 | + }); |
| 60 | + |
| 61 | + expect(writeFileSyncMock).toHaveBeenCalledWith( |
| 62 | + '/mnt/efs/Pal/Content/Paks/MyMod.pak', |
| 63 | + binaryContent, |
| 64 | + { flag: 'w', mode: 0o644 }, |
| 65 | + ); |
| 66 | + }); |
| 67 | + |
| 68 | + it('should apply a custom mode when provided', async () => { |
| 69 | + await handler({ |
| 70 | + game: GAME, |
| 71 | + seeds: [{ path: '/palworld/server.sh', content: '#!/bin/sh', mode: '0755' }], |
| 72 | + container_path: CONTAINER_PATH, |
| 73 | + }); |
| 74 | + |
| 75 | + expect(writeFileSyncMock).toHaveBeenCalledWith( |
| 76 | + '/mnt/efs/server.sh', |
| 77 | + expect.any(Buffer), |
| 78 | + { flag: 'w', mode: 0o755 }, |
| 79 | + ); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should write multiple seeds in a single invocation', async () => { |
| 83 | + await handler({ |
| 84 | + game: GAME, |
| 85 | + seeds: [ |
| 86 | + { path: '/palworld/a.ini', content: 'a=1' }, |
| 87 | + { path: '/palworld/b.ini', content: 'b=2' }, |
| 88 | + ], |
| 89 | + container_path: CONTAINER_PATH, |
| 90 | + }); |
| 91 | + |
| 92 | + expect(writeFileSyncMock).toHaveBeenCalledTimes(2); |
| 93 | + }); |
| 94 | + |
| 95 | + it('should throw when a seed path does not start with container_path', async () => { |
| 96 | + await expect( |
| 97 | + handler({ |
| 98 | + game: GAME, |
| 99 | + seeds: [{ path: '/other/path/config.ini', content: 'x=1' }], |
| 100 | + container_path: CONTAINER_PATH, |
| 101 | + }), |
| 102 | + ).rejects.toThrow('does not start with container_path'); |
| 103 | + }); |
| 104 | + |
| 105 | + it('should throw when the seed path equals container_path with no file component', async () => { |
| 106 | + await expect( |
| 107 | + handler({ |
| 108 | + game: GAME, |
| 109 | + seeds: [{ path: '/palworld', content: 'x=1' }], |
| 110 | + container_path: CONTAINER_PATH, |
| 111 | + }), |
| 112 | + ).rejects.toThrow('no file component after container_path'); |
| 113 | + }); |
| 114 | + |
| 115 | + it('should throw on path traversal attempts', async () => { |
| 116 | + await expect( |
| 117 | + handler({ |
| 118 | + game: GAME, |
| 119 | + seeds: [{ path: '/palworld/../../../etc/passwd', content: 'evil' }], |
| 120 | + container_path: CONTAINER_PATH, |
| 121 | + }), |
| 122 | + ).rejects.toThrow(); |
| 123 | + }); |
| 124 | + |
| 125 | + it('should throw when a seed has neither content nor content_base64', async () => { |
| 126 | + await expect( |
| 127 | + handler({ |
| 128 | + game: GAME, |
| 129 | + seeds: [{ path: '/palworld/empty.txt' }], |
| 130 | + container_path: CONTAINER_PATH, |
| 131 | + }), |
| 132 | + ).rejects.toThrow('neither content nor content_base64'); |
| 133 | + }); |
| 134 | + |
| 135 | + it('should throw when a seed sets both content and content_base64', async () => { |
| 136 | + await expect( |
| 137 | + handler({ |
| 138 | + game: GAME, |
| 139 | + seeds: [{ path: '/palworld/a.ini', content: 'x=1', content_base64: 'eD0x' }], |
| 140 | + container_path: CONTAINER_PATH, |
| 141 | + }), |
| 142 | + ).rejects.toThrow('sets both content and content_base64'); |
| 143 | + }); |
| 144 | + |
| 145 | + it('should throw when mode is not a valid octal string', async () => { |
| 146 | + await expect( |
| 147 | + handler({ |
| 148 | + game: GAME, |
| 149 | + seeds: [{ path: '/palworld/a.ini', content: 'x=1', mode: 'rwxr-xr-x' }], |
| 150 | + container_path: CONTAINER_PATH, |
| 151 | + }), |
| 152 | + ).rejects.toThrow('invalid mode'); |
| 153 | + }); |
| 154 | + |
| 155 | + it('should handle an empty seeds list without errors', async () => { |
| 156 | + await expect( |
| 157 | + handler({ game: GAME, seeds: [], container_path: CONTAINER_PATH }), |
| 158 | + ).resolves.toBeUndefined(); |
| 159 | + |
| 160 | + expect(writeFileSyncMock).not.toHaveBeenCalled(); |
| 161 | + }); |
| 162 | +}); |
0 commit comments