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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
//! Test that verifies Database.db pointer is NOT nulled in Drop
//! This prevents "Database connection is null" errors when JavaScript
//! still holds references after Rust Drop runs
#[cfg(target_arch = "wasm32")]
mod wasm_tests {
use absurder_sql::{Database, DatabaseConfig};
use wasm_bindgen_test::*;
wasm_bindgen_test_configure!(run_in_browser);
#[wasm_bindgen_test]
async fn test_drop_does_not_null_db_pointer() {
let db_name = format!("drop_ptr_test_{}", js_sys::Date::now() as u64);
// This test documents that Drop SHOULD NOT null self.db pointer
// because JavaScript may still have a reference to the Database struct via wasm-bindgen
//
// The PWA E2E tests demonstrate the actual problem:
// 1. JavaScript stores Database in window.testDb
// 2. Rust scope ends, Drop runs
// 3. If Drop sets self.db = null_mut(), then JavaScript calls testDb.execute()
// 4. Result: "Database connection is null" error
//
// This test verifies the fix works by ensuring all WASM tests still pass
// The real validation is in the PWA E2E tests
let mut config = DatabaseConfig::default();
config.name = db_name.clone();
let mut db = Database::new(config)
.await
.expect("Failed to create database");
db.execute("CREATE TABLE test (id INTEGER PRIMARY KEY)")
.await
.expect("Failed to create table");
// Test passes if we get here without panic
drop(db);
Database::delete_database(format!("{}.db", db_name))
.await
.ok();
}
#[wasm_bindgen_test]
async fn test_database_usable_after_drop_scope() {
let db_name = format!("drop_ptr_test_{}", js_sys::Date::now() as u64);
// Create database and perform initial operation
let mut config = DatabaseConfig::default();
config.name = db_name.clone();
let mut db = Database::new(config)
.await
.expect("Failed to create database");
db.execute("CREATE TABLE test (id INTEGER PRIMARY KEY, value TEXT)")
.await
.expect("Failed to create table");
// Simulate JavaScript keeping reference while Rust scope ends
// In PWA, JavaScript stores Database in window.testDb
// When Rust variables go out of scope, Drop runs
// But JavaScript still has the reference and can call methods
// Store in "JavaScript" (simulate by keeping db alive)
let mut js_db = db;
// In real scenario, Drop would run here if we let db go out of scope
// But JavaScript would still call methods on the stored reference
// This should work - database connection should still be valid
let result = js_db
.execute("INSERT INTO test (value) VALUES ('after_drop')")
.await;
// RED TEST: This currently fails with "Database connection is null"
// because Drop sets self.db = std::ptr::null_mut()
assert!(
result.is_ok(),
"Database should still be usable after Drop scope"
);
// Verify data was inserted
let rows = js_db
.query("SELECT value FROM test")
.await
.expect("Query should work");
assert_eq!(rows.len(), 1, "Should have 1 row");
// Cleanup
drop(js_db);
Database::delete_database(format!("{}.db", db_name))
.await
.ok();
}
#[wasm_bindgen_test]
async fn test_multiple_database_instances_dont_null_each_other() {
let db_name = format!("multi_drop_test_{}", js_sys::Date::now() as u64);
// Create first database instance
let mut config = DatabaseConfig::default();
config.name = db_name.clone();
let mut db1 = Database::new(config).await.expect("Failed to create db1");
db1.execute("CREATE TABLE test (id INTEGER PRIMARY KEY)")
.await
.expect("Failed to create table");
// Create second instance to same database (shares BlockStorage)
let mut config = DatabaseConfig::default();
config.name = db_name.clone();
let mut db2 = Database::new(config).await.expect("Failed to create db2");
// Drop first instance (simulates JavaScript losing reference)
drop(db1);
// RED TEST: db2 should still work, but currently fails if Drop nulls the pointer
let result = db2.execute("INSERT INTO test DEFAULT VALUES").await;
assert!(result.is_ok(), "db2 should still work after db1 drops");
// Cleanup
drop(db2);
Database::delete_database(format!("{}.db", db_name))
.await
.ok();
}
#[wasm_bindgen_test]
async fn test_database_query_after_explicit_operations() {
let db_name = format!("query_after_ops_{}", js_sys::Date::now() as u64);
let mut config = DatabaseConfig::default();
config.name = db_name.clone();
let mut db = Database::new(config)
.await
.expect("Failed to create database");
// Perform multiple operations
db.execute("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)")
.await
.expect("Create table failed");
db.execute("INSERT INTO users (name) VALUES ('Alice')")
.await
.expect("Insert failed");
// Query should work - this is the pattern PWA tests use
let rows = db.query("SELECT * FROM users").await;
// RED TEST: Currently fails with "Database connection is null" in PWA tests
assert!(
rows.is_ok(),
"Query should work after operations: {:?}",
rows.err()
);
let rows = rows.unwrap();
assert_eq!(rows.len(), 1, "Should have 1 user");
// Cleanup
drop(db);
Database::delete_database(format!("{}.db", db_name))
.await
.ok();
}
}