/** * Database storage layer tests */ import { describe, test, expect, beforeEach, afterEach } from "bun:test"; import { database } from "../../../src/storage/database.js"; import { createTempDir } from "../../helpers/test-utils.js"; import { setupTestDatabase } from "../../helpers/database-helper.js"; import { testCodeSnippet, testNote, testTask, testBabyMilestone, testMathResource, testGameWishlist, } from "../../fixtures/test-data.js"; describe("Database", () => { let testContext: ReturnType; let cleanupDb: () => void; beforeEach(() => { testContext = createTempDir(); cleanupDb = setupTestDatabase(testContext); }); afterEach(() => { cleanupDb(); testContext.cleanup(); }); describe("Code Snippets", () => { test("should save and retrieve code snippet", () => { database.saveCodeSnippet(testCodeSnippet); const snippet = database.getCodeSnippet(testCodeSnippet.id); expect(snippet).toBeDefined(); expect(snippet?.title).toBe(testCodeSnippet.title); expect(snippet?.code).toBe(testCodeSnippet.code); expect(snippet?.language).toBe(testCodeSnippet.language); }); test("should list all code snippets", () => { database.saveCodeSnippet(testCodeSnippet); const snippets = database.getCodeSnippets(); expect(snippets.length).toBeGreaterThan(0); expect(snippets.find((s) => s.id === testCodeSnippet.id)).toBeDefined(); }); test("should search code snippets", () => { database.saveCodeSnippet(testCodeSnippet); const results = database.searchCodeSnippets("Test"); expect(results.length).toBeGreaterThan(0); expect(results[0].title).toContain("Test"); }); test("should search code snippets by tags", () => { database.saveCodeSnippet(testCodeSnippet); const results = database.searchCodeSnippets("", ["test"]); expect(results.length).toBeGreaterThan(0); expect(results[0].tags).toContain("test"); }); test("should delete code snippet", () => { database.saveCodeSnippet(testCodeSnippet); const deleted = database.deleteCodeSnippet(testCodeSnippet.id); expect(deleted).toBe(true); expect(database.getCodeSnippet(testCodeSnippet.id)).toBeUndefined(); }); test("should update existing code snippet", () => { database.saveCodeSnippet(testCodeSnippet); const updated = { ...testCodeSnippet, title: "Updated Title", }; database.saveCodeSnippet(updated); const snippet = database.getCodeSnippet(testCodeSnippet.id); expect(snippet?.title).toBe("Updated Title"); }); }); describe("Notes", () => { test("should save and retrieve note", () => { database.saveNote(testNote); const note = database.getNote(testNote.id); expect(note).toBeDefined(); expect(note?.title).toBe(testNote.title); expect(note?.content).toBe(testNote.content); }); test("should list all notes", () => { database.saveNote(testNote); const notes = database.getNotes(); expect(notes.length).toBeGreaterThan(0); expect(notes.find((n) => n.id === testNote.id)).toBeDefined(); }); test("should search notes", () => { database.saveNote(testNote); const results = database.searchNotes("Test"); expect(results.length).toBeGreaterThan(0); expect(results[0].title).toContain("Test"); }); test("should delete note", () => { database.saveNote(testNote); const deleted = database.deleteNote(testNote.id); expect(deleted).toBe(true); expect(database.getNote(testNote.id)).toBeUndefined(); }); }); describe("Tasks", () => { test("should save and retrieve task", () => { database.saveTask(testTask); const task = database.getTask(testTask.id); expect(task).toBeDefined(); expect(task?.title).toBe(testTask.title); expect(task?.completed).toBe(false); }); test("should list all tasks", () => { database.saveTask(testTask); const tasks = database.getTasks(); expect(tasks.length).toBeGreaterThan(0); expect(tasks.find((t) => t.id === testTask.id)).toBeDefined(); }); test("should filter tasks by completion status", () => { database.saveTask(testTask); const completedTask = { ...testTask, id: "task-2", completed: true }; database.saveTask(completedTask); const pendingTasks = database.getTasks(false); const completedTasks = database.getTasks(true); expect(pendingTasks.length).toBeGreaterThan(0); expect(completedTasks.length).toBeGreaterThan(0); expect(pendingTasks.find((t) => t.id === testTask.id)).toBeDefined(); expect(completedTasks.find((t) => t.id === "task-2")).toBeDefined(); }); }); describe("Baby Milestones", () => { test("should save and retrieve baby milestone", () => { database.saveBabyMilestone(testBabyMilestone); const milestones = database.getBabyMilestones(); expect(milestones.length).toBeGreaterThan(0); expect(milestones.find((m) => m.id === testBabyMilestone.id)).toBeDefined(); }); }); describe("Math Resources", () => { test("should save and retrieve math resource", () => { database.saveMathResource(testMathResource); const resources = database.getMathResources(); expect(resources.length).toBeGreaterThan(0); expect(resources.find((r) => r.id === testMathResource.id)).toBeDefined(); }); test("should search math resources", () => { database.saveMathResource(testMathResource); const results = database.searchMathResources("Addition"); expect(results.length).toBeGreaterThan(0); expect(results[0].title).toContain("Addition"); }); test("should filter math resources by grade", () => { database.saveMathResource(testMathResource); const results = database.searchMathResources("", "1st"); expect(results.length).toBeGreaterThan(0); expect(results[0].grade).toBe("1st"); }); }); describe("Game Wishlist", () => { test("should save and retrieve game wishlist", () => { database.saveGameWishlist(testGameWishlist); const games = database.getGameWishlist(); expect(games.length).toBeGreaterThan(0); expect(games.find((g) => g.id === testGameWishlist.id)).toBeDefined(); }); test("should delete game from wishlist", () => { database.saveGameWishlist(testGameWishlist); const deleted = database.deleteGameWishlist(testGameWishlist.id); expect(deleted).toBe(true); const games = database.getGameWishlist(); expect(games.find((g) => g.id === testGameWishlist.id)).toBeUndefined(); }); }); });