File size: 6,809 Bytes
1f1a802
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import { describe, it, expect, beforeEach, afterEach } from 'vitest'
import fs from 'node:fs'
import path from 'node:path'
import os from 'node:os'
import { execFileSync } from 'node:child_process'
import {
  mcStatusToGnap,
  gnapStatusToMc,
  mcPriorityToGnap,
  initGnapRepo,
  pushTaskToGnap,
  removeTaskFromGnap,
  pullTasksFromGnap,
  getGnapStatus,
  type McTask,
} from '../gnap-sync'

let tmpDir: string

beforeEach(() => {
  tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'gnap-test-'))
})

afterEach(() => {
  fs.rmSync(tmpDir, { recursive: true, force: true })
})

describe('status mapping', () => {
  it('maps MC statuses to GNAP states', () => {
    expect(mcStatusToGnap('pending')).toBe('backlog')
    expect(mcStatusToGnap('inbox')).toBe('backlog')
    expect(mcStatusToGnap('in_progress')).toBe('in_progress')
    expect(mcStatusToGnap('done')).toBe('done')
    expect(mcStatusToGnap('review')).toBe('review')
    expect(mcStatusToGnap('blocked')).toBe('blocked')
    expect(mcStatusToGnap('cancelled')).toBe('cancelled')
  })

  it('maps GNAP states back to MC statuses', () => {
    expect(gnapStatusToMc('backlog')).toBe('inbox')
    expect(gnapStatusToMc('in_progress')).toBe('in_progress')
    expect(gnapStatusToMc('done')).toBe('done')
    expect(gnapStatusToMc('review')).toBe('review')
  })

  it('falls back for unknown values', () => {
    expect(mcStatusToGnap('unknown_status')).toBe('backlog')
    expect(gnapStatusToMc('unknown_state')).toBe('inbox')
  })
})

describe('priority mapping', () => {
  it('maps MC priorities to GNAP priorities', () => {
    expect(mcPriorityToGnap('low')).toBe('low')
    expect(mcPriorityToGnap('medium')).toBe('medium')
    expect(mcPriorityToGnap('high')).toBe('high')
    expect(mcPriorityToGnap('critical')).toBe('critical')
    expect(mcPriorityToGnap('urgent')).toBe('critical')
  })

  it('falls back to medium for unknown priorities', () => {
    expect(mcPriorityToGnap('unknown')).toBe('medium')
  })
})

describe('initGnapRepo', () => {
  it('creates directory structure and initializes git', () => {
    const repoPath = path.join(tmpDir, 'gnap-repo')
    initGnapRepo(repoPath)

    expect(fs.existsSync(path.join(repoPath, 'version'))).toBe(true)
    expect(fs.existsSync(path.join(repoPath, 'agents.json'))).toBe(true)
    expect(fs.existsSync(path.join(repoPath, 'tasks'))).toBe(true)
    expect(fs.existsSync(path.join(repoPath, '.git'))).toBe(true)

    expect(fs.readFileSync(path.join(repoPath, 'version'), 'utf-8').trim()).toBe('1')
  })

  it('is idempotent — re-running does not error', () => {
    const repoPath = path.join(tmpDir, 'gnap-repo')
    initGnapRepo(repoPath)
    initGnapRepo(repoPath)

    expect(fs.existsSync(path.join(repoPath, '.git'))).toBe(true)
  })
})

