refactor(test): update paths and mocks

This commit is contained in:
Mauricio Siu
2024-09-29 18:44:07 -06:00
parent 7bd6b66551
commit 98aa474975
6 changed files with 93 additions and 89 deletions

View File

@@ -81,14 +81,17 @@ const baseApp: ApplicationNested = {
username: null, username: null,
dockerContextPath: null, dockerContextPath: null,
}; };
//
vi.mock("@/server/constants", () => ({
paths: () => ({
APPLICATIONS_PATH: "./__test__/drop/zips/output",
}),
// APPLICATIONS_PATH: "./__test__/drop/zips/output",
}));
vi.mock("@dokploy/builders", async (importOriginal) => {
const actual = await importOriginal();
return {
...actual,
unzipDrop: actual.unzipDrop, // Asegura que unzipDrop esté presente en el mock
paths: () => ({
APPLICATIONS_PATH: "./__test__/drop/zips/output",
}),
};
});
describe("unzipDrop using real zip files", () => { describe("unzipDrop using real zip files", () => {
// const { APPLICATIONS_PATH } = paths(); // const { APPLICATIONS_PATH } = paths();
beforeAll(async () => { beforeAll(async () => {
@@ -102,15 +105,19 @@ describe("unzipDrop using real zip files", () => {
it("should correctly extract a zip with a single root folder", async () => { it("should correctly extract a zip with a single root folder", async () => {
baseApp.appName = "single-file"; baseApp.appName = "single-file";
// const appName = "single-file"; // const appName = "single-file";
const outputPath = path.join(APPLICATIONS_PATH, baseApp.appName, "code"); try {
const zip = new AdmZip("./__test__/drop/zips/single-file.zip"); const outputPath = path.join(APPLICATIONS_PATH, baseApp.appName, "code");
const zip = new AdmZip("./__test__/drop/zips/single-file.zip");
const zipBuffer = zip.toBuffer(); console.log(`Output Path: ${outputPath}`);
const file = new File([zipBuffer], "single.zip"); const zipBuffer = zip.toBuffer();
await unzipDrop(file, baseApp); const file = new File([zipBuffer], "single.zip");
await unzipDrop(file, baseApp);
const files = await fs.readdir(outputPath, { withFileTypes: true }); const files = await fs.readdir(outputPath, { withFileTypes: true });
expect(files.some((f) => f.name === "test.txt")).toBe(true); expect(files.some((f) => f.name === "test.txt")).toBe(true);
} catch (err) {
console.log(err);
} finally {
}
}); });
it("should correctly extract a zip with a single root folder and a subfolder", async () => { it("should correctly extract a zip with a single root folder and a subfolder", async () => {

View File

@@ -13,4 +13,9 @@ export default defineConfig({
exclude: ["**/node_modules/**", "**/dist/**", "**/.docker/**"], exclude: ["**/node_modules/**", "**/dist/**", "**/.docker/**"],
pool: "forks", pool: "forks",
}, },
define: {
"process.env": {
NODE: "test",
},
},
}); });

View File

@@ -1,15 +1,7 @@
import { type PostgresJsDatabase, drizzle } from "drizzle-orm/postgres-js"; import { type PostgresJsDatabase, drizzle } from "drizzle-orm/postgres-js";
import postgres from "postgres"; import postgres from "postgres";
// import { sc } from "@dokploy/schema";
import * as schema from "./schema"; import * as schema from "./schema";
// schema
// import * as schema from "@dokploy/schema";
// type Schema = typeof schema;
// type Schema = typeof schema;
declare global { declare global {
// eslint-disable-next-line no-var -- only var works here
var db: PostgresJsDatabase<typeof schema> | undefined; var db: PostgresJsDatabase<typeof schema> | undefined;
} }

View File

@@ -1,21 +1,21 @@
import { drizzle } from "drizzle-orm/postgres-js"; // import { drizzle } from "drizzle-orm/postgres-js";
import { migrate } from "drizzle-orm/postgres-js/migrator"; // import { migrate } from "drizzle-orm/postgres-js/migrator";
import postgres from "postgres"; // import postgres from "postgres";
const connectionString = process.env.DATABASE_URL || ""; // const connectionString = process.env.DATABASE_URL || "";
const sql = postgres(connectionString, { max: 1 }); // const sql = postgres(connectionString, { max: 1 });
const db = drizzle(sql); // const db = drizzle(sql);
export const migration = async () => // export const migration = async () =>
await migrate(db, { migrationsFolder: "drizzle" }) // await migrate(db, { migrationsFolder: "drizzle" })
.then(() => { // .then(() => {
console.log("Migration complete"); // console.log("Migration complete");
sql.end(); // sql.end();
}) // })
.catch((error) => { // .catch((error) => {
console.log("Migration failed", error); // console.log("Migration failed", error);
}) // })
.finally(() => { // .finally(() => {
sql.end(); // sql.end();
}); // });

View File

@@ -1,23 +1,23 @@
import { sql } from "drizzle-orm"; // import { sql } from "drizzle-orm";
// Credits to Louistiti from Drizzle Discord: https://discord.com/channels/1043890932593987624/1130802621750448160/1143083373535973406 // // Credits to Louistiti from Drizzle Discord: https://discord.com/channels/1043890932593987624/1130802621750448160/1143083373535973406
import { drizzle } from "drizzle-orm/postgres-js"; // import { drizzle } from "drizzle-orm/postgres-js";
import postgres from "postgres"; // import postgres from "postgres";
const connectionString = process.env.DATABASE_URL || ""; // const connectionString = process.env.DATABASE_URL || "";
const pg = postgres(connectionString, { max: 1 }); // const pg = postgres(connectionString, { max: 1 });
const db = drizzle(pg); // const db = drizzle(pg);
const clearDb = async (): Promise<void> => { // const clearDb = async (): Promise<void> => {
try { // try {
const tablesQuery = sql<string>`DROP SCHEMA public CASCADE; CREATE SCHEMA public; DROP schema drizzle CASCADE;`; // const tablesQuery = sql<string>`DROP SCHEMA public CASCADE; CREATE SCHEMA public; DROP schema drizzle CASCADE;`;
const tables = await db.execute(tablesQuery); // const tables = await db.execute(tablesQuery);
console.log(tables); // console.log(tables);
await pg.end(); // await pg.end();
} catch (error) { // } catch (error) {
console.error("Error to clean database", error); // console.error("Error to clean database", error);
} finally { // } finally {
} // }
}; // };
clearDb(); // clearDb();

View File

@@ -1,35 +1,35 @@
import bc from "bcrypt"; // import bc from "bcrypt";
import { drizzle } from "drizzle-orm/postgres-js"; // import { drizzle } from "drizzle-orm/postgres-js";
import postgres from "postgres"; // import postgres from "postgres";
import { users } from "./schema"; // import { users } from "./schema";
const connectionString = process.env.DATABASE_URL || ""; // const connectionString = process.env.DATABASE_URL || "";
const pg = postgres(connectionString, { max: 1 }); // const pg = postgres(connectionString, { max: 1 });
const db = drizzle(pg); // const db = drizzle(pg);
function password(txt: string) { // function password(txt: string) {
return bc.hashSync(txt, 10); // return bc.hashSync(txt, 10);
} // }
async function seed() { // async function seed() {
console.log("> Seed:", process.env.DATABASE_PATH, "\n"); // console.log("> Seed:", process.env.DATABASE_PATH, "\n");
// const authenticationR = await db // // const authenticationR = await db
// .insert(users) // // .insert(users)
// .values([ // // .values([
// { // // {
// email: "user1@hotmail.com", // // email: "user1@hotmail.com",
// password: password("12345671"), // // password: password("12345671"),
// }, // // },
// ]) // // ])
// .onConflictDoNothing() // // .onConflictDoNothing()
// .returning(); // // .returning();
// console.log("\nSemillas Update:", authenticationR.length); // // console.log("\nSemillas Update:", authenticationR.length);
} // }
seed().catch((e) => { // seed().catch((e) => {
console.error(e); // console.error(e);
process.exit(1); // process.exit(1);
}); // });