AFFiNE/packages/backend/server/tests/session.spec.ts

47 lines
1.2 KiB
TypeScript
Raw Normal View History

/// <reference types="../src/global.d.ts" />
import { Test, TestingModule } from '@nestjs/testing';
import ava, { type TestFn } from 'ava';
import { ConfigModule } from '../src/config';
import { SessionModule, SessionService } from '../src/session';
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,
],
}).compile();
const session = module.get(SessionService);
t.context.app = module;
t.context.session = session;
});
test.afterEach.always(async t => {
await t.context.app.close();
});
2023-09-01 22:41:29 +03:00
test('should be able to set session', async t => {
const { session } = t.context;
await session.set('test', 'value');
t.is(await session.get('test'), 'value');
});
2023-09-01 22:41:29 +03:00
test('should be expired by ttl', async t => {
const { session } = t.context;
await session.set('test', 'value', 100);
t.is(await session.get('test'), 'value');
await new Promise(resolve => setTimeout(resolve, 500));
t.is(await session.get('test'), undefined);
});