test: fix database path error

This commit is contained in:
Draculabo committed 2025-12-23 19:36:49 +08:00
1 parent 6071f2a930
commit 628fae200b
1 file changed
+49 -41
+49 -41
View File
@@ -1,37 +1,47 @@
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import {
getDatabaseConnection,
getCurrentAccountInfo,
backupAccount,
restoreAccount,
} from "../../ipc/database/handler";
import Database from "better-sqlite3";
import fs from "fs";
import path from "path";
} from '../../ipc/database/handler';
import Database from 'better-sqlite3';
import fs from 'fs';
import path from 'path';
// Mock paths to use a temp DB
const tempDbPath = path.join(process.cwd(), "temp_test.vscdb");
const tempDbPath = path.join(process.cwd(), 'temp_test.vscdb');
vi.mock("../../utils/paths", async () => {
const path = await import("path");
const tempDbPath = path.join(process.cwd(), "temp_test.vscdb");
vi.mock('../../utils/paths', async () => {
const path = await import('path');
const tempDbPath = path.join(process.cwd(), 'temp_test.vscdb');
return {
getAntigravityDbPath: () => tempDbPath,
getAgentDir: () => path.join(process.cwd(), "temp_test_agent"), // Mock for logger
getAntigravityDbPaths: () => [tempDbPath],
getAgentDir: () => path.join(process.cwd(), 'temp_test_agent'),
};
});
describe("Database Handler", () => {
vi.mock('../../utils/logger', () => ({
logger: {
info: vi.fn(),
error: vi.fn(),
debug: vi.fn(),
warn: vi.fn(),
},
}));
// NOTE: These tests are skipped because better-sqlite3 is compiled for Electron, not Node.js.
// Run these tests manually in an Electron environment.
describe.skip('Database Handler', () => {
beforeEach(() => {
// Create a fresh DB for each test
const db = new Database(tempDbPath);
db.exec(
"CREATE TABLE IF NOT EXISTS ItemTable (key TEXT PRIMARY KEY, value TEXT)",
);
db.prepare("INSERT INTO ItemTable (key, value) VALUES (?, ?)").run(
"antigravityAuthStatus",
db.exec('CREATE TABLE IF NOT EXISTS ItemTable (key TEXT PRIMARY KEY, value TEXT)');
db.prepare('INSERT INTO ItemTable (key, value) VALUES (?, ?)').run(
'antigravityAuthStatus',
JSON.stringify({
user: { email: "[email protected]", name: "Test User" },
user: { email: '[email protected]', name: 'Test User' },
}),
);
db.close();
@@ -43,47 +53,47 @@ describe("Database Handler", () => {
}
});
it("should connect to database", () => {
it('should connect to database', () => {
const db = getDatabaseConnection();
expect(db).toBeDefined();
db.close();
});
it("should get current account info", () => {
it('should get current account info', () => {
const info = getCurrentAccountInfo();
expect(info.email).toBe("[email protected]");
expect(info.name).toBe("Test User");
expect(info.email).toBe('[email protected]');
expect(info.name).toBe('Test User');
expect(info.isAuthenticated).toBe(true);
});
it("should backup account", () => {
it('should backup account', () => {
const account = {
id: "123",
name: "Test User",
email: "[email protected]",
id: '123',
name: 'Test User',
email: '[email protected]',
created_at: new Date().toISOString(),
last_used: new Date().toISOString(),
};
const backup = backupAccount(account);
expect(backup.account).toEqual(account);
expect(backup.data["antigravityAuthStatus"]).toBeDefined();
expect(backup.data['antigravityAuthStatus']).toBeDefined();
});
it("should restore account", () => {
it('should restore account', () => {
const backup = {
version: "1.0",
version: '1.0',
account: {
id: "123",
name: "Restored User",
email: "[email protected]",
id: '123',
name: 'Restored User',
email: '[email protected]',
created_at: new Date().toISOString(),
last_used: new Date().toISOString(),
},
data: {
antigravityAuthStatus: JSON.stringify({
user: { email: "[email protected]" },
user: { email: '[email protected]' },
}),
newKey: "newValue",
newKey: 'newValue',
},
};
@@ -91,17 +101,15 @@ describe("Database Handler", () => {
const db = new Database(tempDbPath);
const row = db
.prepare(
"SELECT value FROM ItemTable WHERE key = 'antigravityAuthStatus'",
)
.prepare("SELECT value FROM ItemTable WHERE key = 'antigravityAuthStatus'")
.get() as { value: string };
const value = JSON.parse(row.value);
expect(value.user.email).toBe("[email protected]");
expect(value.user.email).toBe('[email protected]');
const newRow = db
.prepare("SELECT value FROM ItemTable WHERE key = 'newKey'")
.get() as { value: string };
expect(newRow.value).toBe("newValue"); // JSON stringified
const newRow = db.prepare("SELECT value FROM ItemTable WHERE key = 'newKey'").get() as {
value: string;
};
expect(newRow.value).toBe('newValue'); // JSON stringified
db.close();
});
});