Skip to content
Merged
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
177 changes: 177 additions & 0 deletions packages/ingest/tests/Context.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { describe, it } from 'mocha';
import { expect } from 'chai';
import Request from '../src/Request';
import RequestContext from '../src/Context';

describe('Context Tests', () => {
it('Should initialize', () => {
Expand Down Expand Up @@ -61,4 +62,180 @@ describe('Context Tests', () => {
expect(context.data('blah')).to.equal('zeh');
expect(request.data('blah')).to.equal('zeh');
});

it('Should initialize with different argument types', () => {
const request = new Request({
method: 'GET',
url: 'http://localhost/test/arg1/arg2',
data: { existingKey: 'existingValue' }
});

// Test with Set arguments
const setArgs = new Set(['arg1', 'arg2']);
let context = request.fromRoute('/test/*/**');
expect(context.args.has('arg1')).to.be.true;
expect(context.args.has('arg2')).to.be.true;

// Test with Array arguments
request.url.pathname = '/test/arg3/arg4';
context = request.fromRoute('/test/*/**');
expect(context.args.has('arg3')).to.be.true;
expect(context.args.has('arg4')).to.be.true;

// Test with no matches
request.url.pathname = '/no-match';
context = request.fromRoute('/test/*/**');
expect(context.args.size).to.equal(0);

// Test with null arguments
context = new RequestContext(request, { args: null as any });
expect(context.args.size).to.equal(0);

// Test with non-array/set arguments
context = new RequestContext(request, { args: 123 as any });
expect(context.args.size).to.equal(0);

// Test with empty Set
context = new RequestContext(request, { args: new Set() });
expect(context.args.size).to.equal(0);

// Test with empty Array
context = new RequestContext(request, { args: [] });
expect(context.args.size).to.equal(0);

// Test with Set containing non-string values
const nonStringSet = new Set([1, 2, 3]);
context = new RequestContext(request, {
args: Array.from(nonStringSet).map(String)
});
expect(context.args.size).to.equal(3);
expect(context.args.has('1')).to.be.true;
expect(context.args.has('2')).to.be.true;
expect(context.args.has('3')).to.be.true;
});

it('Should initialize with different parameter types', () => {
const request = new Request({
method: 'GET',
url: 'http://localhost/test/value1/value2',
data: { existingKey: 'existingValue' }
});

// Test with Map parameters
const mapParams = new Map([['key1', 'value1'], ['key2', 'value2']]);
let context = request.fromRoute('/test/:key1/:key2');
expect(context.params('key1')).to.equal('value1');
expect(context.params('key2')).to.equal('value2');
expect(context.data('key1')).to.equal('value1');
expect(context.data('key2')).to.equal('value2');

// Test with object parameters
request.url.pathname = '/test/value3/value4';
context = request.fromRoute('/test/:key3/:key4');
expect(context.params('key3')).to.equal('value3');
expect(context.params('key4')).to.equal('value4');
expect(context.data('key3')).to.equal('value3');
expect(context.data('key4')).to.equal('value4');

// Test with no matches
request.url.pathname = '/no-match';
context = request.fromRoute('/test/:key1/:key2');
expect(context.params.size).to.equal(0);

// Test with null parameters
context = new RequestContext(request, { params: null as any });
expect(context.params.size).to.equal(0);

// Test with non-map/object parameters
context = new RequestContext(request, { params: 123 as any });
expect(context.params.size).to.equal(0);

// Test with empty Map
context = new RequestContext(request, { params: new Map() });
expect(context.params.size).to.equal(0);

// Test with empty object
context = new RequestContext(request, { params: {} });
expect(context.params.size).to.equal(0);

// Test with Map containing non-string values
const nonStringMap = new Map<string, string | number | boolean>([['key1', 1], ['key2', true]]);
context = new RequestContext(request, {
params: new Map(Array.from(nonStringMap.entries()).map(([k, v]) => [k, String(v)]))
});
expect(context.params.size).to.equal(2);
expect(context.params('key1')).to.equal('1');
expect(context.params('key2')).to.equal('true');

// Test with object containing non-string values
context = new RequestContext(request, {
params: { key1: String(1), key2: String(true) }
});
expect(context.params.size).to.equal(2);
expect(context.params('key1')).to.equal('1');
expect(context.params('key2')).to.equal('true');
});

it('Should handle loaded state correctly', async () => {
// Create a request with a body
const request = new Request({
method: 'POST',
url: 'http://localhost/test',
body: 'test body',
mimetype: 'text/plain'
});
await request.load();
const context = request.fromRoute('/test');
expect(context.loaded).to.be.true;

// Create a request without a body
const emptyRequest = new Request({
method: 'GET',
url: 'http://localhost/test'
});
const emptyContext = emptyRequest.fromRoute('/test');
expect(emptyContext.loaded).to.be.false;
});

it('Should handle data sync correctly', () => {
const request = new Request({
method: 'GET',
url: 'http://localhost/test',
data: { key1: 'existing1', key2: 'existing2' }
});

// Test when param key exists in data (shouldn't override)
let context = new RequestContext(request, {
params: new Map([
['key1', 'param1'], // exists in data
['key3', 'param3'] // doesn't exist in data
])
});

expect(context.data('key1')).to.equal('existing1'); // unchanged
expect(context.data('key2')).to.equal('existing2'); // unchanged
expect(context.data('key3')).to.equal('param3'); // added

// Test with empty params (no data sync needed)
context = new RequestContext(request, {
params: new Map()
});
expect(context.data('key1')).to.equal('existing1');
expect(context.data('key2')).to.equal('existing2');

// Test with undefined params (no data sync needed)
context = new RequestContext(request);
expect(context.data('key1')).to.equal('existing1');
expect(context.data('key2')).to.equal('existing2');

// Test with null data in request
const emptyRequest = new Request({
method: 'GET',
url: 'http://localhost/test'
});
context = new RequestContext(emptyRequest, {
params: new Map([['key1', 'value1']])
});
expect(context.data('key1')).to.equal('value1');
});
});
49 changes: 49 additions & 0 deletions packages/ingest/tests/Response.test.ts
Copy link
Member

Choose a reason for hiding this comment

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

Body test is a good test. thanks!

Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,53 @@ describe('Response Tests', () => {
await response.dispatch();
expect(dispatched).to.be.true;
});

it('Should handle all body types', () => {
const response = new Response();

// Test Buffer type
response.body = Buffer.from('test');
expect(response.type).to.equal('buffer');

// Test Uint8Array type
response.body = new Uint8Array([1, 2, 3]);
expect(response.type).to.equal('uint8array');

// Test null type
response.body = null;
expect(response.type).to.equal('null');

// Test string type
response.body = 'test';
expect(response.type).to.equal('string');
});

it('Should handle all header initialization types', () => {
// Test Map headers
const mapHeaders = new Map([['Content-Type', 'application/json']]);
const responseWithMap = new Response({ headers: mapHeaders });
expect(responseWithMap.headers.get('Content-Type')).to.equal('application/json');

// Test object headers
const objHeaders = { 'Content-Type': 'application/json' };
const responseWithObj = new Response({ headers: objHeaders });
expect(responseWithObj.headers.get('Content-Type')).to.equal('application/json');

// Test undefined headers
const responseWithUndefined = new Response({ headers: undefined });
expect(responseWithUndefined.headers.size).to.equal(0);
});

it('Should handle all dispatch scenarios', async () => {
const response = new Response();

// Test dispatching without dispatcher
await response.dispatch();
expect(response.sent).to.be.true;

// Test dispatching when already sent
const prevSent = response.sent;
await response.dispatch();
expect(response.sent).to.equal(prevSent);
});
});
47 changes: 46 additions & 1 deletion packages/ingest/tests/helpers.test.ts
Copy link
Member

