-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathsqlite-storage.test.ts
More file actions
139 lines (124 loc) · 4.59 KB
/
sqlite-storage.test.ts
File metadata and controls
139 lines (124 loc) · 4.59 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import { vi } from 'vitest'
import * as sqliteStorage from './sqlite-storage';
// Mock better-sqlite3 to prevent actual database operations
// In vitest v4, mocks used as constructors must use 'function' or 'class' syntax
vi.mock('better-sqlite3', () => {
const mockStatement = {
run: vi.fn(() => ({ lastInsertRowid: 1, changes: 1 })),
get: vi.fn(() => undefined),
all: vi.fn(() => []),
}
return {
default: vi.fn(function() {
return {
prepare: vi.fn(() => mockStatement),
exec: vi.fn(),
close: vi.fn(),
}
})
}
});
// Integration-style tests for SQLite storage
// These tests verify the public API without heavy mocking
describe('SQLite Storage', () => {
// Mock console.log to reduce noise during tests
beforeAll(() => {
vi.spyOn(console, 'log').mockImplementation(() => {});
vi.spyOn(console, 'error').mockImplementation(() => {});
});
afterAll(async () => {
vi.restoreAllMocks();
});
describe('Module exports', () => {
it('should export TicTacToe game functions', () => {
expect(typeof sqliteStorage.getTicTacToeGame).toBe('function');
expect(typeof sqliteStorage.setTicTacToeGame).toBe('function');
expect(typeof sqliteStorage.getAllTicTacToeGames).toBe('function');
expect(typeof sqliteStorage.deleteTicTacToeGame).toBe('function');
});
it('should export RPS game functions', () => {
expect(typeof sqliteStorage.getRPSGame).toBe('function');
expect(typeof sqliteStorage.setRPSGame).toBe('function');
expect(typeof sqliteStorage.getAllRPSGames).toBe('function');
expect(typeof sqliteStorage.deleteRPSGame).toBe('function');
});
});
describe('Function signatures', () => {
it('TicTacToe functions should have correct arity', () => {
expect(sqliteStorage.getTicTacToeGame.length).toBe(1);
expect(sqliteStorage.setTicTacToeGame.length).toBe(2);
expect(sqliteStorage.getAllTicTacToeGames.length).toBe(0);
expect(sqliteStorage.deleteTicTacToeGame.length).toBe(1);
});
it('RPS functions should have correct arity', () => {
expect(sqliteStorage.getRPSGame.length).toBe(1);
expect(sqliteStorage.setRPSGame.length).toBe(2);
expect(sqliteStorage.getAllRPSGames.length).toBe(0);
expect(sqliteStorage.deleteRPSGame.length).toBe(1);
});
});
describe('Return types', () => {
it('getTicTacToeGame should return a Promise', () => {
const result = sqliteStorage.getTicTacToeGame('test-id');
expect(result).toBeInstanceOf(Promise);
});
it('setTicTacToeGame should return a Promise', () => {
const mockGameSession = {
gameState: {
id: 'test',
players: [],
currentPlayerId: 'player1' as const,
status: 'playing' as const,
createdAt: new Date(),
updatedAt: new Date(),
board: [[null, null, null], [null, null, null], [null, null, null]],
playerSymbols: {}
},
gameType: 'tic-tac-toe' as const,
history: []
};
const result = sqliteStorage.setTicTacToeGame('test-id', mockGameSession);
expect(result).toBeInstanceOf(Promise);
});
it('getAllTicTacToeGames should return a Promise', () => {
const result = sqliteStorage.getAllTicTacToeGames();
expect(result).toBeInstanceOf(Promise);
});
it('deleteTicTacToeGame should return a Promise', () => {
const result = sqliteStorage.deleteTicTacToeGame('test-id');
expect(result).toBeInstanceOf(Promise);
});
it('getRPSGame should return a Promise', () => {
const result = sqliteStorage.getRPSGame('test-id');
expect(result).toBeInstanceOf(Promise);
});
it('setRPSGame should return a Promise', () => {
const mockGameSession = {
gameState: {
id: 'test',
players: [],
currentPlayerId: 'player1' as const,
status: 'playing' as const,
createdAt: new Date(),
updatedAt: new Date(),
rounds: [],
currentRound: 0,
scores: {},
maxRounds: 3
},
gameType: 'rock-paper-scissors' as const,
history: []
};
const result = sqliteStorage.setRPSGame('test-id', mockGameSession);
expect(result).toBeInstanceOf(Promise);
});
it('getAllRPSGames should return a Promise', () => {
const result = sqliteStorage.getAllRPSGames();
expect(result).toBeInstanceOf(Promise);
});
it('deleteRPSGame should return a Promise', () => {
const result = sqliteStorage.deleteRPSGame('test-id');
expect(result).toBeInstanceOf(Promise);
});
});
});