97 lines
2.5 KiB
TypeScript
97 lines
2.5 KiB
TypeScript
|
|
import { INestApplication } from '@nestjs/common';
|
||
|
|
import { Test, TestingModule } from '@nestjs/testing';
|
||
|
|
import { AccessMode, ToolStatus } from '@prisma/client';
|
||
|
|
import { randomUUID } from 'crypto';
|
||
|
|
import request from 'supertest';
|
||
|
|
import { AppModule } from '../src/app.module';
|
||
|
|
import { PrismaService } from '../src/prisma/prisma.service';
|
||
|
|
|
||
|
|
interface ToolDetailResponse {
|
||
|
|
id: string;
|
||
|
|
slug: string;
|
||
|
|
name: string;
|
||
|
|
description: string;
|
||
|
|
}
|
||
|
|
|
||
|
|
describe('Tools detail by slug (e2e)', () => {
|
||
|
|
let app: INestApplication;
|
||
|
|
let prisma: PrismaService;
|
||
|
|
let categoryId = '';
|
||
|
|
let toolId = '';
|
||
|
|
let toolSlug = '';
|
||
|
|
|
||
|
|
beforeAll(async () => {
|
||
|
|
const moduleFixture: TestingModule = await Test.createTestingModule({
|
||
|
|
imports: [AppModule],
|
||
|
|
}).compile();
|
||
|
|
|
||
|
|
app = moduleFixture.createNestApplication();
|
||
|
|
await app.init();
|
||
|
|
prisma = app.get(PrismaService);
|
||
|
|
});
|
||
|
|
|
||
|
|
beforeEach(async () => {
|
||
|
|
categoryId = `cat_${randomUUID().replace(/-/g, '')}`;
|
||
|
|
toolId = `tool_${randomUUID().replace(/-/g, '')}`;
|
||
|
|
toolSlug = `tool-${randomUUID().slice(0, 8)}`;
|
||
|
|
|
||
|
|
await prisma.category.create({
|
||
|
|
data: {
|
||
|
|
id: categoryId,
|
||
|
|
name: `Category ${toolSlug}`,
|
||
|
|
},
|
||
|
|
});
|
||
|
|
|
||
|
|
await prisma.tool.create({
|
||
|
|
data: {
|
||
|
|
id: toolId,
|
||
|
|
name: 'Slug Detail Tool',
|
||
|
|
slug: toolSlug,
|
||
|
|
categoryId,
|
||
|
|
description: '# Manual\n\n## Install',
|
||
|
|
accessMode: AccessMode.web,
|
||
|
|
openUrl: 'https://example.com/tool',
|
||
|
|
status: ToolStatus.published,
|
||
|
|
updatedAt: '2026-04-11',
|
||
|
|
},
|
||
|
|
});
|
||
|
|
});
|
||
|
|
|
||
|
|
afterEach(async () => {
|
||
|
|
await prisma.tool.deleteMany({
|
||
|
|
where: {
|
||
|
|
id: toolId,
|
||
|
|
},
|
||
|
|
});
|
||
|
|
await prisma.category.deleteMany({
|
||
|
|
where: {
|
||
|
|
id: categoryId,
|
||
|
|
},
|
||
|
|
});
|
||
|
|
});
|
||
|
|
|
||
|
|
afterAll(async () => {
|
||
|
|
await app.close();
|
||
|
|
});
|
||
|
|
|
||
|
|
function getHttpServer(): Parameters<typeof request>[0] {
|
||
|
|
return app.getHttpServer() as Parameters<typeof request>[0];
|
||
|
|
}
|
||
|
|
|
||
|
|
it('returns a published tool detail by slug', async () => {
|
||
|
|
await request(getHttpServer())
|
||
|
|
.get(`/tools/slug/${toolSlug}`)
|
||
|
|
.expect(200)
|
||
|
|
.expect(({ body }: { body: ToolDetailResponse }) => {
|
||
|
|
expect(body.id).toBe(toolId);
|
||
|
|
expect(body.slug).toBe(toolSlug);
|
||
|
|
expect(body.name).toBe('Slug Detail Tool');
|
||
|
|
expect(body.description).toContain('# Manual');
|
||
|
|
});
|
||
|
|
});
|
||
|
|
|
||
|
|
it('returns 404 for an unknown slug', async () => {
|
||
|
|
await request(getHttpServer()).get('/tools/slug/missing-tool').expect(404);
|
||
|
|
});
|
||
|
|
});
|