Skip to content

Commit 4c1e68b

Browse files
committed
chore: Remove deprecated env file and update key generation test setup
1 parent 1c97784 commit 4c1e68b

File tree

6 files changed

+340
-9
lines changed

6 files changed

+340
-9
lines changed

env

Lines changed: 0 additions & 7 deletions
This file was deleted.

server/db/database.test.ts

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import { assertEquals, assertExists } from "https://deno.land/[email protected]/testing/asserts.ts";
2+
import "../test_setup.ts";
3+
import { _R_getAllUsersData, _R_getAllUsersAvatar, _R_joinChat, _R_exitUserFromSocket, _R_deleteChatKey, _R_fileUploadAuth, type User, type Key } from "./database.ts";
4+
5+
// Mock data for testing
6+
const mockUser: User = {
7+
avatar: "Pikachu",
8+
uid: "test-user-id",
9+
publicKey: "test-public-key",
10+
joinedAt: Date.now()
11+
};
12+
13+
const mockKey: Key = {
14+
keyId: "AB-CDE-FG",
15+
activeUsers: 1,
16+
maxUsers: 2,
17+
admin: "test-admin-id",
18+
createdAt: Date.now()
19+
};
20+
21+
// Helper function to create test data
22+
async function setupTestChat() {
23+
await _R_joinChat(true, mockKey, mockUser);
24+
}
25+
26+
// Helper function to clean up test data
27+
async function cleanupTestChat() {
28+
await _R_deleteChatKey(mockKey.keyId, mockUser.uid);
29+
}
30+
31+
Deno.test({
32+
name: "Chat creation and user join test",
33+
async fn() {
34+
try {
35+
await setupTestChat();
36+
37+
// Test getting user data
38+
const users = await _R_getAllUsersData(mockKey.keyId);
39+
assertExists(users[mockUser.uid], "User should exist in chat");
40+
assertEquals(users[mockUser.uid].avatar, mockUser.avatar, "User avatar should match");
41+
assertEquals(users[mockUser.uid].publicKey, mockUser.publicKey, "User public key should match");
42+
} finally {
43+
await cleanupTestChat();
44+
}
45+
}
46+
});
47+
48+
Deno.test({
49+
name: "Get users avatar test",
50+
async fn() {
51+
try {
52+
await setupTestChat();
53+
54+
// Test getting user avatars
55+
const avatars = await _R_getAllUsersAvatar(mockKey.keyId);
56+
assertExists(avatars[mockUser.uid], "User avatar entry should exist");
57+
assertEquals(avatars[mockUser.uid].avatar, mockUser.avatar, "Avatar should match");
58+
} finally {
59+
await cleanupTestChat();
60+
}
61+
}
62+
});
63+
64+
Deno.test({
65+
name: "User exit test",
66+
async fn() {
67+
try {
68+
await setupTestChat();
69+
70+
// Test user exit
71+
await _R_exitUserFromSocket(mockKey.keyId, mockUser.uid);
72+
73+
// Verify user was removed
74+
const users = await _R_getAllUsersData(mockKey.keyId);
75+
assertEquals(Object.keys(users).length, 0, "No users should remain in chat");
76+
} finally {
77+
await cleanupTestChat();
78+
}
79+
}
80+
});
81+
82+
Deno.test({
83+
name: "File upload authorization test",
84+
async fn() {
85+
try {
86+
await setupTestChat();
87+
88+
// Test file upload authorization
89+
const [exists, activeUsers] = await _R_fileUploadAuth(mockKey.keyId, mockUser.uid) as [number, number];
90+
assertEquals(+exists, 1, "User should exist");
91+
assertEquals(+activeUsers, 1, "Should have one active user");
92+
} finally {
93+
await cleanupTestChat();
94+
}
95+
}
96+
});

