2024-03-24 08:32:40 +00:00
|
|
|
import supertest from 'supertest';
|
|
|
|
import { afterAll } from 'vitest';
|
2024-07-28 12:32:41 +00:00
|
|
|
import { app } from '../app.js';
|
|
|
|
import { prisma } from '../model/_client.js';
|
2024-03-24 08:32:40 +00:00
|
|
|
import { PrismaPromise } from '@prisma/client';
|
2024-07-28 12:32:41 +00:00
|
|
|
import { createUser } from '../model/user.js';
|
2024-03-24 08:32:40 +00:00
|
|
|
import { nanoid } from 'nanoid';
|
|
|
|
|
|
|
|
export function createTestContext() {
|
|
|
|
const testDataCallback: (() => PrismaPromise<any> | Promise<any>)[] = [];
|
|
|
|
|
|
|
|
afterAll(async () => {
|
|
|
|
for (const cb of testDataCallback.reverse()) {
|
|
|
|
await cb();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const createTestUser = async () => {
|
|
|
|
const data = await createUser(nanoid(), '');
|
|
|
|
|
|
|
|
testDataCallback.push(async () => {
|
|
|
|
await prisma.user.delete({
|
|
|
|
where: { id: data.id },
|
|
|
|
});
|
|
|
|
|
|
|
|
await prisma.workspace.delete({
|
|
|
|
where: { id: data.currentWorkspace.id },
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
user: data,
|
|
|
|
workspace: data.currentWorkspace,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const createTestWorkspace = async () => {
|
|
|
|
const data = await prisma.workspace.create({
|
|
|
|
data: {
|
|
|
|
name: 'Test Workspace',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
testDataCallback.push(() =>
|
|
|
|
prisma.workspace.delete({
|
|
|
|
where: { id: data.id },
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
return data;
|
|
|
|
};
|
|
|
|
|
|
|
|
const createTestTelemetry = async (workspaceId: string) => {
|
|
|
|
const data = await prisma.telemetry.create({
|
|
|
|
data: {
|
|
|
|
name: 'Test Telemetry',
|
|
|
|
workspaceId,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
testDataCallback.push(() =>
|
|
|
|
prisma.telemetry.delete({
|
|
|
|
where: { id: data.id },
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
return data;
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
app: supertest(app),
|
|
|
|
createTestUser,
|
|
|
|
createTestWorkspace,
|
|
|
|
createTestTelemetry,
|
|
|
|
};
|
|
|
|
}
|