2023-09-15 10:34:14 +03:00
|
|
|
/// <reference types="../src/global.d.ts" />
|
2023-09-01 19:59:33 +03:00
|
|
|
|
|
|
|
import { Test, TestingModule } from '@nestjs/testing';
|
2023-09-29 06:02:26 +03:00
|
|
|
import ava, { type TestFn } from 'ava';
|
2023-09-01 19:59:33 +03:00
|
|
|
|
2023-09-15 10:34:14 +03:00
|
|
|
import { ConfigModule } from '../src/config';
|
|
|
|
import { SessionModule, SessionService } from '../src/session';
|
2023-09-01 19:59:33 +03:00
|
|
|
|
2023-09-08 23:02:27 +03:00
|
|
|
const test = ava as TestFn<{
|
|
|
|
session: SessionService;
|
|
|
|
app: TestingModule;
|
|
|
|
}>;
|
|
|
|
|
|
|
|
test.beforeEach(async t => {
|
|
|
|
const module = await Test.createTestingModule({
|
|
|
|
imports: [
|
|
|
|
ConfigModule.forRoot({
|
|
|
|
redis: {
|
|
|
|
enabled: false,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
SessionModule,
|
|
|
|
],
|
2023-09-01 19:59:33 +03:00
|
|
|
}).compile();
|
2023-09-08 23:02:27 +03:00
|
|
|
const session = module.get(SessionService);
|
|
|
|
t.context.app = module;
|
|
|
|
t.context.session = session;
|
2023-09-01 19:59:33 +03:00
|
|
|
});
|
|
|
|
|
2023-09-11 12:30:39 +03:00
|
|
|
test.afterEach.always(async t => {
|
2023-09-08 23:02:27 +03:00
|
|
|
await t.context.app.close();
|
2023-09-01 19:59:33 +03:00
|
|
|
});
|
|
|
|
|
2023-09-01 22:41:29 +03:00
|
|
|
test('should be able to set session', async t => {
|
2023-09-08 23:02:27 +03:00
|
|
|
const { session } = t.context;
|
2023-09-01 19:59:33 +03:00
|
|
|
await session.set('test', 'value');
|
2023-09-05 11:01:45 +03:00
|
|
|
t.is(await session.get('test'), 'value');
|
2023-09-01 19:59:33 +03:00
|
|
|
});
|
|
|
|
|
2023-09-01 22:41:29 +03:00
|
|
|
test('should be expired by ttl', async t => {
|
2023-09-08 23:02:27 +03:00
|
|
|
const { session } = t.context;
|
2023-09-01 19:59:33 +03:00
|
|
|
await session.set('test', 'value', 100);
|
2023-09-05 11:01:45 +03:00
|
|
|
t.is(await session.get('test'), 'value');
|
2023-09-01 19:59:33 +03:00
|
|
|
await new Promise(resolve => setTimeout(resolve, 500));
|
2023-09-05 11:01:45 +03:00
|
|
|
t.is(await session.get('test'), undefined);
|
2023-09-01 19:59:33 +03:00
|
|
|
});
|