Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions .nycrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
"statements": 80,
"functions": 80,
"branches": 80,
"include": [],
"include": [
"lib/**/*.ts",
"lib/**/*.js",
"wrappers/**/*.js",
"index.js"
],
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing TypeScript test file exclusion pattern

Low Severity

The exclude pattern only includes **/*.utest.js but not **/*.utest.ts. With all: true enabled and TypeScript extensions configured, nyc now processes .ts source files directly. If a .utest.ts file were created in the lib/ directory (matching the include pattern lib/**/*.ts), it wouldn't be excluded since **/*.utest.js only matches JavaScript test files. This could incorrectly inflate coverage metrics.

Fix in Cursor Fix in Web

"exclude": [
"**/*.utest.js",
"**/*.d.ts",
Expand Down Expand Up @@ -38,5 +43,5 @@
".tsx"
],
"cache": true,
"all": false
"all": true
}
174 changes: 174 additions & 0 deletions test/lib.DynamodbFetcher.utest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
import { expect, assert } from "chai";
import sinon from "sinon";
import { DynamodbFetcher } from "../lib/DynamodbFetcher";

describe('lib/DynamodbFetcher.ts', function () {
let fetcher: DynamodbFetcher<any, any>;

beforeEach(function () {
fetcher = new DynamodbFetcher('test-table', (item) => ({ id: item.id }));
});

describe("constructor", function () {
it('should set tableName', function () {
expect(fetcher.tableName).to.equal('test-table');
});

it('should set getDdbKey function', function () {
const item = { id: '123', name: 'test' };
const key = fetcher.getDdbKey(item);
expect(key).to.deep.equal({ id: '123' });
});

it('should initialize batch counter to 0', function () {
expect(fetcher.batch).to.equal(0);
});

it('should set batchOptions if provided', function () {
const options = { maxRecords: 100, parallelLimit: 10 };
const fetcherWithOptions = new DynamodbFetcher('table', (i) => i, options as any);
expect(fetcherWithOptions.batchOptions).to.not.be.undefined;
});

it('should create ddb client', function () {
expect(fetcher.ddb).to.not.be.undefined;
});
});

describe("keyToString", function () {
it('should convert single key to string', function () {
const key = { id: '123' };
const result = fetcher.keyToString(key);
expect(result).to.equal('id:123');
});

it('should convert multiple keys to string sorted alphabetically', function () {
const key = { z: '3', a: '1', m: '2' };
const result = fetcher.keyToString(key);
expect(result).to.equal('a:1/m:2/z:3');
});

it('should handle numeric values', function () {
const key = { id: 123 };
const result = fetcher.keyToString(key);
expect(result).to.equal('id:123');
});

it('should handle composite keys', function () {
const key = { pk: 'user#123', sk: 'profile' };
const result = fetcher.keyToString(key);
expect(result).to.equal('pk:user#123/sk:profile');
});

it('should handle empty object', function () {
const key = {};
const result = fetcher.keyToString(key);
expect(result).to.equal('');
});
});

describe("join", function () {
let batchGetStub: sinon.SinonStub;

beforeEach(function () {
batchGetStub = sinon.stub(fetcher.ddb, 'batchGet');
});

afterEach(function () {
batchGetStub.restore();
});

it('should return events with joinedData when DDB returns data', async function () {
batchGetStub.resolves({
Responses: {
'test-table': [
{ id: '1', name: 'Item 1' },
{ id: '2', name: 'Item 2' }
]
}
});

const events = [
{ id: '1', value: 'a' },
{ id: '2', value: 'b' }
];

const result = await fetcher.join(events);

expect(result).to.have.length(2);
expect((result[0] as any).joinedData).to.deep.equal({ id: '1', name: 'Item 1' });
expect((result[1] as any).joinedData).to.deep.equal({ id: '2', name: 'Item 2' });
});

it('should handle empty events array', async function () {
const result = await fetcher.join([]);
expect(result).to.deep.equal([]);
expect(batchGetStub.called).to.be.false;
});

it('should handle events with null keys', async function () {
const nullKeyFetcher = new DynamodbFetcher('test-table', (item) => item.id ? { id: item.id } : null);
const batchGetStubNull = sinon.stub(nullKeyFetcher.ddb, 'batchGet');
batchGetStubNull.resolves({
Responses: {
'test-table': [{ id: '1', name: 'Item 1' }]
}
});

const events = [
{ id: '1', value: 'a' },
{ value: 'b' } // no id, key will be null
];

const result = await nullKeyFetcher.join(events);

expect(result).to.have.length(2);
expect((result[0] as any).joinedData).to.not.be.undefined;
expect((result[1] as any).joinedData).to.be.undefined;

batchGetStubNull.restore();
});

it('should deduplicate keys', async function () {
batchGetStub.resolves({
Responses: {
'test-table': [{ id: '1', name: 'Item 1' }]
}
});

const events = [
{ id: '1', value: 'a' },
{ id: '1', value: 'b' } // same id
];

await fetcher.join(events);

// Should only have one key in the batch
const batchParams = batchGetStub.firstCall.args[0];
expect(batchParams.RequestItems['test-table'].Keys).to.have.length(1);
});

it('should increment batch counter', async function () {
batchGetStub.resolves({ Responses: { 'test-table': [] } });

expect(fetcher.batch).to.equal(0);
await fetcher.join([{ id: '1' }]);
expect(fetcher.batch).to.equal(1);
await fetcher.join([{ id: '2' }]);
expect(fetcher.batch).to.equal(2);
});

it('should handle DDB returning undefined joinedData', async function () {
batchGetStub.resolves({
Responses: {
'test-table': [] // no matching items
}
});

const events = [{ id: '1', value: 'a' }];
const result = await fetcher.join(events);

expect((result[0] as any).joinedData).to.be.undefined;
});
});
});
145 changes: 145 additions & 0 deletions test/lib.aws-util.utest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
import { expect, assert } from "chai";
import { copy, error, date } from "../lib/aws-util";

