Skip to content

Commit

Permalink
refactor: fixup tests
Browse files Browse the repository at this point in the history
  • Loading branch information
blacha committed Dec 16, 2024
1 parent 8ba3e75 commit bce84b8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
25 changes: 19 additions & 6 deletions packages/lambda-analytic-cloudfront/src/__test__/analytics.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import assert from 'node:assert';
import { beforeEach, describe, it, TestContext } from 'node:test';
import { afterEach, beforeEach, describe, it, TestContext } from 'node:test';
import { gzipSync } from 'node:zlib';

import { Env, fsa, FsMemory } from '@basemaps/shared';
import { Env, fsa, FsMemory, LogConfig } from '@basemaps/shared';
import { Client } from '@elastic/elasticsearch';
import { LambdaRequest } from '@linzjs/lambda';
import { Context } from 'aws-lambda';

import { getYesterday } from '../date.js';
import { Elastic } from '../elastic.js';
Expand All @@ -16,15 +18,26 @@ interface IndexOperation {
}
type BulkOperation = (IndexOperation | LogStats)[];

export class FakeLambdaRequest extends LambdaRequest {
constructor() {
super({}, {} as Context, LogConfig.get());
}
}

describe('analytic lambda', () => {
const memory = new FsMemory();
beforeEach(() => {
fsa.register('mem://', memory);
memory.files.clear();

Elastic.indexDelay = 1; // do not wait between requests
Elastic.minRequestCount = 1; // index everything
Elastic.minRequestCount = 0; // index everything
Elastic._client = undefined;
LogConfig.get().level = 'silent';
});

afterEach(() => {
LogConfig.get().level = 'info';
});

function setupEnv(t: TestContext): void {
Expand All @@ -37,7 +50,7 @@ describe('analytic lambda', () => {
case Env.Analytics.CloudFrontId:
return 'cfid';
case Env.Analytics.MaxRecords:
return '30';
return '1';
}
throw new Error(`Invalid test process.env access ${key}`);
});
Expand All @@ -59,7 +72,7 @@ describe('analytic lambda', () => {

await fsa.write(new URL(`mem://source/cfid.${shortDate}/data.txt.gz`), gzipSync(LogData));

await main();
await main(new FakeLambdaRequest());

// One call to insert
assert.equal(operations.length, 1);
Expand Down Expand Up @@ -105,7 +118,7 @@ describe('analytic lambda', () => {

await fsa.write(new URL(`mem://source/cfid.${shortDate}/data.txt.gz`), gzipSync(LogData));

const ret = await main().catch((e: Error) => e);
const ret = await main(new FakeLambdaRequest()).catch((e: Error) => e);

assert.equal(String(ret), 'Error: Failed to index');

Expand Down
2 changes: 1 addition & 1 deletion packages/lambda-analytic-cloudfront/src/handler.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { promisify } from 'node:util';
import { gzip } from 'node:zlib';

import { Env, fsa, LogConfig } from '@basemaps/shared';
import { Env, fsa } from '@basemaps/shared';
import { LambdaRequest } from '@linzjs/lambda';
import pLimit from 'p-limit';
import { basename } from 'path';
Expand Down

0 comments on commit bce84b8

Please sign in to comment.