Choose a reason for hiding this comment

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

Thanks for the stream tests!

Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ import {
objectFromJson,
eventParams,
routeParams,
withUnknownHost
withUnknownHost,
readableStreamToReadable,
readableToReadableStream
} from '../src/helpers';
import { Readable } from 'stream';

describe('helpers', () => {
describe('isHash', () => {
Expand Down Expand Up @@ -134,4 +137,46 @@ describe('helpers', () => {
expect(result).to.equal('http://unknownhost/https://example.com/path');
});
});

describe('readableStreamToReadable', () => {
it('should convert ReadableStream to Node.js Readable', async () => {
const data = 'Hello, World!';
const stream = new ReadableStream({
start(controller) {
controller.enqueue(new TextEncoder().encode(data));
controller.close();
}
});

const readable = readableStreamToReadable(stream);
expect(readable).to.be.instanceOf(Readable);

let result = '';
for await (const chunk of readable) {
result += chunk;
}
expect(result).to.equal(data);
});
});

describe('readableToReadableStream', () => {
it('should convert Node.js Readable to ReadableStream', async () => {
const data = 'Hello, World!';
const buffer = Buffer.from(data);
const readable = Readable.from([buffer]);

const stream = readableToReadableStream(readable);
expect(stream).to.be.instanceOf(ReadableStream);

const reader = stream.getReader();
const chunks: Uint8Array[] = [];
while (true) {
const { done, value } = await reader.read();
if (done) break;
chunks.push(value);
}
const result = Buffer.concat(chunks).toString();
expect(result).to.equal(data);
});
});
});