Files
daywork/intern-log/repro/sync-repro.ts
T

56 lines
2.5 KiB
TypeScript

import 'fake-indexeddb/auto'
import { bindDatabase, WorklogDB } from '../src/db/schema'
async function main() {
bindDatabase(new WorklogDB('repro_db'))
const mod = await import('../src/db/schema')
const db = mod.db
await db.project.bulkPut([{ name: 'P1', archived: 0, created_at: '2025-01-01T00:00:00' }])
await db.work_log.bulkPut([{ date: '2025-01-02', created_at: '2025-01-02T00:00:00' }])
await db.setting.bulkPut([
{ key: 'work_start', value: '09:00' },
{ key: 'sync_base_url', value: 'http://127.0.0.1:3780' }
])
await db.attendance.bulkPut([{ date: '2025-01-02', clock_in_at: '2025-01-02T09:00:00', clock_out_at: '2025-01-02T18:00:00', kind: 'normal', note: null, created_at: '2025-01-02T09:00:00' }])
await db.table('transaction').bulkPut([{ type: 'expense', category: '餐饮', amount: 2500, date: '2025-01-02', note: 'lunch', reimbursable: 1, reimbursed: 0, created_at: '2025-01-02T12:00:00' }])
const engine = await import('../src/sync/engine')
engine.enableRealtimeSync()
await fetch('http://127.0.0.1:3780/api/auth/register', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ username: 'rt', password: 'rt123456' })
}).catch(() => { /* 已注册则忽略 */ })
const r1 = await engine.runSync({ baseUrl: 'http://127.0.0.1:3780', username: 'rt', password: 'rt123456' })
console.log('SYNC1 OK:', JSON.stringify(r1))
// 触发本地变更,走实时推送路径
await db.table('log_item').add({
work_log_id: 1, content: '实时测试条目', project_id: null, category: 'dev',
minutes: null, is_milestone: 0, needs_followup: 0, takeaway: null,
remark: null, sort_order: 0, created_at: '2025-01-02T10:00:00'
})
await new Promise(r => setTimeout(r, 4500))
console.log('REALTIME PATH DONE')
// 二次同步幂等验证
const r2 = await engine.runSync({ baseUrl: 'http://127.0.0.1:3780', username: 'rt', password: 'rt123456' })
console.log('SYNC2 OK:', JSON.stringify(r2))
// 记账加载路径验证(与 LedgerView 相同的查询)
const ledger = await import('./db/repo')
const ledgerRows = await ledger.listTransactionsBetween('2026-01-01', '2026-12-31')
console.log('LEDGER LOAD OK:', ledgerRows.length, '条')
const stats = await ledger.listTransactionsBetween('2025-01-01', '2026-12-31')
console.log('LEDGER RANGE OK:', stats.length, '条')
process.exit(0)
}
main().catch(e => {
console.error('FAILED:', (e && (e.stack || e.message)) || e)
process.exit(1)
})