|
| 1 | +import test from 'tape'; |
| 2 | +import path from 'node:path'; |
| 3 | +import Worker from '../src/worker.js'; |
| 4 | +import fs from 'node:fs'; |
| 5 | +import fsp from 'node:fs/promises'; |
| 6 | +import Sinon from 'sinon'; |
| 7 | +import { |
| 8 | + S3Client, |
| 9 | + GetObjectCommand, |
| 10 | + PutObjectCommand, |
| 11 | +} from '@aws-sdk/client-s3'; |
| 12 | +import { MockAgent, setGlobalDispatcher, getGlobalDispatcher } from 'undici'; |
| 13 | + |
| 14 | +for (const fixturename of await fsp.readdir(new URL('./fixtures/transform-raster/', import.meta.url))) { |
| 15 | + const { ext } = path.parse(fixturename); |
| 16 | + |
| 17 | + test(`Worker Data Transform Raster: ${fixturename}`, async (t) => { |
| 18 | + let id: string; |
| 19 | + |
| 20 | + const mockAgent = new MockAgent(); |
| 21 | + const originalDispatcher = getGlobalDispatcher(); |
| 22 | + mockAgent.disableNetConnect(); |
| 23 | + setGlobalDispatcher(mockAgent); |
| 24 | + const mockPool = mockAgent.get('http://localhost:5001'); |
| 25 | + |
| 26 | + mockPool.intercept({ |
| 27 | + path: /profile\/asset/, |
| 28 | + method: 'POST' |
| 29 | + }).reply((req) => { |
| 30 | + const body = JSON.parse(req.body) as { |
| 31 | + id: string |
| 32 | + }; |
| 33 | + |
| 34 | + id = body.id; |
| 35 | + |
| 36 | + return { |
| 37 | + statusCode: 200, |
| 38 | + data: JSON.stringify({ |
| 39 | + id: body.id |
| 40 | + }) |
| 41 | + }; |
| 42 | + }); |
| 43 | + |
| 44 | + const ExternalOperations = [ |
| 45 | + (command) => { |
| 46 | + t.ok(command instanceof GetObjectCommand); |
| 47 | + t.deepEquals(command.input, { |
| 48 | + Bucket: 'test-bucket', |
| 49 | + Key: `import/ba58a298-a3fe-46b4-a29a-9dd33fbb2139${ext}` |
| 50 | + }); |
| 51 | + |
| 52 | + return Promise.resolve({ |
| 53 | + Body: fs.createReadStream(new URL(`./fixtures/transform-raster/${fixturename}`, import.meta.url)) |
| 54 | + }) |
| 55 | + }, |
| 56 | + (command) => { |
| 57 | + t.ok(command instanceof PutObjectCommand); |
| 58 | + |
| 59 | + t.equals(command.input.Bucket, 'test-bucket') |
| 60 | + t.ok(command.input.Key.startsWith(`profile/admin@example.com/`)) |
| 61 | + t.ok(command.input.Key.endsWith(ext)) |
| 62 | + |
| 63 | + return Promise.resolve({}); |
| 64 | + }, |
| 65 | + (command) => { |
| 66 | + t.ok(command instanceof PutObjectCommand); |
| 67 | + |
| 68 | + t.equals(command.input.Bucket, 'test-bucket') |
| 69 | + t.equals(command.input.Key, `profile/admin@example.com/${id}.pmtiles`); |
| 70 | + |
| 71 | + return Promise.resolve({}); |
| 72 | + }, |
| 73 | + ].reverse(); |
| 74 | + |
| 75 | + Sinon.stub(S3Client.prototype, 'send').callsFake((command) => { |
| 76 | + return ExternalOperations.pop()(command); |
| 77 | + }); |
| 78 | + |
| 79 | + const worker = new Worker({ |
| 80 | + api: 'http://localhost:5001', |
| 81 | + secret: 'coe-wildland-fire', |
| 82 | + bucket: 'test-bucket', |
| 83 | + job: { |
| 84 | + id: 'ba58a298-a3fe-46b4-a29a-9dd33fbb2139', |
| 85 | + created: '2025-08-25T18:08:21.563Z', |
| 86 | + updated: '2025-08-25T18:08:21.563Z', |
| 87 | + status: 'Running', |
| 88 | + error: null, |
| 89 | + result: {}, |
| 90 | + name: `import${ext}`, |
| 91 | + username: 'admin@example.com', |
| 92 | + source: 'Upload', |
| 93 | + config: {}, |
| 94 | + source_id: null, |
| 95 | + } |
| 96 | + }); |
| 97 | + |
| 98 | + worker.on('error', (err) => { |
| 99 | + t.error(err); |
| 100 | + }); |
| 101 | + |
| 102 | + worker.on('success', () => { |
| 103 | + Sinon.restore(); |
| 104 | + setGlobalDispatcher(originalDispatcher); |
| 105 | + mockAgent.close(); |
| 106 | + t.end() |
| 107 | + }); |
| 108 | + |
| 109 | + await worker.process() |
| 110 | + }); |
| 111 | +} |
0 commit comments