server/libs/fileHandler.test.ts

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";
2+
import "../test_setup.ts";
3+
import { redis } from "../db/database.ts";
4+
5+
// Test constants
6+
const TEST_KEY = "AB-CDE-FG";
7+
const TEST_UID = "test-user-id";
8+
const TEST_MESSAGE_ID = "test-message-id";
9+
10+
// Helper function to setup test environment
11+
async function setupTestEnv() {
12+
// Setup mock chat data in Redis
13+
await redis.hset(`chat:${TEST_KEY}`, {
14+
maxUsers: 2,
15+
activeUsers: 2,
16+
admin: TEST_UID
17+
});
18+
await redis.hset(`uid:${TEST_UID}`, {
19+
avatar: "Pikachu",
20+
uid: TEST_UID
21+
});
22+
}
23+
24+
// Helper function to cleanup test environment
25+
async function cleanupTestEnv() {
26+
await redis.del(`chat:${TEST_KEY}`);
27+
await redis.del(`uid:${TEST_UID}`);
28+
await redis.del(`chat:${TEST_KEY}:file:${TEST_MESSAGE_ID}`);
29+
30+
try {
31+
await Deno.remove(`./uploads/${TEST_KEY}/${TEST_MESSAGE_ID}`);
32+
await Deno.remove(`./uploads/${TEST_KEY}`);
33+
} catch {
34+
// Ignore errors if files don't exist
35+
}
36+
}
37+
38+
Deno.test({
39+
name: "File upload test",
40+
async fn() {
41+
try {
42+
await setupTestEnv();
43+
44+
// Create a mock file
45+
const mockFile = new File(["test file content"], "test.txt", {
46+
type: "text/plain",
47+
});
48+
49+
// Create test directory
50+
await Deno.mkdir(`./uploads/${TEST_KEY}`, { recursive: true });
51+
52+
// Simulate file upload by writing the file
53+
const fileContent = await mockFile.arrayBuffer();
54+
await Deno.writeFile(
55+
`./uploads/${TEST_KEY}/${TEST_MESSAGE_ID}`,
56+
new Uint8Array(fileContent)
57+
);
58+
59+
// Verify file was written correctly
60+
const writtenContent = await Deno.readFile(`./uploads/${TEST_KEY}/${TEST_MESSAGE_ID}`);
61+
const decoder = new TextDecoder();
62+
assertEquals(
63+
decoder.decode(writtenContent),
64+
"test file content",
65+
"File content should match"
66+
);
67+
68+
// Verify file metadata was set correctly
69+
await redis.hset(`chat:${TEST_KEY}:file:${TEST_MESSAGE_ID}`, {
70+
maxDownload: 1,
71+
downloadCount: 0
72+
});
73+
74+
const [maxDownload, downloadCount] = await redis.hmget(
75+
`chat:${TEST_KEY}:file:${TEST_MESSAGE_ID}`,
76+
"maxDownload",
77+
"downloadCount"
78+
) as [string, string];
79+
80+
assertEquals(maxDownload, "1", "Max download should be set correctly");
81+
assertEquals(downloadCount, "0", "Download count should start at 0");
82+
83+
} finally {
84+
await cleanupTestEnv();
85+
}
86+
}
87+
});
88+
89+
Deno.test({
90+
name: "File upload authorization validation",
91+
async fn() {
92+
try {
93+
await setupTestEnv();
94+
95+
// Test file upload authorization
96+
const exists = await redis.exists(`uid:${TEST_UID}`);
97+
const activeUsers = await redis.hget(`chat:${TEST_KEY}`, "activeUsers");
98+
99+
assertEquals(exists, 1, "User should exist");
100+
assertEquals(activeUsers, "2", "Should have two active users");
101+
102+
} finally {
103+
await cleanupTestEnv();
104+
}
105+
}
106+
});
107+
108+
Deno.test({
109+
name: "File metadata storage test",
110+
async fn() {
111+
try {
112+
await setupTestEnv();
113+
114+
// Set file metadata
115+
await redis.hset(`chat:${TEST_KEY}:file:${TEST_MESSAGE_ID}`, {
116+
maxDownload: 1,
117+
downloadCount: 0
118+
});
119+
120+
// Verify metadata
121+
const [maxDownload, downloadCount] = await redis.hmget(
122+
`chat:${TEST_KEY}:file:${TEST_MESSAGE_ID}`,
123+
"maxDownload",
124+
"downloadCount"
125+
) as [string, string];
126+
127+
assertEquals(maxDownload, "1", "Max download should be 1");
128+
assertEquals(downloadCount, "0", "Download count should start at 0");
129+
130+
} finally {
131+
await cleanupTestEnv();
132+
}
133+
}
134+
});
135+
136+
Deno.test({
137+
name: "File cleanup after max downloads test",
138+
async fn() {
139+
try {
140+
await setupTestEnv();
141+
142+
// Create test directory and file
143+
await Deno.mkdir(`./uploads/${TEST_KEY}`, { recursive: true });
144+
await Deno.writeFile(
145+
`./uploads/${TEST_KEY}/${TEST_MESSAGE_ID}`,
146+
new TextEncoder().encode("test content")
147+
);
148+
149+
// Set metadata to trigger cleanup
150+
await redis.hset(`chat:${TEST_KEY}:file:${TEST_MESSAGE_ID}`, {
151+
maxDownload: 1,
152+
downloadCount: 1
153+
});
154+
155+
// Verify file exists
156+
const fileExists = await Deno.stat(`./uploads/${TEST_KEY}/${TEST_MESSAGE_ID}`)
157+
.then(() => true)
158+
.catch(() => false);
159+
160+
assertEquals(fileExists, true, "File should exist before cleanup");
161+
162+
// Cleanup should happen when download count reaches max
163+
await redis.del(`chat:${TEST_KEY}:file:${TEST_MESSAGE_ID}`);
164+
await Deno.remove(`./uploads/${TEST_KEY}/${TEST_MESSAGE_ID}`);
165+
166+
// Verify file is removed
167+
const fileExistsAfter = await Deno.stat(`./uploads/${TEST_KEY}/${TEST_MESSAGE_ID}`)
168+
.then(() => true)
169+
.catch(() => false);
170+
171+
assertEquals(fileExistsAfter, false, "File should be removed after cleanup");
172+
173+
} finally {
174+
await cleanupTestEnv();
175+
}
176+
}
177+
});

