const path = require('path') const fs = require('fs') const assert = require('assert') const crypto = require('crypto') const { initCrypto, sign, verify, enc, dec, hashPassword } = require('./weekly_crypto') const { initDB, getDB } = require('./weekly_db') const core = require('./weekly_core') const integrations = require('./weekly_integrations') const TEST_DB = path.join(__dirname, 'test_weekly.db') const TEST_ENC = path.join(__dirname, 'test_weekly.enc') // Cleanup if (fs.existsSync(TEST_DB)) fs.unlinkSync(TEST_DB) if (fs.existsSync(TEST_ENC)) fs.unlinkSync(TEST_ENC) console.log('--- Starting Weekly Module Tests ---') // 1. Crypto Test console.log('[1/5] Testing Crypto...') initCrypto('testpass', 'testsalt') const payload = { foo: 'bar' } const sig = sign(payload) assert.strictEqual(verify(payload, sig), true, 'Signature verification failed') assert.strictEqual(verify(payload, 'wrong'), false, 'Signature rejection failed') const secret = 'hello world' const e = enc(secret) const d = dec(e.ct, e.iv, e.tag) assert.strictEqual(d, secret, 'Encryption/Decryption failed') console.log(' Crypto OK') // 2. DB Init console.log('[2/5] Testing DB Init...') initDB(TEST_DB, TEST_ENC) const db = getDB() const tables = db.prepare(`SELECT name FROM sqlite_master WHERE type='table'`).all() const expectedTables = ['users', 'reports', 'events', 'event_reviews', 'attachments', 'versions', 'logs', 'event_external', 'event_relations', 'email_sends'] expectedTables.forEach(t => { assert.ok(tables.find(x => x.name === t), `Table ${t} missing`) }) console.log(' DB Init OK') // 3. Core Logic console.log('[3/5] Testing Core Logic...') // User core.ensureDefaultUser() const loginRes = core.login('admin_wy', process.env.WEEKLY_DEFAULT_PASSWORD || process.env.LOCK_DEFAULT_PASSWORD || '') assert.strictEqual(loginRes.ok, true, 'Default user login failed') // Report const weekId = '2025-W01' const rId = core.ensureReportForWeek(weekId) const report = core.getReportByWeek(weekId) assert.strictEqual(report.week_id, weekId, 'Report creation failed') // Event const eId = core.createEvent({ report_id: rId, week_id: weekId, title: 'Test Event', content: 'Test Content', status_code: 'doing', priority: 1 }) const events = core.listEvents(rId) assert.strictEqual(events.length, 1, 'Event listing failed') assert.strictEqual(events[0].title, 'Test Event', 'Event content mismatch') // Update Event core.updateEvent({ id: eId, status_code: 'done' }) const events2 = core.listEvents(rId) assert.strictEqual(events2[0].status_code, 'done', 'Event update failed') // Search // Need to wait for index rebuild? It's async in scheduleMaintain but synchronous in initial load. // core.js rebuildIndex is not exported directly but called inside module? // Actually I exported it. core.rebuildIndex() const searchRes = core.search('Test') console.log('Search Results:', JSON.stringify(searchRes, null, 2)) assert.strictEqual(searchRes.filter(x => x.type === 'event').length, 1, 'Search failed') console.log(' Core Logic OK') // 4. Integrations console.log('[4/5] Testing Integrations...') // Just check if functions exist assert.strictEqual(typeof integrations.pushCalendarByEventId, 'function') assert.strictEqual(typeof integrations.summarizeText, 'function') console.log(' Integrations Interface OK') // 5. Cleanup if (db && typeof db.close === 'function') db.close() if (fs.existsSync(TEST_DB)) fs.unlinkSync(TEST_DB) if (fs.existsSync(TEST_ENC)) fs.unlinkSync(TEST_ENC) console.log('--- All Tests Passed ---')