import { test, expect } from "bun:test" import * as db from "./database.ts" import _, { rearg } from "lodash" import { None, Ok, Option, Some } from "ts-results-es" // Test basic function for database test("DataBase", () => { const allTables = db.allTables() expect(allTables).toBeArray() expect(allTables).toEqual(["Users", "Boards"]) expect(db.BoardTable.countAll()).toBe(0) expect(db.UserTable.countAll()).toBe(0) }) // Test Boards table function const boardsNumber = 10 const rooms = ["A1", "A1", "A1", "A1", "A1", "A2", "A2", "A2", "A2", "A2"] test("Find something empty", () => { const findEmptyByID = db.BoardTable.find(_.random(0, boardsNumber)) expect(findEmptyByID).toEqual(Ok(None)) const findEmptyByName = db.BoardTable.find("Hello", "World") expect(findEmptyByName).toEqual(Ok(None)) }) test("Add some boards", () => { const boardsArray: Array = [] for (let i = 0; i < boardsNumber; i++) { boardsArray.push({ id: i, name: `Board ${i}`, room: rooms[i], ipv4: `192.168.172.${i}`, port: i, }) } const retAdd = db.BoardTable.addFromArray(boardsArray) const isAddOk = retAdd.isOk() expect(isAddOk).toBeTrue() }) test("Get boards table column unique element", () => { expect(db.BoardTable.rooms().unwrap()).toEqual(["A1", "A2"]) }) test("Find something from boards table", () => { expect(db.BoardTable.find(1)).toEqual(Ok(Some({ id: 1, name: `Board ${1}`, room: rooms[1], ipv4: `192.168.172.${1}`, port: 1, } as db.Board))) expect(db.BoardTable.find("Board 3", "A1")).toEqual(Ok(Some({ id: 3, name: `Board ${3}`, room: rooms[3], ipv4: `192.168.172.${3}`, port: 3, } as db.Board))) }) test("Get count of elements from boards table", () => { expect(db.BoardTable.countByName("Board 1")).toBe(1) expect(db.BoardTable.countByRoom("A1")).toBe(5) })