server/libs/keyGen.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";
2+
import "../test_setup.ts";
23
import { makeKey } from "./keyGen.ts";
34

45
//Deno test
56

67
Deno.test({
78
name: "Unique key generation test",
89
fn() {
9-
1010
const generatedKeys: string[] = [];
1111

1212
function getRandomKey() {
13-
1413
const key = makeKey();
1514
//check if key exists
1615
if (generatedKeys.includes(key)) {

server/libs/utils.test.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";
2+
import { validateKey, validatename, validateAll, avList } from "./utils.ts";
3+
4+
Deno.test({
5+
name: "Key validation tests",
6+
fn() {
7+
// Valid key format tests
8+
assertEquals(validateKey("AB-CDE-FG"), true, "Valid key format should pass");
9+
assertEquals(validateKey("12-345-67"), true, "Valid key with numbers should pass");
10+
assertEquals(validateKey("A1-B2C-D3"), true, "Valid key with mixed characters should pass");
11+
12+
// Invalid key format tests
13+
assertEquals(validateKey(""), false, "Empty key should fail");
14+
assertEquals(validateKey("AB-CDE"), false, "Incomplete key should fail");
15+
assertEquals(validateKey("ABCDEFGH"), false, "Key without hyphens should fail");
16+
assertEquals(validateKey("AB-CDEF-GH"), false, "Key with wrong segment lengths should fail");
17+
assertEquals(validateKey("AB-CDE-FGH"), false, "Key with extra characters should fail");
18+
assertEquals(validateKey("A@-CDE-FG"), false, "Key with special characters should fail");
19+
}
20+
});
21+
22+
Deno.test({
23+
name: "Avatar validation tests",
24+
fn() {
25+
// Test all predefined avatars
26+
for (const avatar of avList) {
27+
assertEquals(validatename(avatar), true, `Predefined avatar ${avatar} should be valid`);
28+
}
29+
30+
// Invalid avatar tests
31+
assertEquals(validatename(""), false, "Empty avatar name should fail");
32+
assertEquals(validatename("NonExistentPokemon"), false, "Non-existent Pokemon should fail");
33+
assertEquals(validatename("pikachu"), false, "Case-sensitive validation - lowercase should fail");
34+
assertEquals(validatename(" Pikachu "), false, "Avatar with whitespace should fail");
35+
}
36+
});
37+
38+
Deno.test({
39+
name: "Combined validation tests",
40+
fn() {
41+
// Valid combinations
42+
assertEquals(validateAll("AB-CDE-FG", "Pikachu"), true, "Valid key and avatar should pass");
43+
assertEquals(validateAll("12-345-67", "Mewtwo"), true, "Valid numeric key and avatar should pass");
44+
45+
// Invalid combinations
46+
assertEquals(validateAll("invalid-key", "Pikachu"), false, "Invalid key should fail");
47+
assertEquals(validateAll("AB-CDE-FG", "InvalidPokemon"), false, "Invalid avatar should fail");
48+
assertEquals(validateAll("invalid-key", "InvalidPokemon"), false, "Both invalid should fail");
49+
}
50+
});

server/test_setup.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Set up test environment variables if they don't exist
2+
const testEnv = {
3+
clienturl: "http://localhost:3000",
4+
host: "localhost",
5+
port: "6379",
6+
password: "",
7+
adminPasskey: "test-admin-key",
8+
devMode: "true"
9+
};
10+
11+
// Set environment variables if they don't exist
12+
Object.entries(testEnv).forEach(([key, value]) => {
13+
if (!Deno.env.get(key)) {
14+
Deno.env.set(key, value);
15+
}
16+
});

0 commit comments

Comments
 (0)