describe('pushTaskToGnap', () => {
  it('writes task JSON and commits', () => {
    const repoPath = path.join(tmpDir, 'gnap-repo')
    initGnapRepo(repoPath)

    const task: McTask = {
      id: 42,
      title: 'Test task',
      description: 'A test',
      status: 'in_progress',
      priority: 'high',
      assigned_to: 'agent-claude',
      tags: ['auth', 'sprint-1'],
      created_at: 1710500000,
      updated_at: 1710510000,
      project_id: 1,
    }

    pushTaskToGnap(task, repoPath)

    const filePath = path.join(repoPath, 'tasks', 'mc-42.json')
    expect(fs.existsSync(filePath)).toBe(true)

    const content = JSON.parse(fs.readFileSync(filePath, 'utf-8'))
    expect(content.id).toBe('mc-42')
    expect(content.title).toBe('Test task')
    expect(content.state).toBe('in_progress')
    expect(content.priority).toBe('high')
    expect(content.assignee).toBe('agent-claude')
    expect(content.tags).toEqual(['auth', 'sprint-1'])
    expect(content.mc_id).toBe(42)
    expect(content.mc_project_id).toBe(1)

    // Verify it was committed
    const log = execFileSync('git', ['log', '--oneline'], {
      cwd: repoPath,
      encoding: 'utf-8',
    })
    expect(log).toContain('Update task mc-42')
  })

  it('handles string tags (JSON serialized)', () => {
    const repoPath = path.join(tmpDir, 'gnap-repo')
    initGnapRepo(repoPath)

    const task: McTask = {
      id: 1,
      title: 'String tags task',
      status: 'pending',
      priority: 'low',
      tags: '["bug","fix"]',
    }

    pushTaskToGnap(task, repoPath)

    const content = JSON.parse(
      fs.readFileSync(path.join(repoPath, 'tasks', 'mc-1.json'), 'utf-8')
    )
    expect(content.tags).toEqual(['bug', 'fix'])
  })
})

describe('removeTaskFromGnap', () => {
  it('removes the task file and commits', () => {
    const repoPath = path.join(tmpDir, 'gnap-repo')
    initGnapRepo(repoPath)

    const task: McTask = {
      id: 7,
      title: 'To be removed',
      status: 'done',
      priority: 'low',
    }
    pushTaskToGnap(task, repoPath)
    expect(fs.existsSync(path.join(repoPath, 'tasks', 'mc-7.json'))).toBe(true)

    removeTaskFromGnap(7, repoPath)
    expect(fs.existsSync(path.join(repoPath, 'tasks', 'mc-7.json'))).toBe(false)

    const log = execFileSync('git', ['log', '--oneline'], {
      cwd: repoPath,
      encoding: 'utf-8',
    })
    expect(log).toContain('Remove task mc-7')
  })

  it('does nothing when task does not exist', () => {
    const repoPath = path.join(tmpDir, 'gnap-repo')
    initGnapRepo(repoPath)
    // Should not throw
    removeTaskFromGnap(999, repoPath)
  })
})

describe('pullTasksFromGnap', () => {
  it('reads all task files from the repo', () => {
    const repoPath = path.join(tmpDir, 'gnap-repo')
    initGnapRepo(repoPath)

    pushTaskToGnap({ id: 1, title: 'Task A', status: 'pending', priority: 'low' }, repoPath)
    pushTaskToGnap({ id: 2, title: 'Task B', status: 'done', priority: 'high' }, repoPath)

    const tasks = pullTasksFromGnap(repoPath)
    expect(tasks).toHaveLength(2)

    const ids = tasks.map(t => t.id).sort()
    expect(ids).toEqual(['mc-1', 'mc-2'])
  })

  it('returns empty array for non-existent directory', () => {
    const tasks = pullTasksFromGnap(path.join(tmpDir, 'nonexistent'))
    expect(tasks).toEqual([])
  })
})

describe('getGnapStatus', () => {
  it('reports uninitialized for empty directory', () => {
    const status = getGnapStatus(path.join(tmpDir, 'empty'))
    expect(status.initialized).toBe(false)
    expect(status.taskCount).toBe(0)
    expect(status.hasRemote).toBe(false)
  })

  it('reports correct status after init and push', () => {
    const repoPath = path.join(tmpDir, 'gnap-repo')
    initGnapRepo(repoPath)
    pushTaskToGnap({ id: 1, title: 'Task', status: 'pending', priority: 'medium' }, repoPath)

    const status = getGnapStatus(repoPath)
    expect(status.initialized).toBe(true)
    expect(status.taskCount).toBe(1)
    expect(status.hasRemote).toBe(false)
    expect(status.remoteUrl).toBe('')
  })
})