Merge pull request #5 from MatthieuTD/codex/prepare-minimal-context-for-repositories

Fix e2e inventory tests to rely on in-memory Prisma stub
This commit is contained in:
MatthieuTD
2025-09-22 10:39:20 +02:00
committed by GitHub

View File

@@ -175,6 +175,21 @@ class InMemoryPrismaService {
return fn(this);
}
reset() {
this.sites = [];
this.typeComposants = [];
this.typePieces = [];
this.typeMachines = [];
this.typeMachineComponentRequirements = [];
this.typeMachinePieceRequirements = [];
this.machines = [];
this.composants = [];
this.pieces = [];
this.customFields = [];
this.customFieldValues = [];
this.profiles = [];
}
site = {
create: async ({ data }: any) => {
const now = new Date();
@@ -898,6 +913,7 @@ class InMemoryPrismaService {
describe('Inventory flow (e2e)', () => {
let app: INestApplication;
let prismaStub: InMemoryPrismaService;
let prisma: InMemoryPrismaService;
beforeAll(async () => {
prismaStub = new InMemoryPrismaService();
@@ -913,25 +929,19 @@ describe('Inventory flow (e2e)', () => {
app = moduleFixture.createNestApplication();
await app.init();
prisma = app.get(PrismaService);
prisma = prismaStub;
});
afterAll(async () => {
await app.close();
});
beforeEach(async () => {
await prisma.$executeRawUnsafe(
'TRUNCATE TABLE custom_field_values, documents, pieces, composants, machines, composant_models, piece_models, type_machine_component_requirements, type_machine_piece_requirements, custom_fields, type_machines, type_composants, type_pieces, constructeurs, sites RESTART IDENTITY CASCADE',
);
beforeEach(() => {
prisma.reset();
});
afterEach(async () => {
await app.close();
});
afterAll(async () => {
await app.close();
afterEach(() => {
jest.restoreAllMocks();
});
it('should create a type, create a machine with new component/piece selections, and edit technical fields', async () => {
@@ -1090,17 +1100,17 @@ describe('Inventory flow (e2e)', () => {
describe('POST /composants', () => {
it('accepts creation when requirement matches the machine skeleton', async () => {
prisma.machine.findUnique.mockResolvedValue({
jest.spyOn(prisma.machine, 'findUnique').mockResolvedValue({
id: 'machine-1',
typeMachine: {
componentRequirements: [
{ id: 'req-1', typeComposantId: 'type-comp-1' },
],
},
});
} as any);
const created = { id: 'component-1' };
prisma.composant.create.mockResolvedValue(created);
const createSpy = jest.spyOn(prisma.composant, 'create').mockResolvedValue(created as any);
const response = await request(app.getHttpServer())
.post('/composants')
@@ -1113,18 +1123,20 @@ describe('Inventory flow (e2e)', () => {
.expect(201);
expect(response.body).toEqual(created);
expect(prisma.composant.create).toHaveBeenCalled();
expect(createSpy).toHaveBeenCalled();
});
it('refuses creation when requirement is not part of the machine skeleton', async () => {
prisma.machine.findUnique.mockResolvedValue({
jest.spyOn(prisma.machine, 'findUnique').mockResolvedValue({
id: 'machine-1',
typeMachine: {
componentRequirements: [
{ id: 'req-1', typeComposantId: 'type-comp-1' },
],
},
});
} as any);
const createSpy = jest.spyOn(prisma.composant, 'create');
await request(app.getHttpServer())
.post('/composants')
@@ -1136,23 +1148,23 @@ describe('Inventory flow (e2e)', () => {
})
.expect(400);
expect(prisma.composant.create).not.toHaveBeenCalled();
expect(createSpy).not.toHaveBeenCalled();
});
});
describe('POST /pieces', () => {
it('accepts creation when requirement matches the machine skeleton', async () => {
prisma.machine.findUnique.mockResolvedValue({
jest.spyOn(prisma.machine, 'findUnique').mockResolvedValue({
id: 'machine-1',
typeMachine: {
pieceRequirements: [
{ id: 'req-1', typePieceId: 'type-piece-1' },
],
},
});
} as any);
const created = { id: 'piece-1' };
prisma.piece.create.mockResolvedValue(created);
const createSpy = jest.spyOn(prisma.piece, 'create').mockResolvedValue(created as any);
const response = await request(app.getHttpServer())
.post('/pieces')
@@ -1165,18 +1177,20 @@ describe('Inventory flow (e2e)', () => {
.expect(201);
expect(response.body).toEqual(created);
expect(prisma.piece.create).toHaveBeenCalled();
expect(createSpy).toHaveBeenCalled();
});
it('refuses creation when requirement is not part of the machine skeleton', async () => {
prisma.machine.findUnique.mockResolvedValue({
jest.spyOn(prisma.machine, 'findUnique').mockResolvedValue({
id: 'machine-1',
typeMachine: {
pieceRequirements: [
{ id: 'req-1', typePieceId: 'type-piece-1' },
],
},
});
} as any);
const createSpy = jest.spyOn(prisma.piece, 'create');
await request(app.getHttpServer())
.post('/pieces')
@@ -1188,7 +1202,7 @@ describe('Inventory flow (e2e)', () => {
})
.expect(400);
expect(prisma.piece.create).not.toHaveBeenCalled();
expect(createSpy).not.toHaveBeenCalled();
});
});