|
| 1 | +import { describe, it, expect, beforeEach } from 'vitest' |
| 2 | +import { Database } from 'bun:sqlite' |
| 3 | +import { migrate } from '../../src/db/migration-runner' |
| 4 | +import { allMigrations } from '../../src/db/migrations' |
| 5 | +import { |
| 6 | + listAllScheduleJobsWithRepos, |
| 7 | + listAllScheduleRuns, |
| 8 | +} from '../../src/db/schedules' |
| 9 | + |
| 10 | +describe('assistant repo (repo_id=0) in global aggregate queries', () => { |
| 11 | + let db: Database |
| 12 | + |
| 13 | + beforeEach(() => { |
| 14 | + db = new Database(':memory:') |
| 15 | + // Disable FK enforcement to match bun:sqlite default behavior (OFF) |
| 16 | + db.exec('PRAGMA foreign_keys = OFF') |
| 17 | + migrate(db, allMigrations) |
| 18 | + |
| 19 | + const now = Date.now() |
| 20 | + |
| 21 | + // Insert a real repo (id=1) |
| 22 | + db.exec( |
| 23 | + `INSERT INTO repos (id, repo_url, local_path, branch, default_branch, clone_status, cloned_at) |
| 24 | + VALUES (1, 'https://github.com/test/my-repo', 'repos/my-repo', 'main', 'main', 'ready', ${now})`, |
| 25 | + ) |
| 26 | + |
| 27 | + // Insert a schedule job for the real repo |
| 28 | + db.exec( |
| 29 | + `INSERT INTO schedule_jobs (id, repo_id, name, enabled, schedule_mode, prompt, created_at, updated_at) |
| 30 | + VALUES (1, 1, 'Real repo job', 1, 'interval', 'Run the real repo job', ${now}, ${now})`, |
| 31 | + ) |
| 32 | + |
| 33 | + // Insert a schedule job for the assistant (repo_id=0) |
| 34 | + db.exec( |
| 35 | + `INSERT INTO schedule_jobs (id, repo_id, name, enabled, schedule_mode, prompt, created_at, updated_at) |
| 36 | + VALUES (2, 0, 'Assistant job', 1, 'interval', 'Run the assistant job', ${now}, ${now})`, |
| 37 | + ) |
| 38 | + |
| 39 | + // Insert a schedule run for the real repo job |
| 40 | + db.exec( |
| 41 | + `INSERT INTO schedule_runs (id, job_id, repo_id, trigger_source, status, started_at, created_at) |
| 42 | + VALUES (1, 1, 1, 'manual', 'completed', ${now}, ${now})`, |
| 43 | + ) |
| 44 | + |
| 45 | + // Insert a schedule run for the assistant job |
| 46 | + db.exec( |
| 47 | + `INSERT INTO schedule_runs (id, job_id, repo_id, trigger_source, status, started_at, created_at) |
| 48 | + VALUES (2, 2, 0, 'manual', 'completed', ${now}, ${now})`, |
| 49 | + ) |
| 50 | + }) |
| 51 | + |
| 52 | + it('listAllScheduleJobsWithRepos includes assistant jobs with synthetic metadata', () => { |
| 53 | + const jobs = listAllScheduleJobsWithRepos(db) |
| 54 | + expect(jobs).toHaveLength(2) |
| 55 | + |
| 56 | + const assistantJob = jobs.find(j => j.repoId === 0) |
| 57 | + expect(assistantJob).toBeDefined() |
| 58 | + if (assistantJob) { |
| 59 | + expect(assistantJob.repoName).toBe('Assistant') |
| 60 | + expect(assistantJob.repoPath).toBe('assistant') |
| 61 | + expect(assistantJob.repoUrl).toBe('') |
| 62 | + expect(assistantJob.name).toBe('Assistant job') |
| 63 | + } |
| 64 | + |
| 65 | + const realJob = jobs.find(j => j.repoId === 1) |
| 66 | + expect(realJob).toBeDefined() |
| 67 | + if (realJob) { |
| 68 | + expect(realJob.repoName).toBe('my-repo') |
| 69 | + expect(realJob.repoPath).toBe('repos/my-repo') |
| 70 | + expect(realJob.repoUrl).toBe('https://github.com/test/my-repo') |
| 71 | + expect(realJob.name).toBe('Real repo job') |
| 72 | + } |
| 73 | + }) |
| 74 | + |
| 75 | + it('listAllScheduleRuns includes assistant runs with synthetic metadata', () => { |
| 76 | + const runs = listAllScheduleRuns(db, {}) |
| 77 | + expect(runs).toHaveLength(2) |
| 78 | + |
| 79 | + const assistantRun = runs.find(r => r.repoId === 0) |
| 80 | + expect(assistantRun).toBeDefined() |
| 81 | + if (assistantRun) { |
| 82 | + expect(assistantRun.repoName).toBe('Assistant') |
| 83 | + expect(assistantRun.repoPath).toBe('assistant') |
| 84 | + expect(assistantRun.jobName).toBe('Assistant job') |
| 85 | + } |
| 86 | + |
| 87 | + const realRun = runs.find(r => r.repoId === 1) |
| 88 | + expect(realRun).toBeDefined() |
| 89 | + if (realRun) { |
| 90 | + expect(realRun.repoName).toBe('my-repo') |
| 91 | + expect(realRun.repoPath).toBe('repos/my-repo') |
| 92 | + expect(realRun.jobName).toBe('Real repo job') |
| 93 | + } |
| 94 | + }) |
| 95 | + |
| 96 | + it('listAllScheduleRuns with repoId=0 filter returns only assistant runs', () => { |
| 97 | + const runs = listAllScheduleRuns(db, { repoId: 0 }) |
| 98 | + expect(runs).toHaveLength(1) |
| 99 | + const run = runs[0]! |
| 100 | + expect(run.repoId).toBe(0) |
| 101 | + expect(run.repoName).toBe('Assistant') |
| 102 | + expect(run.repoPath).toBe('assistant') |
| 103 | + }) |
| 104 | +}) |
0 commit comments