|
| 1 | +import { describe, it, expect, beforeEach, afterEach } from "vitest"; |
| 2 | +import { Knex, knex } from "knex"; |
| 3 | +import { SQlitePlayerConnectionHistoryRepository } from "./SQlitePlayerConnectionHistoryRepository"; |
| 4 | + |
| 5 | +describe("SQlitePlayerConnectionHistoryRepository", () => { |
| 6 | + let db: Knex; |
| 7 | + let repository: SQlitePlayerConnectionHistoryRepository; |
| 8 | + |
| 9 | + beforeEach(async () => { |
| 10 | + db = knex({ |
| 11 | + client: "sqlite3", |
| 12 | + connection: ":memory:", |
| 13 | + useNullAsDefault: true, |
| 14 | + }); |
| 15 | + |
| 16 | + await db.schema.createTable("player_connection_history", (table) => { |
| 17 | + table.increments("id").primary(); |
| 18 | + table.string("steam_id_3").notNullable(); |
| 19 | + table.string("ip_address").notNullable(); |
| 20 | + table.string("nickname").notNullable(); |
| 21 | + table.timestamp("timestamp").notNullable().defaultTo(db.fn.now()); |
| 22 | + }); |
| 23 | + |
| 24 | + repository = new SQlitePlayerConnectionHistoryRepository({ knex: db }); |
| 25 | + }); |
| 26 | + |
| 27 | + afterEach(async () => { |
| 28 | + await db.destroy(); |
| 29 | + }); |
| 30 | + |
| 31 | + describe("save", () => { |
| 32 | + it("should save player connection history", async () => { |
| 33 | + const result = await repository.save({ |
| 34 | + connectionHistory: { |
| 35 | + steamId3: "[U:1:12345678]", |
| 36 | + ipAddress: "192.168.1.1", |
| 37 | + nickname: "TestPlayer", |
| 38 | + }, |
| 39 | + }); |
| 40 | + |
| 41 | + expect(result).toMatchObject({ |
| 42 | + id: expect.any(Number), |
| 43 | + steamId3: "[U:1:12345678]", |
| 44 | + ipAddress: "192.168.1.1", |
| 45 | + nickname: "TestPlayer", |
| 46 | + }); |
| 47 | + }); |
| 48 | + }); |
| 49 | + |
| 50 | + describe("findBySteamId3", () => { |
| 51 | + it("should return empty array when no history found", async () => { |
| 52 | + const result = await repository.findBySteamId3({ |
| 53 | + steamId3: "[U:1:99999999]", |
| 54 | + }); |
| 55 | + |
| 56 | + expect(result).toEqual([]); |
| 57 | + }); |
| 58 | + |
| 59 | + it("should find connection history by steamId3", async () => { |
| 60 | + // Insert directly with explicit timestamps to ensure order |
| 61 | + await db("player_connection_history").insert({ |
| 62 | + steam_id_3: "[U:1:12345678]", |
| 63 | + ip_address: "192.168.1.1", |
| 64 | + nickname: "TestPlayer", |
| 65 | + timestamp: new Date("2024-01-01T10:00:00Z"), |
| 66 | + }); |
| 67 | + |
| 68 | + await db("player_connection_history").insert({ |
| 69 | + steam_id_3: "[U:1:12345678]", |
| 70 | + ip_address: "192.168.1.2", |
| 71 | + nickname: "TestPlayer2", |
| 72 | + timestamp: new Date("2024-01-01T11:00:00Z"), |
| 73 | + }); |
| 74 | + |
| 75 | + const result = await repository.findBySteamId3({ |
| 76 | + steamId3: "[U:1:12345678]", |
| 77 | + }); |
| 78 | + |
| 79 | + expect(result).toHaveLength(2); |
| 80 | + // Most recent first (11:00) |
| 81 | + expect(result[0]).toMatchObject({ |
| 82 | + steamId3: "[U:1:12345678]", |
| 83 | + ipAddress: "192.168.1.2", |
| 84 | + nickname: "TestPlayer2", |
| 85 | + timestamp: expect.any(Date), |
| 86 | + }); |
| 87 | + // Older second (10:00) |
| 88 | + expect(result[1]).toMatchObject({ |
| 89 | + steamId3: "[U:1:12345678]", |
| 90 | + ipAddress: "192.168.1.1", |
| 91 | + nickname: "TestPlayer", |
| 92 | + timestamp: expect.any(Date), |
| 93 | + }); |
| 94 | + }); |
| 95 | + |
| 96 | + it("should order results by timestamp descending (newest first)", async () => { |
| 97 | + // Insert directly with explicit timestamps |
| 98 | + await db("player_connection_history").insert({ |
| 99 | + steam_id_3: "[U:1:12345678]", |
| 100 | + ip_address: "192.168.1.1", |
| 101 | + nickname: "OlderConnection", |
| 102 | + timestamp: new Date("2024-01-01T10:00:00Z"), |
| 103 | + }); |
| 104 | + |
| 105 | + await db("player_connection_history").insert({ |
| 106 | + steam_id_3: "[U:1:12345678]", |
| 107 | + ip_address: "192.168.1.2", |
| 108 | + nickname: "NewerConnection", |
| 109 | + timestamp: new Date("2024-01-01T12:00:00Z"), |
| 110 | + }); |
| 111 | + |
| 112 | + const result = await repository.findBySteamId3({ |
| 113 | + steamId3: "[U:1:12345678]", |
| 114 | + }); |
| 115 | + |
| 116 | + expect(result[0].nickname).toBe("NewerConnection"); |
| 117 | + expect(result[1].nickname).toBe("OlderConnection"); |
| 118 | + }); |
| 119 | + |
| 120 | + it("should not return history for different steamId3", async () => { |
| 121 | + await repository.save({ |
| 122 | + connectionHistory: { |
| 123 | + steamId3: "[U:1:12345678]", |
| 124 | + ipAddress: "192.168.1.1", |
| 125 | + nickname: "Player1", |
| 126 | + }, |
| 127 | + }); |
| 128 | + |
| 129 | + await repository.save({ |
| 130 | + connectionHistory: { |
| 131 | + steamId3: "[U:1:87654321]", |
| 132 | + ipAddress: "192.168.1.2", |
| 133 | + nickname: "Player2", |
| 134 | + }, |
| 135 | + }); |
| 136 | + |
| 137 | + const result = await repository.findBySteamId3({ |
| 138 | + steamId3: "[U:1:12345678]", |
| 139 | + }); |
| 140 | + |
| 141 | + expect(result).toHaveLength(1); |
| 142 | + expect(result[0].steamId3).toBe("[U:1:12345678]"); |
| 143 | + }); |
| 144 | + }); |
| 145 | +}); |
0 commit comments