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
653 changes: 653 additions & 0 deletions packages/ingest/tests/Loader.test.ts

Large diffs are not rendered by default.

115 changes: 113 additions & 2 deletions packages/ingest/tests/Router.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { describe, it } from 'mocha';
import { expect } from 'chai';

import type { Method } from '@stackpress/types/dist/types';
import type { RouterEntry } from '../src/types';
import type { EntryAction } from '../src/types';
import path from 'path';
import Router from '../src/Router';
import Request from '../src/Request';
Expand Down Expand Up @@ -93,7 +93,7 @@ describe('Router Tests', () => {
for (const method of methods) {
const route = router[method].bind(router) as (
path: string,
action: RouterEntry<unknown, unknown, unknown>, priority?: number
action: EntryAction<unknown, unknown, unknown>, priority?: number
) => Router<unknown, unknown, unknown>;
route('/some/route/path', path.join(__dirname, 'fixtures/any'));
const METHOD = method.toUpperCase() as Method;
Expand Down Expand Up @@ -122,4 +122,115 @@ describe('Router Tests', () => {
await router.emit('POST /some/route/path', req, res);
expect(res.body).to.equal(`POST /some/route/path`);
})

it('Should handle route parameters correctly', async () => {
const router = new Router();
router.get('/users/:id/posts/:postId', (req, res) => {
res.setBody('application/json', {
params: req.data.get(),
path: req.url.pathname
});
});

const req = new Request({
method: 'GET',
url: new URL('http://localhost/users/123/posts/456')
});
const res = new Response();
await router.emit('GET /users/123/posts/456', req, res);

const body = res.body as { params: any, path: string };
expect(body.params).to.deep.equal({
id: '123',
postId: '456'
});
expect(body.path).to.equal('/users/123/posts/456');
});

it('Should handle wildcard routes correctly', async () => {
const router = new Router();
router.get('/files/**', (req, res) => {
res.setBody('application/json', {
params: req.data.get(),
path: req.url.pathname
});
});

const req = new Request({
method: 'GET',
url: new URL('http://localhost/files/images/avatar.png')
});
const res = new Response();
await router.emit('GET /files/images/avatar.png', req, res);

const body = res.body as { params: any, path: string };
expect(body.path).to.equal('/files/images/avatar.png');
});

it('Should handle route priority correctly', async () => {
const router = new Router();
const calls: string[] = [];

// Lower priority (1) route
router.get('/api/resource', (req, res) => {
calls.push('low');
}, 1);

// Higher priority (2) route
router.get('/api/resource', (req, res) => {
calls.push('high');
}, 2);

const req = new Request({
method: 'GET',
url: new URL('http://localhost/api/resource')
});
const res = new Response();
await router.emit('GET /api/resource', req, res);

// Higher priority should execute first
expect(calls).to.deep.equal(['high', 'low']);
});

it('Should handle router chaining with use() correctly', async () => {
const router1 = new Router();
const router2 = new Router();

router1.get('/api/v1/test', (req, res) => {
res.setBody('text/plain', 'router1');
});

router2.use(router1);

const req = new Request({
method: 'GET',
url: new URL('http://localhost/api/v1/test')
});
const res = new Response();
await router2.emit('GET /api/v1/test', req, res);

expect(res.body).to.equal('router1');
});

it('Should handle entry file caching correctly', async () => {
const uncachedRouter = new Router(false);
const cachedRouter = new Router(true);

uncachedRouter.get('/test', path.join(__dirname, 'fixtures/get'));
cachedRouter.get('/test', path.join(__dirname, 'fixtures/get'));

const req = new Request({
method: 'GET',
url: new URL('http://localhost/test')
});
const res1 = new Response();
const res2 = new Response();

await uncachedRouter.emit('GET /test', req, res1);
await cachedRouter.emit('GET /test', req, res2);

expect(res1.body).to.equal(res2.body);
expect(uncachedRouter.cache).to.be.false;
expect(cachedRouter.cache).to.be.true;
});
})
Loading
Loading