Repository navigation
Expand file tree
/
Copy pathtest-ipc.js
More file actions
178 lines (150 loc) · 5.66 KB
/
Copy pathtest-ipc.js
File metadata and controls
178 lines (150 loc) · 5.66 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
/**
* 综合功能测试脚本
* 在 Electron 主进程中运行,测试所有 IPC 通信
*/
const initSqlJs = require('sql.js');
const path = require('path');
const fs = require('fs');
const dbPath = path.join(process.env.APPDATA, 'project-delivery-tool', 'delivery.db');
const testResults = {
pass: [],
fail: [],
errors: []
};
function test(name, fn) {
try {
const result = fn();
if (result === true) {
testResults.pass.push(name);
console.log(`✓ ${name}`);
} else {
testResults.fail.push(name);
console.log(`✗ ${name}: ${result}`);
}
} catch (error) {
testResults.errors.push({ name, error: error.message });
console.log(`✗ ${name}: ${error.message}`);
}
}
async function runTests() {
console.log('='.repeat(60));
console.log(' 综合功能测试 - IPC 通信测试');
console.log('='.repeat(60));
console.log();
let SQL, db;
try {
SQL = await initSqlJs();
const fileBuffer = fs.readFileSync(dbPath);
db = new SQL.Database(fileBuffer);
console.log('数据库加载成功');
} catch (error) {
console.log(`数据库加载失败:${error.message}`);
return;
}
console.log();
console.log('--- 数据库操作测试 ---');
// Test 1: 查询项目
test('查询项目列表', () => {
const projects = db.exec('SELECT * FROM projects ORDER BY id DESC');
return projects[0]?.values.length >= 0 ? true : '无数据';
});
// Test 2: 查询任务
test('查询任务列表', () => {
const tasks = db.exec('SELECT * FROM tasks ORDER BY id DESC');
return tasks[0]?.values.length >= 0 ? true : '无数据';
});
// Test 3: 查询成员
test('查询成员列表', () => {
const members = db.exec('SELECT * FROM members ORDER BY id DESC');
return members[0]?.values.length >= 0 ? true : '无数据';
});
// Test 4: 新建项目
test('新建项目', () => {
const testName = `测试项目_${Date.now()}`;
db.run('INSERT INTO projects (name, client, start_date, end_date, status, budget_hours) VALUES (?, ?, ?, ?, ?, ?)',
[testName, '测试客户', '2026-03-20', '2026-04-20', 'planning', 100]);
const result = db.exec(`SELECT id FROM projects WHERE name = '${testName}'`);
if (result[0]?.values.length > 0) {
const id = result[0].values[0][0];
db.run('DELETE FROM projects WHERE id = ?', [id]);
return true;
}
return '插入失败';
});
// Test 5: 更新项目
test('更新项目', () => {
const testName = `更新测试_${Date.now()}`;
db.run('INSERT INTO projects (name, client, start_date, end_date, status, budget_hours) VALUES (?, ?, ?, ?, ?, ?)',
[testName, '测试客户', '2026-03-20', '2026-04-20', 'planning', 100]);
const result = db.exec(`SELECT id FROM projects WHERE name = '${testName}'`);
if (result[0]?.values.length > 0) {
const id = result[0].values[0][0];
db.run('UPDATE projects SET name = ? WHERE id = ?', ['新名称', id]);
const updated = db.exec(`SELECT name FROM projects WHERE id = ${id}`);
db.run('DELETE FROM projects WHERE id = ?', [id]);
return updated[0]?.values[0]?.[0] === '新名称' ? true : '更新后验证失败';
}
return '插入失败';
});
// Test 6: 删除项目
test('删除项目', () => {
const testName = `删除测试_${Date.now()}`;
db.run('INSERT INTO projects (name, client, start_date, end_date, status, budget_hours) VALUES (?, ?, ?, ?, ?, ?)',
[testName, '测试客户', '2026-03-20', '2026-04-20', 'planning', 100]);
const result = db.exec(`SELECT id FROM projects WHERE name = '${testName}'`);
if (result[0]?.values.length > 0) {
const id = result[0].values[0][0];
db.run('DELETE FROM projects WHERE id = ?', [id]);
const afterDelete = db.exec(`SELECT id FROM projects WHERE id = ${id}`);
return afterDelete[0]?.values.length === 0 ? true : '删除后仍有数据';
}
return '插入失败';
});
// Test 7: 查询项目相关任务
test('按项目 ID 查询任务', () => {
const tasks = db.exec('SELECT * FROM tasks WHERE project_id = 3 ORDER BY id DESC');
return true; // 即使没有数据也不报错
});
// Test 8: 检查任务层级关系
test('任务层级关系', () => {
const query = `
SELECT t.id, t.name, t.parent_id,
CASE WHEN t.parent_id IS NOT NULL AND p.id IS NULL THEN 'orphan' ELSE 'ok' END as status
FROM tasks t
LEFT JOIN tasks p ON t.parent_id = p.id
WHERE t.parent_id IS NOT NULL AND p.id IS NULL
`;
const orphans = db.exec(query);
return orphans[0]?.values.length === 0 ? true : `发现 ${orphans[0].values.length} 个孤立任务`;
});
// Test 9: 数据库保存测试
test('数据库导出保存', () => {
const data = db.export();
const buffer = Buffer.from(data);
const testPath = dbPath + '.test';
fs.writeFileSync(testPath, buffer);
const exists = fs.existsSync(testPath);
if (exists) fs.unlinkSync(testPath);
return exists ? true : '文件保存失败';
});
db.close();
console.log();
console.log('='.repeat(60));
console.log(' 测试结果汇总');
console.log('='.repeat(60));
console.log(`通过:${testResults.pass.length}`);
console.log(`失败:${testResults.fail.length}`);
console.log(`错误:${testResults.errors.length}`);
if (testResults.fail.length > 0) {
console.log();
console.log('失败项目:');
testResults.fail.forEach(f => console.log(` - ${f}`));
}
if (testResults.errors.length > 0) {
console.log();
console.log('错误详情:');
testResults.errors.forEach(e => console.log(` - ${e.name}: ${e.error}`));
}
console.log('='.repeat(60));
}
runTests().catch(console.error);