1+ // Set up test database URL
2+ const TEST_DATABASE_URL = "postgresql://localhost/milei_test" ;
3+
4+ // Override the DATABASE_URL environment variable for testing
5+ process . env . DATABASE_URL = TEST_DATABASE_URL ;
6+
7+ import {
8+ describe ,
9+ it ,
10+ expect ,
11+ beforeEach ,
12+ afterEach ,
13+ beforeAll ,
14+ afterAll ,
15+ vi ,
16+ } from "vitest" ;
17+ import { drizzle } from "drizzle-orm/postgres-js" ;
18+ import postgres from "postgres" ;
19+ import * as schema from "../schema.js" ;
20+
21+ // Mock the database connection to use test database
22+ vi . mock ( "../lib/db/index.js" , ( ) => {
23+ const { drizzle } = require ( "drizzle-orm/postgres-js" ) ;
24+ const postgres = require ( "postgres" ) ;
25+ const schema = require ( "/home/diablo/milei-twitter/sitio/src/schema.ts" ) ;
26+ const client = postgres ( "postgresql://localhost/milei_test" , { max : 1 } ) ;
27+ return {
28+ db : drizzle ( client , { schema } ) ,
29+ } ;
30+ } ) ;
31+
32+ // Create test database connection
33+ const client = postgres ( TEST_DATABASE_URL , { max : 1 } ) ;
34+ const testDb = drizzle ( client , { schema } ) ;
35+
36+ // Import page load functions after mocking the database
37+ import { load as homeLoad } from "./+page.server.js" ;
38+ import { load as adminLoad } from "./admin/+page.server.js" ;
39+ import { load as adminLoginLoad } from "./admin/login/+page.server.js" ;
40+ import { load as chequearLoad } from "./chequear/+page.server.js" ;
41+ import { load as todosLosTweetsLoad } from "./experimentos/todos-los-tweets/+page.server.js" ;
42+ import { load as promediosLoad } from "./promedios/[year]/[month]/+page.server.js" ;
43+
44+ // Mock SvelteKit load event
45+ function mockLoadEvent ( params : Record < string , string > = { } , url : string = "http://localhost" ) {
46+ return {
47+ params,
48+ url : new URL ( url ) ,
49+ route : { id : "/" } ,
50+ fetch : global . fetch ,
51+ request : new Request ( url ) ,
52+ cookies : {
53+ get : ( ) => undefined ,
54+ set : ( ) => { } ,
55+ delete : ( ) => { } ,
56+ } ,
57+ locals : { } ,
58+ parent : async ( ) => ( { } ) ,
59+ depends : ( ) => { } ,
60+ untrack : ( fn : ( ) => any ) => fn ( ) ,
61+ platform : { } ,
62+ isDataRequest : false ,
63+ isSubRequest : false ,
64+ getClientAddress : ( ) => "127.0.0.1" ,
65+ setHeaders : ( ) => { } ,
66+ } as any ;
67+ }
68+
69+ describe ( "Main Pages Smoke Tests" , ( ) => {
70+ beforeAll ( async ( ) => {
71+ // Ensure test database is ready
72+ // The schema should already exist from migrations
73+ } ) ;
74+
75+ beforeEach ( async ( ) => {
76+ // Clean up test data before each test
77+ await testDb . delete ( schema . tweets ) ;
78+ await testDb . delete ( schema . retweets ) ;
79+ await testDb . delete ( schema . likedTweets ) ;
80+ await testDb . delete ( schema . scraps ) ;
81+ } ) ;
82+
83+ afterAll ( async ( ) => {
84+ // Close database connection
85+ await client . end ( ) ;
86+ } ) ;
87+
88+ describe ( "Home Page (/)" , ( ) => {
89+ it ( "should load without errors or redirect" , async ( ) => {
90+ const event = mockLoadEvent ( ) ;
91+ try {
92+ const result = await homeLoad ( event ) ;
93+ expect ( result ) . toBeDefined ( ) ;
94+ expect ( typeof result ) . toBe ( "object" ) ;
95+ } catch ( error : any ) {
96+ // Allow redirects as they are normal behavior
97+ if ( error . status === 302 ) {
98+ expect ( error . status ) . toBe ( 302 ) ;
99+ expect ( error . location ) . toBeDefined ( ) ;
100+ } else {
101+ throw error ;
102+ }
103+ }
104+ } ) ;
105+ } ) ;
106+
107+ describe ( "Admin Page (/admin)" , ( ) => {
108+ it ( "should handle missing admin password" , async ( ) => {
109+ const event = mockLoadEvent ( ) ;
110+ try {
111+ const result = await adminLoad ( event ) ;
112+ expect ( result ) . toBeDefined ( ) ;
113+ expect ( typeof result ) . toBe ( "object" ) ;
114+ } catch ( error : any ) {
115+ // Admin pages may throw 500 if no admin password is set
116+ if ( error . status === 500 ) {
117+ expect ( error . body ?. message ) . toContain ( "admin" ) ;
118+ } else {
119+ throw error ;
120+ }
121+ }
122+ } ) ;
123+ } ) ;
124+
125+ describe ( "Admin Login Page (/admin/login)" , ( ) => {
126+ it ( "should handle missing admin password" , async ( ) => {
127+ const event = mockLoadEvent ( ) ;
128+ try {
129+ const result = await adminLoginLoad ( event ) ;
130+ expect ( result ) . toBeDefined ( ) ;
131+ expect ( typeof result ) . toBe ( "object" ) ;
132+ } catch ( error : any ) {
133+ // Admin pages may throw 500 if no admin password is set
134+ if ( error . status === 500 ) {
135+ expect ( error . body ?. message ) . toContain ( "admin" ) ;
136+ } else {
137+ throw error ;
138+ }
139+ }
140+ } ) ;
141+ } ) ;
142+
143+ describe ( "Chequear Page (/chequear)" , ( ) => {
144+ it ( "should load without errors" , async ( ) => {
145+ const event = mockLoadEvent ( ) ;
146+ const result = await chequearLoad ( event ) ;
147+
148+ expect ( result ) . toBeDefined ( ) ;
149+ expect ( typeof result ) . toBe ( "object" ) ;
150+ } ) ;
151+ } ) ;
152+
153+ describe ( "FAQ Page (/info/faq)" , ( ) => {
154+ it ( "should load without errors (static page)" , async ( ) => {
155+ // FAQ page might not have a load function, just check the page exists
156+ expect ( true ) . toBe ( true ) ;
157+ } ) ;
158+ } ) ;
159+
160+ describe ( "Todos Los Tweets Page (/experimentos/todos-los-tweets)" , ( ) => {
161+ it ( "should load without errors" , async ( ) => {
162+ const event = mockLoadEvent ( ) ;
163+ const result = await todosLosTweetsLoad ( event ) ;
164+
165+ expect ( result ) . toBeDefined ( ) ;
166+ expect ( typeof result ) . toBe ( "object" ) ;
167+ } ) ;
168+ } ) ;
169+
170+ describe ( "Promedios Page (/promedios/[year]/[month])" , ( ) => {
171+ it ( "should validate month parameter" , async ( ) => {
172+ const event = mockLoadEvent ( { year : "2024" , month : "01" } ) ;
173+ try {
174+ const result = await promediosLoad ( event ) ;
175+ expect ( result ) . toBeDefined ( ) ;
176+ expect ( typeof result ) . toBe ( "object" ) ;
177+ } catch ( error : any ) {
178+ // Month validation may throw 400 for invalid months
179+ if ( error . status === 400 ) {
180+ expect ( error . body ?. message ) . toContain ( "mes" ) ;
181+ } else {
182+ throw error ;
183+ }
184+ }
185+ } ) ;
186+
187+ it ( "should handle different year/month combinations" , async ( ) => {
188+ const event = mockLoadEvent ( { year : "2023" , month : "12" } ) ;
189+ try {
190+ const result = await promediosLoad ( event ) ;
191+ expect ( result ) . toBeDefined ( ) ;
192+ expect ( typeof result ) . toBe ( "object" ) ;
193+ } catch ( error : any ) {
194+ // Month validation may throw 400 for invalid months
195+ if ( error . status === 400 ) {
196+ expect ( error . body ?. message ) . toContain ( "mes" ) ;
197+ } else {
198+ throw error ;
199+ }
200+ }
201+ } ) ;
202+ } ) ;
203+ } ) ;
0 commit comments