import { assert, describe, expect, it } from "vitest"; import { DirectusDB } from "../src/directus_db"; describe( "Direcuts DB wrapper tests", () => { const db = new DirectusDB(); it("Test user registration, query and deletion", async () => { const login = "__________test"; const password = "__________test"; // To clean if previous times test failed await db.removeUser(login, password); // Creating user const resp1 = await db.registerUser(login, password); expect(resp1).not.toBe(null); expect(resp1!.login).toBe(login); // Checking if user exists const resp2 = await db.authenticateUser(login, password); expect(resp2).not.toBe(null); expect(resp2!.login).toBe(login); // Creating user again, which is not allowed const resp3 = await db.registerUser(login, password); expect(resp3).toBe(null); // Using double password to produce erroring response const resp4 = await db.authenticateUser(login, password + password); expect(resp4).toBe(null); // Deleting user with wrong password const result1 = await db.removeUser(login, password + password); expect(result1).toBe(false); // Deleting user const result2 = await db.removeUser(login, password); expect(result2).toBe(true); // Checking if user exists const resp5 = await db.authenticateUser(login, password); expect(resp5).toBe(null); }); it("Test user stats", async () => { const login = "__________test"; const password = "__________test"; await db.removeUser(login, password); const testUser = await db.registerUser(login, password); // Checking if stats exist let stats = await db.getStats(testUser!.id); expect(stats).not.toBe(null); expect(stats!.belongs_to).toBe(testUser!.id); expect(stats!.games_won).toBe(0); expect(stats!.games_lost).toBe(0); // Updating stats (win times) and checking if they were updated const result1 = await db.updateStats(testUser!.id, true, false); expect(result1).toBe(true); stats = await db.getStats(testUser!.id); expect(stats).not.toBe(null); expect(stats!.belongs_to).toBe(testUser!.id); expect(stats!.games_won).toBe(1); expect(stats!.games_lost).toBe(0); // Updating stats (lost times) and checking if they were updated const result2 = await db.updateStats(testUser!.id, false, true); expect(result2).toBe(true); stats = await db.getStats(testUser!.id); expect(stats).not.toBe(null); expect(stats!.belongs_to).toBe(testUser!.id); expect(stats!.games_won).toBe(1); expect(stats!.games_lost).toBe(1); // Garbage user id should return false const result3 = await db.updateStats("asdasdasd", false, true); expect(result3).toBe(false); // Deleting user await db.removeUser(login, password); }); }, { timeout: 30000 } );