describe('lib/aws-util.ts', function () {

describe("copy", function () {
it('should return null for null input', function () {
assert.equal(copy(null), null);
});

it('should return undefined for undefined input', function () {
assert.equal(copy(undefined), undefined);
});

it('should create a shallow copy of an object', function () {
const original = { a: 1, b: 2 };
const copied = copy(original);
expect(copied).to.deep.equal(original);
expect(copied).to.not.equal(original);
});

it('should copy all properties', function () {
const original = { name: 'test', value: 42, flag: true };
const copied = copy(original);
expect(copied.name).to.equal('test');
expect(copied.value).to.equal(42);
expect(copied.flag).to.equal(true);
});

it('should handle empty object', function () {
const original = {};
const copied = copy(original);
expect(copied).to.deep.equal({});
expect(copied).to.not.equal(original);
});
});

describe("error", function () {
it('should set message from string options', function () {
const err: any = new Error();
const result = error(err, 'test message');
expect(result.message).to.equal('test message');
});

it('should set message from object options', function () {
const err: any = new Error();
const result = error(err, { message: 'object message' });
expect(result.message).to.equal('object message');
});

it('should set code from options', function () {
const err: any = new Error();
const result = error(err, { code: 'TEST_CODE' });
expect(result.code).to.equal('TEST_CODE');
});

it('should set name from options.name', function () {
const err: any = new Error();
const result = error(err, { name: 'CustomError' });
expect(result.name).to.equal('CustomError');
});

it('should use code as name if name not provided', function () {
const err: any = new Error();
const result = error(err, { code: 'ERROR_CODE' });
expect(result.name).to.equal('ERROR_CODE');
});

it('should set stack from options', function () {
const err: any = new Error();
const customStack = 'custom stack trace';
const result = error(err, { stack: customStack });
expect(result.stack).to.equal(customStack);
});

it('should set time to current date', function () {
const err: any = new Error();
const before = new Date();
const result = error(err, 'test');
const after = new Date();
expect(result.time.getTime()).to.be.at.least(before.getTime());
expect(result.time.getTime()).to.be.at.most(after.getTime());
});

it('should preserve original error when overwriting message', function () {
const err: any = new Error('original message');
const result = error(err, 'new message');
expect(result.originalError).to.not.be.undefined;
expect(result.originalError.message).to.equal('original message');
});

it('should preserve original error with object options containing message', function () {
const err: any = new Error('original message');
const result = error(err, { message: 'new message' });
expect(result.originalError).to.not.be.undefined;
expect(result.originalError.message).to.equal('original message');
});

it('should handle null options', function () {
const err: any = new Error('test');
const result = error(err, null);
expect(result.message).to.equal('test');
});

it('should handle error with empty message', function () {
const err: any = new Error();
err.message = '';
const result = error(err, { code: 'TEST' });
expect(result.message).to.equal(null);
});

it('should default name to Error when no options provided', function () {
const err: any = {};
const result = error(err, null);
expect(result.name).to.equal('Error');
});

it('should update err with all options properties', function () {
const err: any = new Error();
const result = error(err, {
message: 'test',
code: 'CODE',
customProp: 'custom'
});
expect(result.customProp).to.equal('custom');
});
});

describe("date", function () {
describe("getDate", function () {
it('should return a Date object', function () {
const result = date.getDate();
expect(result).to.be.instanceOf(Date);
});

it('should return current time', function () {
const before = Date.now();
const result = date.getDate();
const after = Date.now();
expect(result.getTime()).to.be.at.least(before);
expect(result.getTime()).to.be.at.most(after);
});
});
});
});
Loading