summaryrefslogtreecommitdiff
path: root/test/database.test.ts
blob: d0a76c73662d1fff82bde1b3a0ba495d990fe13b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import { describe, test, expect } from "vitest";
import { db } from "../src/db/db";
import { users, linkedDevices, deviceConfig, alerts } from "../src/db/schema";
import { eq } from "drizzle-orm";

describe("Database Operations", () => {
  test("should retrieve test user from database", async () => {
    const testUsers = await db
      .select()
      .from(users)
      .where(eq(users.email, "test@example.com"));

    expect(testUsers).toHaveLength(1);
    expect(testUsers[0]?.email).toBe("test@example.com");
    expect(testUsers[0]?.emailVerified).toBe(true);
  });

  test("should retrieve test device from database", async () => {
    const devices = await db.select().from(linkedDevices);

    expect(devices).toHaveLength(1);
    expect(devices[0]?.nickname).toBe("Test Device");
    expect(devices[0]?.parentId).toBe(1);
  });

  test("should create new user", async () => {
    const newUser = await db
      .insert(users)
      .values({
        email: "newuser@example.com",
        password: "hashedpassword",
        emailVerified: false,
        emailCode: "654321",
        pushTokens: ["token1", "token2"],
      })
      .returning();

    expect(newUser).toHaveLength(1);
    expect(newUser[0]?.email).toBe("newuser@example.com");
    expect(newUser[0]?.pushTokens).toEqual(["token1", "token2"]);
  });

  test("should create device config", async () => {
    const config = await db
      .insert(deviceConfig)
      .values({
        deviceId: 1,
        disableBuddy: false,
        blockAdultSites: true,
        familyLinkAntiCircumvention: true,
        newContactAlerts: true,
        blockStrangers: false,
        notifyDangerousMessages: true,
        notifyNewContactAdded: true,
      })
      .returning();

    expect(config).toHaveLength(1);
    expect(config[0]?.deviceId).toBe(1);
    expect(config[0]?.familyLinkAntiCircumvention).toBe(true);
  });

  test("should create alert", async () => {
    const alert = await db
      .insert(alerts)
      .values({
        deviceId: 1,
        parentId: 1,
        category: "test",
        title: "Test Alert",
        message: "This is a test alert",
        summary: "Test alert summary",
        confidence: 95,
        packageName: "com.test.app",
        timestamp: Math.floor(Date.now() / 1000),
        read: false,
      })
      .returning();

    expect(alert).toHaveLength(1);
    expect(alert[0]?.title).toBe("Test Alert");
    expect(alert[0]?.confidence).toBe(95);
  });

  test("should update device last online", async () => {
    const newTimestamp = Math.floor(Date.now() / 1000);

    await db
      .update(linkedDevices)
      .set({ lastOnline: newTimestamp })
      .where(eq(linkedDevices.id, 1));

    const device = await db
      .select()
      .from(linkedDevices)
      .where(eq(linkedDevices.id, 1))
      .limit(1);

    expect(device[0]?.lastOnline).toBe(newTimestamp);
  });
});