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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
require "minitest/autorun"
require "open3"
require "pathname"
require "tmpdir"

class JeansTest < Minitest::Test
  JEANS = Pathname.new(ENV.fetch("JEANS")).expand_path
  DONE = 'bookmarks(exact:"jeans/thing/done")'

  def setup
    @tmp = Pathname.new(Dir.mktmpdir)
    @repo = @tmp / "repo"
    @repo.mkpath
    sh("jj", "git", "init", ".")
    @goal = jeans("init", "ship the thing", "--goal", "thing", "--team", "2", "--home", @tmp / "jeans")
  end

  def teardown
    FileUtils.rm_rf(@tmp)
  end

  def test_goal_alone_is_ready
    assert_equal [@goal], jeans("ready").lines(chomp: true)
    assert_equal "2", jeans("team-size")
  end

  def test_subtasks_block_their_parent
    a = jeans("add", "A")
    b = jeans("add", "B")

    assert_equal [b, a].sort, jeans("ready").lines(chomp: true).sort
    assert_equal [a, b].sort, parents(@goal).sort
  end

  def test_split_a_task_into_siblings_and_a_dependency
    a = jeans("add", "A")
    a1 = jeans("add", "A.1", "--before", a)
    a2 = jeans("add", "A.2", "--before", a, "--after", a1)

    assert_equal [a2], parents(a)
    assert_equal [a1], parents(a2)
    assert_equal [a1], jeans("ready").lines(chomp: true)
  end

  def test_only_the_claiming_developer_changes_a_claimed_task
    a = jeans("add", "A")
    ws = Pathname.new(jeans("claim", a))

    assert_match(/is claimed/, jeans_fails("add", "A.1", "--before", a))
    assert_match(/is claimed/, jeans_fails("describe", a, "-m", "A, rescoped"))

    a1 = jeans("add", "A.1", "--before", a, chdir: ws)
    jeans("add", "A.1.1", "--before", a1, chdir: ws)
    jeans("describe", a, "-m", "A, rescoped", chdir: ws)

    _, err, status = Open3.capture3(JEANS.to_s, "add", "found a bug", chdir: ws.to_s)
    refute status.success?
    assert_match(/outside your task/, err)
    _, err, = Open3.capture3(JEANS.to_s, "describe", a1, "-m", "nope", chdir: ws.to_s)
    assert_match(/outside your task/, err)
  end

  def test_done_keeps_the_spec_between_message_and_trailers
    a = jeans("add", "A", "-m", "- one\n- two\n\nthen three")
    jeans("claim", a)
    jeans("done", a, "-m", "area: do A\n\nBecause.\n\nAssisted-by: Claude\nSigned-off-by: Someone")

    assert_equal <<~MESSAGE, sh("jj", "log", "--no-graph", "-r", a, "-T", "description")
      area: do A

      Because.

      Task: A

        - one
        - two

        then three

      Assisted-by: Claude
      Signed-off-by: Someone
    MESSAGE
    trailer = 'trailers.filter(|t| t.key() == "Assisted-by").map(|t| t.value()).join(",")'
    assert_equal "Claude", sh("jj", "log", "--no-graph", "-r", a, "-T", trailer)
  end

  def test_done_leaves_a_last_paragraph_that_isnt_trailers
    a = jeans("add", "A")
    jeans("claim", a)
    jeans("done", a, "-m", "do A\n\n- a list\n  wrapped")

    assert_equal "do A\n\n- a list\n  wrapped\n\nTask: A\n", sh("jj", "log", "--no-graph", "-r", a, "-T", "description")
  end

  def test_done_needs_a_message
    a = jeans("add", "A")
    ws = Pathname.new(jeans("claim", a))
    (ws / "a.txt").write("hello\n")

    assert_match(/done needs -m MESSAGE/, jeans_fails("done", a))
    assert_match(/done needs -m MESSAGE/, jeans_fails("done", a, "-m", "  "))
    assert_equal "claimed", jeans("show", a).lines.first.strip
    assert_equal "A\n", sh("jj", "log", "--no-graph", "-r", a, "-T", "description")
  end

  def test_describe_rewrites_a_task
    a = jeans("add", "A")
    jeans("describe", a, "-m", "A, but answered")
    assert_includes jeans("show", a), "A, but answered"
  end

  def test_claim_work_done
    a = jeans("add", "A")
    ws = Pathname.new(jeans("claim", a))
    assert_includes jeans("show", a), "claimed"
    refute_includes jeans("ready"), a
    assert_equal "jeans: #{a} is claimed, not ready", jeans_fails("claim", a)

    (ws / "a.txt").write("hello\n")
    finish(a)

    refute ws.exist?
    assert_equal "done", jeans("show", a).lines.first.strip
    assert_equal "A\n\nTask: A\n", sh("jj", "log", "--no-graph", "-r", a, "-T", "description")
    assert_equal [a], parents(DONE)
    assert_equal "hello\n", (@repo / "a.txt").read
    assert_match(/is done/, jeans_fails("add", "late", "--before", a))
    assert_equal "a.txt", sh("jj", "file", "list", "-r", a).strip
    assert_equal [@goal], jeans("ready").lines(chomp: true)
    assert_equal 1, sh("jj", "workspace", "list").lines.size
    assert_equal "", sh("jj", "log", "--no-graph", "-r", "heads(all()) ~ (@ | #{@goal})", "-T", "change_id")
  end

  def test_done_goal_leaves_a_plain_commit
    a = jeans("add", "A")
    jeans("claim", a)
    finish(a)
    jeans("claim", @goal)
    jeans("done", @goal, "-m", "thing: ship it\n\nBuilt A.")

    assert_equal "", sh("jj", "bookmark", "list", "jeans/thing/goal", "jeans/thing/done")
    assert_equal "thing: ship it\n\nBuilt A.\n\nTask: ship the thing\n", sh("jj", "log", "--no-graph", "-r", @goal, "-T", "description")
    assert_equal [@goal], parents("@")
    refute_equal @goal, jeans("init", "the next thing", "--goal", "thing", "--home", @tmp / "jeans")
  end

  def test_finished_work_goes_into_the_megamerge
    @repo = @tmp / "megamerge"
    @repo.mkpath
    sh("jj", "git", "init", ".")
    branches = %w[x y].map do |name|
      sh("jj", "new", "root()", "-m", name)
      (@repo / "#{name}.txt").write("#{name}\n")
      change_id("@")
    end
    sh("jj", "new", *branches, "-m", "mm")
    sh("jj", "bookmark", "create", "mm", "-r", "@")
    sh("jj", "new")
    goal = jeans("init", "ship the thing", "--goal", "thing", "--home", @tmp / "megamerge.jeans")
    done = change_id(DONE)
    assert_equal [*branches, done].sort, parents("mm").sort

    a = jeans("add", "A")
    (Pathname.new(jeans("claim", a)) / "a.txt").write("hello\n")
    finish(a)

    assert_equal [a], parents(DONE)
    assert_equal [*branches, done].sort, parents("mm").sort
    assert_equal [change_id("mm")], parents("@")
    assert_equal "hello\n", (@repo / "a.txt").read

    jeans("claim", goal)
    finish(goal)
    assert_equal [*branches, goal].sort, parents("mm").sort
    assert_equal "", sh("jj", "bookmark", "list", "jeans/thing/goal", "jeans/thing/done")
  end

  def test_goals_keep_their_own_tasks
    other_done = 'bookmarks(exact:"jeans/other/done")'
    jeans("init", "another thing", "--goal", "other", "--home", @tmp / "jeans")
    assert_match(/already has a goal named other/, jeans_fails("init", "again", "--goal", "other"))
    assert_match(/init needs --goal NAME/, jeans_fails("init", "unnamed"))
    assert_match(/has goals other, thing; pick one with --goal NAME/, jeans_fails("ready"))
    assert_match(/no goal named nope; the goals are other, thing/, jeans_fails("ready", "--goal", "nope"))

    a = jeans("add", "A", "--goal", "thing")
    b = jeans("add", "B", "--goal", "other")
    assert_equal [a], jeans("ready", "--goal", "thing").lines(chomp: true)
    assert_equal [b], jeans("ready", "--goal", "other").lines(chomp: true)

    ws = Pathname.new(jeans("claim", a))
    a1 = jeans("add", "A.1", "--before", a, chdir: ws)
    jeans("release", a, chdir: ws)
    (Pathname.new(jeans("claim", a1)) / "a1.txt").write("hello\n")
    finish(a1)

    assert_equal "done", jeans("show", a1).lines.first.strip
    assert_equal [a1], parents(DONE)
    refute_includes parents(other_done), a1
    assert_equal [change_id(DONE), change_id(other_done)].sort, parents("@").sort
    assert_equal "hello\n", (@repo / "a1.txt").read
  end

  def test_release_keeps_work_without_finishing
    a = jeans("add", "A")
    ws = Pathname.new(jeans("claim", a))
    (ws / "partial.txt").write("wip\n")
    jeans("release", a)

    assert_equal [a], jeans("ready").lines(chomp: true)
    assert_equal "partial.txt", sh("jj", "file", "list", "-r", a).strip
  end

  def test_works_from_inside_a_workspace
    a = jeans("add", "A")
    ws = Pathname.new(jeans("claim", a))
    a1 = jeans("add", "A.1", "--before", a, chdir: ws)
    jeans("release", a, chdir: ws)

    assert_equal [a1], jeans("ready").lines(chomp: true)
  end

  def test_integration_supersedes_an_untaken_resolution_task
    [jeans("add", "A"), jeans("add", "B")].each do |task|
      ws = Pathname.new(jeans("claim", task))
      (ws / "same.txt").write("#{task}\n")
      finish(task)
    end
    resolution = parents(DONE).first

    ws = Pathname.new(jeans("claim", @goal))
    (ws / "same.txt").write("both\n")
    finish(@goal)

    assert_equal [@goal], parents("@")
    assert_equal "", sh("jj", "log", "--no-graph", "-r", "present(#{resolution})", "-T", "change_id")
    assert_equal "both\n", (@repo / "same.txt").read
  end

  def test_conflicts_in_the_done_node_get_a_resolution_task
    a = jeans("add", "A")
    b = jeans("add", "B")
    [a, b].each do |task|
      ws = Pathname.new(jeans("claim", task))
      (ws / "same.txt").write("#{task}\n")
      finish(task)
    end

    assert_equal "true", sh("jj", "log", "--no-graph", "-r", "@", "-T", "conflict")
    resolution, *others = parents(DONE)
    assert_empty others
    assert_match(/\AResolve conflicts between [AB], [AB]\n/, sh("jj", "log", "--no-graph", "-r", resolution, "-T", "description"))
    assert_equal [@goal, resolution].sort, jeans("ready").lines(chomp: true).sort
    assert_includes jeans("tree"), "#{resolution[0, 8]} Resolve conflicts"

    [resolution, @goal].each do |task|
      ws = Pathname.new(jeans("claim", task))
      assert_match(/has conflicts/, jeans_fails("done", task, "-m", title(task)))
      (ws / "same.txt").write("both\n")
      finish(task)
    end

    assert_equal "false", sh("jj", "log", "--no-graph", "-r", "@", "-T", "conflict")
    assert_equal "both\n", (@repo / "same.txt").read
    assert_empty jeans("ready")
  end

  def test_views
    a = jeans("add", "A")
    a1 = jeans("add", "A.1", "--before", a)
    b = jeans("add", "B")
    jeans("claim", b)

    assert_equal <<~TREE.strip, jeans("tree")
      #{@goal[0, 8]} ship the thing [blocked]
      ├── #{a[0, 8]} A [blocked]
      │   └── #{a1[0, 8]} A.1 [ready]
      └── #{b[0, 8]} B [claimed]
    TREE
    assert_equal "#{a[0, 8]} A [blocked] (+1 more)", jeans("tree", a, "--depth", "0")
    scoped = jeans("board", a)
    assert_includes scoped, a1[0, 8]
    refute_includes scoped, b[0, 8]

    assert_equal <<~BOARD.strip, jeans("board")
      READY (1)
        #{a1[0, 8]} A.1

      CLAIMED (1)
        #{b[0, 8]} B

      BLOCKED (2)
        #{@goal[0, 8]} ship the thing
        #{a[0, 8]} A

      DONE (0)
    BOARD

    assert_match(/#{a1[0, 12]} \[ready\] A\.1/, jeans("log", "ready"))
    refute_match(/#{b[0, 12]}/, jeans("log", "ready"))
    assert_equal [a, a1].map { it[0, 12] }, jeans("log", "under", a).scan(/[k-z]{12}/)
    assert_equal [@goal, a, a1].map { it[0, 12] }, jeans("log", "path", a1).scan(/[k-z]{12}/)
    assert_equal [b[0, 12]], jeans("log", 'description(exact:"B\n")').scan(/[k-z]{12}/)

    [a1, a].each do |task|
      jeans("claim", task)
      finish(task)
    end
    assert_includes jeans("tree"), "#{a[0, 8]} A [done] (+1 done)"
  end

  def test_color_changes_only_escape_codes
    a = jeans("add", "A")
    jeans("add", "A.1", "--before", a)
    jeans("claim", jeans("add", "B"))

    [%w[tree], %w[board], %w[log], ["show", a]].each do |view|
      colored = jeans(*view, "--color", "always")
      assert_includes colored, "\e[", "#{view.first} has no color"
      assert_equal jeans(*view), colored.gsub(/\e\[[\d;]*m/, ""), "#{view.first} differs beyond color"
    end
    assert_includes jeans("tree", "--color", "always"), "\e[32m[ready]\e[0m"
    refute_includes jeans("tree"), "\e["
  end

  def test_watch_redraws_when_the_graph_changes
    a = jeans("add", "A")
    Open3.popen2(JEANS.to_s, "watch", chdir: @repo.to_s) do |_, out, thread|
      read_until(out, "#{a[0, 8]} A [ready]")
      op = sh("jj", "op", "log", "-n1", "--no-graph", "-T", "id")
      sleep 2
      assert_equal op, sh("jj", "op", "log", "-n1", "--no-graph", "-T", "id"), "watching added an operation"

      b = jeans("add", "B")
      read_until(out, "#{b[0, 8]} B [ready]")
    ensure
      Process.kill("INT", thread.pid)
      thread.join
    end
    assert_match(/tree or board/, jeans_fails("watch", "log"))
  end

  def test_concurrent_done_leaves_no_divergence
    tasks = 3.times.map { jeans("add", "T#{it}") }
    tasks.each do |task|
      ws = Pathname.new(jeans("claim", task))
      (ws / "#{task}.txt").write("x\n")
    end
    tasks.map { |task| Thread.new { finish(task) } }.each(&:join)

    assert_equal [@goal], jeans("ready").lines(chomp: true)
    refute_includes sh("jj", "log", "-r", "all()"), "divergent"
  end

  private

  # Reusing the task's title as the commit title keeps the views' labels stable.
  def finish(task)
    jeans("done", task, "-m", title(task))
  end

  def title(rev)
    sh("jj", "log", "--no-graph", "-r", rev, "-T", "description.first_line()")
  end

  def change_id(rev)
    sh("jj", "log", "--no-graph", "-r", rev, "-T", "change_id")
  end

  def parents(rev)
    sh("jj", "log", "--no-graph", "-r", "parents(#{rev})", "-T", 'change_id ++ "\n"').lines(chomp: true)
  end

  def jeans(*args, chdir: @repo)
    out, err, status = Open3.capture3(JEANS.to_s, *args.map(&:to_s), chdir: chdir.to_s)
    assert status.success?, "jeans #{args.join(" ")} failed: #{err}"
    out.strip
  end

  def read_until(io, text, timeout: 10)
    seen = +""
    deadline = Time.now + timeout
    until seen.include?(text)
      flunk "never printed #{text.inspect}; got:\n#{seen}" unless io.wait_readable([deadline - Time.now, 0].max)
      seen << io.readpartial(4096)
    end
  end

  def jeans_fails(*args)
    _, err, status = Open3.capture3(JEANS.to_s, *args.map(&:to_s), chdir: @repo.to_s)
    refute status.success?
    err.strip
  end

  def sh(*args)
    out, err, status = Open3.capture3(*args, chdir: @repo.to_s)
    assert status.success?, "#{args.join(" ")} failed: #{err}"
    out
  end
end