2024-04-13 14:20:12 +02:00
|
|
|
import { randomBytes } from "node:crypto";
|
2024-05-29 02:59:49 +02:00
|
|
|
import { consoleLogger } from "@/loggers";
|
2024-04-17 06:09:21 +02:00
|
|
|
import { asc, inArray, like } from "drizzle-orm";
|
2024-05-29 02:59:49 +02:00
|
|
|
import type { Status } from "~/database/entities/Status";
|
|
|
|
|
import { db } from "~/drizzle/db";
|
|
|
|
|
import { setupDatabase } from "~/drizzle/db";
|
|
|
|
|
import { Notes, Tokens, Users } from "~/drizzle/schema";
|
|
|
|
|
import { app } from "~/index";
|
|
|
|
|
import { Note } from "~/packages/database-interface/note";
|
|
|
|
|
import { User } from "~/packages/database-interface/user";
|
2024-05-13 23:36:46 +02:00
|
|
|
|
|
|
|
|
await setupDatabase(consoleLogger);
|
|
|
|
|
|
2024-03-11 03:04:14 +01:00
|
|
|
/**
|
|
|
|
|
* This allows us to send a test request to the server even when it isnt running
|
|
|
|
|
* @param req Request to send
|
|
|
|
|
* @returns Response from the server
|
|
|
|
|
*/
|
|
|
|
|
export async function sendTestRequest(req: Request) {
|
2024-05-13 23:36:46 +02:00
|
|
|
// return fetch(req);
|
2024-05-06 09:16:33 +02:00
|
|
|
return app.fetch(req);
|
2024-03-11 03:04:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function wrapRelativeUrl(url: string, base_url: string) {
|
2024-04-07 07:30:49 +02:00
|
|
|
return new URL(url, base_url);
|
2024-03-11 03:04:14 +01:00
|
|
|
}
|
2024-04-11 15:52:44 +02:00
|
|
|
|
|
|
|
|
export const deleteOldTestUsers = async () => {
|
|
|
|
|
// Deletes all users that match the test username (test-<32 random characters>)
|
2024-04-17 08:36:01 +02:00
|
|
|
await db.delete(Users).where(like(Users.username, "test-%"));
|
2024-04-11 15:52:44 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const getTestUsers = async (count: number) => {
|
2024-04-25 05:40:27 +02:00
|
|
|
const users: User[] = [];
|
2024-04-14 02:46:33 +02:00
|
|
|
const passwords: string[] = [];
|
2024-04-11 15:52:44 +02:00
|
|
|
|
|
|
|
|
for (let i = 0; i < count; i++) {
|
2024-04-14 02:46:33 +02:00
|
|
|
const password = randomBytes(32).toString("hex");
|
|
|
|
|
|
2024-04-25 05:48:39 +02:00
|
|
|
const user = await User.fromDataLocal({
|
2024-04-11 15:52:44 +02:00
|
|
|
username: `test-${randomBytes(32).toString("hex")}`,
|
|
|
|
|
email: `${randomBytes(32).toString("hex")}@test.com`,
|
2024-04-14 02:46:33 +02:00
|
|
|
password,
|
2024-04-11 15:52:44 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!user) {
|
|
|
|
|
throw new Error("Failed to create test user");
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-14 02:46:33 +02:00
|
|
|
passwords.push(password);
|
2024-04-11 15:52:44 +02:00
|
|
|
users.push(user);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const tokens = await db
|
2024-04-17 08:36:01 +02:00
|
|
|
.insert(Tokens)
|
2024-04-11 15:52:44 +02:00
|
|
|
.values(
|
|
|
|
|
users.map((u) => ({
|
|
|
|
|
accessToken: randomBytes(32).toString("hex"),
|
|
|
|
|
tokenType: "bearer",
|
|
|
|
|
userId: u.id,
|
|
|
|
|
applicationId: null,
|
|
|
|
|
code: randomBytes(32).toString("hex"),
|
|
|
|
|
scope: "read write follow push",
|
|
|
|
|
})),
|
|
|
|
|
)
|
|
|
|
|
.returning();
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
users,
|
|
|
|
|
tokens,
|
2024-04-14 02:46:33 +02:00
|
|
|
passwords,
|
2024-04-11 15:52:44 +02:00
|
|
|
deleteUsers: async () => {
|
2024-04-17 08:36:01 +02:00
|
|
|
await db.delete(Users).where(
|
2024-04-11 15:52:44 +02:00
|
|
|
inArray(
|
2024-04-17 08:36:01 +02:00
|
|
|
Users.id,
|
2024-04-11 15:52:44 +02:00
|
|
|
users.map((u) => u.id),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const getTestStatuses = async (
|
|
|
|
|
count: number,
|
|
|
|
|
user: User,
|
|
|
|
|
partial?: Partial<Status>,
|
|
|
|
|
) => {
|
|
|
|
|
const statuses: Status[] = [];
|
|
|
|
|
|
|
|
|
|
for (let i = 0; i < count; i++) {
|
2024-04-17 06:09:21 +02:00
|
|
|
const newStatus = await Note.insert({
|
|
|
|
|
content: `${i} ${randomBytes(32).toString("hex")}`,
|
|
|
|
|
authorId: user.id,
|
|
|
|
|
sensitive: false,
|
|
|
|
|
updatedAt: new Date().toISOString(),
|
|
|
|
|
visibility: "public",
|
|
|
|
|
applicationId: null,
|
|
|
|
|
...partial,
|
|
|
|
|
});
|
2024-04-11 15:52:44 +02:00
|
|
|
|
|
|
|
|
if (!newStatus) {
|
|
|
|
|
throw new Error("Failed to create test status");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
statuses.push(newStatus);
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-17 06:09:21 +02:00
|
|
|
return (
|
|
|
|
|
await Note.manyFromSql(
|
2024-04-14 02:46:33 +02:00
|
|
|
inArray(
|
2024-04-17 08:36:01 +02:00
|
|
|
Notes.id,
|
2024-04-14 02:46:33 +02:00
|
|
|
statuses.map((s) => s.id),
|
|
|
|
|
),
|
2024-04-17 08:36:01 +02:00
|
|
|
asc(Notes.id),
|
2024-05-09 01:19:53 +02:00
|
|
|
undefined,
|
|
|
|
|
undefined,
|
|
|
|
|
user.id,
|
2024-04-17 06:09:21 +02:00
|
|
|
)
|
|
|
|
|
).map((n) => n.getStatus());
|
2024-04-11 15:52:44 +02:00
|
|
|
};
|