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+ } ) ;
0 commit comments