summaryrefslogtreecommitdiff
path: root/tests/run.lua
blob: f984123310cf382975cb9821b53a92b34f4847e4 (plain)
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
local script = debug.getinfo(1, "S").source:sub(2)
local root = script:match("^(.*)/tests/run%.lua$") or "."
package.path = table.concat({
  root .. "/lua/?.lua",
  root .. "/lua/?/init.lua",
  package.path,
}, ";")

local domain = require("clever_f.domain")
local capabilities = require("clever_f.capabilities")
local policy = require("clever_f.policy")
local MemoryHost = require("clever_f.testing.memory_host")

local tests = {}
local passed = 0

local function test(name, body)
  tests[#tests + 1] = { name = name, body = body }
end

local function same(expected, actual, message)
  if expected ~= actual then
    error((message or "values differ")
      .. ": expected " .. tostring(expected)
      .. ", got " .. tostring(actual), 2)
  end
end

local function truthy(value, message)
  if not value then
    error(message or "value must be true", 2)
  end
end

local function falsy(value, message)
  if value then
    error(message or "value must be false", 2)
  end
end

local function fails(body, expected_text)
  local ok, failure = pcall(body)
  if ok then
    error("operation must fail", 2)
  end
  if expected_text and not tostring(failure):find(expected_text, 1, true) then
    error("failure does not contain '" .. expected_text .. "': " .. tostring(failure), 2)
  end
end

local function list_same(expected, actual)
  same(#expected, #actual, "list lengths differ")
  for index = 1, #expected do
    same(expected[index], actual[index], "list item " .. tostring(index) .. " differs")
  end
end

test("Position validates one-based byte coordinates", function()
  local position = domain.Position.new(1, 1)
  same(1, position.line)
  same(1, position.byte_column)
  same("Position", domain.type_of(position))
  fails(function()
    domain.Position.new(0, 1)
  end, "line")
  fails(function()
    domain.Position.new(1, 0)
  end, "byte_column")
  fails(function()
    domain.Position.new(1.5, 1)
  end, "line")
  fails(function()
    position.line = 2
  end, "immutable")
end)

test("Position comparison is lexicographic and detects stationarity", function()
  local first = domain.Position.new(1, 9)
  local later_column = domain.Position.new(1, 10)
  local later_line = domain.Position.new(2, 1)
  local same_place = domain.Position.new(1, 9)

  same(-1, domain.Position.compare(first, later_column))
  same(-1, domain.Position.compare(later_column, later_line))
  same(1, domain.Position.compare(later_line, first))
  truthy(domain.Position.is_forward(later_line, first))
  truthy(domain.Position.is_backward(first, later_line))
  truthy(domain.Position.stationary(first, same_place))
  falsy(domain.Position.is_forward(first, same_place))
  falsy(domain.Position.is_backward(first, same_place))
  truthy(first == same_place)

  local serialized = first:to_table()
  same(2, (function()
    local count = 0
    for _ in pairs(serialized) do
      count = count + 1
    end
    return count
  end)())
end)

test("Descriptor conversion defines all families and directions", function()
  local cases = {
    { "f", domain.Family.FIND, domain.Direction.FORWARD, false },
    { "F", domain.Family.FIND, domain.Direction.BACKWARD, true },
    { "t", domain.Family.TILL, domain.Direction.FORWARD, false },
    { "T", domain.Family.TILL, domain.Direction.BACKWARD, true },
  }

  for _, case in ipairs(cases) do
    local descriptor = domain.Descriptor.from_string(case[1])
    same(case[1], domain.Descriptor.to_string(descriptor))
    same(case[2], descriptor.family)
    same(case[3], descriptor.direction)
    same(case[4], domain.Descriptor.is_uppercase(descriptor))
    same(descriptor, domain.Descriptor.from_parts(case[2], case[3]))
    same(descriptor, domain.Descriptor.swap(domain.Descriptor.swap(descriptor)))
  end

  same(domain.Descriptor.FIND_BACKWARD, domain.Descriptor.swap("f"))
  same(domain.Descriptor.TILL_FORWARD, domain.Descriptor.swap("T"))
  same(domain.Descriptor.FIND_FORWARD, domain.Descriptor.lowercase("F"))
  same(domain.Descriptor.TILL_BACKWARD, domain.Descriptor.uppercase("t"))
  falsy(domain.Descriptor.is_valid("x"))
  fails(function()
    domain.Descriptor.from_string("x")
  end, "f, F, t, or T")
end)

test("Count normalizes absent input and rejects nonpositive values", function()
  same(domain.Count.ONE, domain.Count.new(nil))
  same(1, domain.Count.to_number(nil))
  same(3, domain.Count.to_number(domain.Count.new(3)))
  fails(function()
    domain.Count.new(0)
  end, "positive")
  fails(function()
    domain.Count.new(-1)
  end, "positive")
  fails(function()
    domain.Count.new(1.5)
  end, "positive")
end)

test("ModeContext normalizes every operator-pending variant", function()
  local operator = domain.ModeContext.from_full_mode("no")
  local variants = {
    "no",
    "nov",
    "noV",
    "no" .. string.char(0x16),
  }
  for _, mode in ipairs(variants) do
    local context = domain.ModeContext.from_full_mode(mode)
    same(operator, context)
    same("no", context.key)
    truthy(context.operator)
    truthy(context.command_path)
  end
end)

test("ModeContext preserves other full strings and exposes mode traits", function()
  local normal = domain.ModeContext.from_full_mode("n")
  local insert_normal = domain.ModeContext.from_full_mode("niI")
  local other_full_mode = domain.ModeContext.from_full_mode("normal-extension")
  local visual_character = domain.ModeContext.from_full_mode("v")
  local visual_line = domain.ModeContext.from_full_mode("V")
  local visual_block = domain.ModeContext.from_full_mode(string.char(0x16))
  local visual_from_select = domain.ModeContext.from_full_mode("vs")
  local select_character = domain.ModeContext.from_full_mode("s")
  local select_line = domain.ModeContext.from_full_mode("S")
  local select_block = domain.ModeContext.from_full_mode(string.char(0x13))

  same("n", normal.key)
  same("niI", insert_normal.key)
  same("normal-extension", other_full_mode.key)
  falsy(other_full_mode.operator)
  falsy(normal == insert_normal)
  same(domain.SelectionKind.CHARACTER, visual_character.visual_kind)
  same(domain.SelectionKind.LINE, visual_line.visual_kind)
  same(domain.SelectionKind.BLOCK, visual_block.visual_kind)
  same(domain.SelectionKind.CHARACTER, visual_from_select.visual_kind)
  falsy(visual_character.command_path)
  same(domain.SelectionKind.CHARACTER, select_character.select_kind)
  same(domain.SelectionKind.LINE, select_line.select_kind)
  same(domain.SelectionKind.BLOCK, select_block.select_kind)
  truthy(select_character.command_path)
  falsy(select_character.visual)
  truthy(select_character.select)
end)

test("Selection and text snapshots preserve semantic input values", function()
  local anchor = domain.Position.new(1, 1)
  local focus = domain.Position.new(1, 3)
  local selection = domain.Selection.active(
    domain.SelectionKind.CHARACTER,
    anchor,
    focus,
    domain.SelectionOption.EXCLUSIVE
  )
  truthy(selection.active)
  same(domain.SelectionKind.CHARACTER, selection.kind)
  same(domain.SelectionOption.EXCLUSIVE, selection.option)
  same(domain.Position.new(1, 4), selection:with_focus(domain.Position.new(1, 4)).focus)

  local inactive = domain.Selection.inactive()
  falsy(inactive.active)
  same(domain.SelectionKind.NONE, inactive.kind)

  local text = domain.TextSnapshot.new({ "alpha", "beta" })
  same(2, text.line_count)
  same("alpha", text:line(1))
  local lines = text:lines()
  lines[1] = "changed"
  same("alpha", text:line(1))
end)

test("TargetValue represents characters, special keys, and code fallback", function()
  local character = domain.TargetValue.character("a", 97)
  local special = domain.TargetValue.special_key(string.char(0x80, 0xfd, 0x01))
  local fallback = domain.TargetValue.code_fallback()

  same(domain.TargetKind.CHARACTER, character.kind)
  same("a", character.value)
  same(97, character.first_code)
  same(domain.TargetKind.SPECIAL_KEY, special.kind)
  same(0x80, special.first_code)
  same(domain.TargetKind.CODE_FALLBACK, fallback.kind)
  same(0, fallback.first_code)
  same("", fallback.value)
  fails(function()
    domain.TargetValue.special_key("x")
  end, "hexadecimal 80")
  fails(function()
    character.value = "b"
  end, "immutable")
end)

test("InputPacket carries every input capability variant", function()
  local text = domain.InputPacket.text("a")
  local bytes = domain.InputPacket.raw_bytes({ 0x80, 0xfd, 0x60 })
  local special = domain.InputPacket.special_key("Left", string.char(0x80, 1))
  local failure = domain.InputPacket.error("input failed")

  same(domain.InputPacketKind.TEXT, text.kind)
  list_same({ 0x80, 0xfd, 0x60 }, bytes:bytes())
  same("Left", special.name)
  same("input failed", failure.message)
end)

test("Plans and requests carry typed immutable motion data", function()
  local target = domain.TargetValue.character("h", 104)
  local target_plan = domain.TargetPlan.new({
    target = target,
    kind = domain.TargetPlanKind.LITERAL,
    case_mode = domain.CaseMode.SENSITIVE,
    matcher = function(character)
      return character == "h"
    end,
  })
  truthy(target_plan:matches("h"))
  falsy(target_plan:matches("H"))

  local motion_plan = domain.ResolvedMotionPlan.new({
    target_plan = target_plan,
    descriptor = "f",
    search_scope = domain.SearchScope.BUFFER,
    endpoint_policy = domain.EndpointPolicy.REGULAR,
  })
  same(domain.Descriptor.FIND_FORWARD, motion_plan.descriptor)
  same(target_plan, motion_plan.target_plan)

  local policy = { search_current_line_only = false }
  local request = domain.MotionRequest.new({
    context = domain.ModeContext.from_full_mode("n"),
    origin = domain.Position.new(1, 1),
    descriptor = "f",
    target = target,
    count = nil,
    policy = policy,
    first_move = true,
  })
  same(1, request.count.value)
  truthy(request.first_move)
  same(policy, request.policy)
  fails(function()
    motion_plan.descriptor = domain.Descriptor.FIND_BACKWARD
  end, "immutable")
end)

test("SearchOutcome distinguishes complete, partial, and first-step failure", function()
  local origin = domain.Position.new(1, 1)
  local reached = domain.Position.new(1, 5)
  local complete = domain.SearchOutcome.complete(reached, 3)
  local partial = domain.SearchOutcome.boundary_after_partial(reached, 2)
  local before = domain.SearchOutcome.boundary_before_any(origin)

  truthy(complete.complete)
  same(domain.SearchStatus.COMPLETE, complete.status)
  falsy(partial.complete)
  same(domain.SearchStatus.BOUNDARY_AFTER_PARTIAL, partial.status)
  same(2, partial.successful_steps)
  same(origin, before.endpoint)
  same(0, before.successful_steps)
  fails(function()
    domain.SearchOutcome.complete(reached, 0)
  end, "successful step")
end)

test("ActionOutcome and DotPayload retain resolved result data", function()
  local target = domain.TargetValue.character("e", 101)
  local payload = domain.DotPayload.new("t", target)
  local destination = domain.Position.new(1, 4)
  local complete = domain.SearchOutcome.complete(destination, 1)
  local movement = domain.ActionOutcome.from_search(complete, "t", payload)

  same(domain.ActionKind.MOVEMENT, movement.kind)
  same(destination, movement.position)
  truthy(movement.complete)
  same(1, movement.successful_steps)
  same(domain.Descriptor.TILL_FORWARD, movement.effective_descriptor)
  same(payload, movement.dot_payload)
  same("t", payload:to_table().descriptor)

  local failed = domain.ActionOutcome.from_search(
    domain.SearchOutcome.boundary_before_any(destination),
    "T"
  )
  same(domain.ActionKind.FAILED_SEARCH, failed.kind)
  same(false, failed.complete)
  same(domain.ActionKind.NEUTRAL, domain.ActionOutcome.neutral(destination).kind)
  same(domain.ActionKind.ESCAPE, domain.ActionOutcome.escape(destination).kind)
  same(domain.ActionKind.EMPTY, domain.ActionOutcome.empty(destination).kind)
  same("problem", domain.ActionOutcome.error(destination, "problem").diagnostic)
end)

test("Capability contract reports every semantic method", function()
  local host = MemoryHost.new()
  same(host, capabilities.assert_implements(host))
  same(0, #capabilities.missing_methods(host))
  local required = capabilities.required_methods()
  truthy(#required >= 30)

  local incomplete = {}
  local missing = capabilities.missing_methods(incomplete)
  truthy(#missing == #required)
  fails(function()
    capabilities.assert_implements(incomplete)
  end, "semantic capabilities")
end)

test("MemoryHost supplies all semantic reads with domain values", function()
  local host = MemoryHost.new({
    buffer_lines = { "alpha", "beta" },
    cursor = { line = 2, byte_column = 2 },
    mode = "nov",
    selection = {
      active = false,
      kind = "none",
      anchor = nil,
      focus = nil,
      option = "exclusive",
    },
    count = 3,
    configuration = {
      enabled = false,
      triggers = { "x", "y" },
    },
    effective_encoding = "cp932",
    macro_register = "q",
    fold_open_policy = { "horizontal" },
    closed_fold_levels = 2,
    pending_operator = "delete",
    time_values_ms = { 10.5, 12 },
  })

  same("alpha", host:read_text():line(1))
  same(domain.Position.new(2, 2), host:read_cursor())
  same("nov", host:read_mode())
  same("no", host:read_mode_context().key)
  same("delete", host:read_pending_operator())
  same(domain.SelectionOption.EXCLUSIVE, host:read_selection().option)
  same(3, host:read_count().value)
  truthy(host:configuration_present("enabled"))
  same(false, host:read_configuration("enabled"))
  local triggers = host:read_configuration("triggers")
  triggers[1] = "changed"
  same("x", host:read_configuration("triggers")[1])
  same("cp932", host:read_encoding())
  truthy(host:read_macro_state().executing)
  truthy(host:read_fold_state():opens("horizontal"))
  same(2, host:read_fold_state().closed_levels)
  same(10.5, host:read_time_ms())
  same(12, host:read_time_ms())
end)

test("MemoryHost queues movement events until action state commits", function()
  local host = MemoryHost.new({
    buffer_lines = { "abc" },
    cursor = { line = 1, byte_column = 1 },
  })
  local state = { committed = false }
  local observations = {}
  host:register_events("CursorMoved", function(name, payload)
    observations[#observations + 1] = {
      name = name,
      position = payload.cursor,
      committed = state.committed,
    }
  end)

  local token = host:begin_action_transition()
  host:apply_cursor(domain.Position.new(1, 2))
  same(1, host:pending_event_count())
  same(0, #observations)
  state.committed = true
  host:commit_action_transition(token)

  same(0, host:pending_event_count())
  same(1, #observations)
  same("CursorMoved", observations[1].name)
  same(domain.Position.new(1, 2), observations[1].position)
  truthy(observations[1].committed)
end)

test("MemoryHost action invocation flushes events after the action callback", function()
  local host = MemoryHost.new({ buffer_lines = { "abc" } })
  local state = { landing = nil }
  local observed_landing
  host:register_events("CursorMoved", function()
    observed_landing = state.landing
  end)
  host:register_action("Move", function()
    local destination = domain.Position.new(1, 3)
    host:apply_cursor(destination)
    state.landing = destination
    return "moved"
  end)

  same("moved", host:invoke_action("Move"))
  same(domain.Position.new(1, 3), observed_landing)
end)

test("MemoryHost models movement, input, folds, and visible effects", function()
  local host = MemoryHost.new({
    buffer_lines = { "abcd" },
    cursor = { line = 1, byte_column = 1 },
    selection = {
      active = true,
      kind = "character",
      anchor = { line = 1, byte_column = 1 },
      focus = { line = 1, byte_column = 1 },
      option = "inclusive",
    },
    input_packets = {
      { kind = "text", text = "d" },
      { kind = "raw_bytes", bytes = { 0x80, 0xfd, 0x60 } },
      { kind = "error", message = "read failed" },
    },
    fold_open_policy = { "all" },
    closed_fold_levels = 2,
  })

  host:apply_selection(domain.Position.new(1, 2), domain.SelectionKind.CHARACTER)
  same(domain.Position.new(1, 2), host:read_cursor())
  same(domain.Position.new(1, 2), host:read_selection().focus)
  host:set_operator_inclusive(true)
  truthy(host:operator_inclusive())

  same("d", host:read_input().text)
  list_same({ 0x80, 0xfd, 0x60 }, host:read_input():bytes())
  fails(function()
    host:read_input()
  end, "read failed")

  truthy(host:open_fold())
  truthy(host:open_fold())
  falsy(host:open_fold())
  same(0, host:read_fold_state().closed_levels)

  host:show_prompt("clever-f: ")
  host:redraw("screen")
  host:redraw("full")
  host:emit_diagnostic("error", "problem")
  same("clever-f: ", host:prompts()[1])
  list_same({ "screen", "full" }, host:redraws())
  same("problem", host:diagnostics()[1].text)
end)

test("MemoryHost manages highlight and cursor-presentation resources", function()
  local presentation = {
    guicursor = "n-v:block",
    terminal_cursor_visible = true,
    hidden = false,
  }
  local host = MemoryHost.new({ cursor_presentation = presentation })
  local highlight = host:create_highlight({
    group = "CleverFChar",
    identity = "char-1",
    position = domain.Position.new(1, 1),
    priority = "high",
  })
  same("char-1", highlight)
  truthy(host:highlights()[highlight] ~= nil)
  truthy(host:remove_highlight(highlight))
  falsy(host:remove_highlight(highlight))

  local lease = host:suppress_cursor_presentation()
  truthy(host:cursor_presentation().hidden)
  truthy(host:restore_cursor_presentation(lease))
  local restored = host:cursor_presentation()
  same(presentation.guicursor, restored.guicursor)
  same(presentation.terminal_cursor_visible, restored.terminal_cursor_visible)
  same(presentation.hidden, restored.hidden)
end)

test("MemoryHost manages timers, events, mappings, and dot repeat", function()
  local host = MemoryHost.new()
  local fired
  truthy(host:supports_timers())
  local timer = host:start_timer(25, function(identity)
    fired = identity
  end)
  truthy(host:timers()[timer].active)
  truthy(host:fire_timer(timer))
  same(timer, fired)
  falsy(host:timers()[timer].active)
  falsy(host:stop_timer(timer))

  local delivered
  local registration = host:register_events({ "InsertEnter", "TextChanged" }, function(name)
    delivered = name
  end, { buffer = "buffer-1" })
  host:deliver_event("InsertEnter", {})
  same("InsertEnter", delivered)
  truthy(host:remove_event_registration(registration))
  delivered = nil
  host:deliver_event("TextChanged", {})
  same(nil, delivered)

  host:register_action("Neutral", function()
    return domain.ActionOutcome.neutral(domain.Position.new(1, 1))
  end)
  local mapping = host:register_mapping(
    { "n", "x", "o" },
    "f",
    "Neutral",
    { silent = true, remap = false }
  )
  same("f", host:mappings()[mapping].lhs)
  truthy(host:mappings()[mapping].options.silent)

  local target = domain.TargetValue.character("a", 97)
  local payload = domain.DotPayload.new("f", target)
  host:register_dot_repeat(payload, function(replayed, count)
    return replayed, count
  end)
  same(payload, host:dot_repeat_payload())
  local replayed, count = host:replay_dot(2)
  same(payload, replayed)
  same(2, count.value)
end)

test("Policy schema contains every default and color target", function()
  local defaults = policy.defaults()
  local false_settings = {
    "search_current_line_only",
    "ignore_case",
    "smart_case",
    "use_migemo",
    "fix_key_direction",
    "show_prompt",
    "mark_direct",
  }
  local true_settings = {
    "mark_cursor",
    "hide_cursor_on_cmdline",
    "mark_char",
    "clean_labels_eagerly",
  }

  for _, name in ipairs(false_settings) do
    same(false, defaults[name], name)
  end
  for _, name in ipairs(true_settings) do
    same(true, defaults[name], name)
  end
  same("", defaults.chars_match_any_signs)
  same(0, defaults.repeat_timeout_ms)
  same(0, defaults.highlight_timeout_ms)
  list_same({ "\r" }, defaults.repeat_last_char_inputs)
  same(nil, policy.default("mark_cursor_color"))
  same(nil, policy.default("mark_char_color"))
  same(nil, policy.default("mark_direct_color"))

  local schema = policy.schema()
  same("Cursor", schema.mark_cursor_color.default_target)
  same("CleverFDefaultLabel", schema.mark_char_color.default_target)
  same("CleverFDefaultLabel", schema.mark_direct_color.default_target)
  same(policy.ValueType.OPTIONAL_GROUP_NAME, schema.mark_cursor_color.value_type)
  same(policy.ValueType.PRESENCE,
    schema[policy.DEFAULT_MAP_SUPPRESSION_SENTINEL].value_type)

  defaults.repeat_last_char_inputs[1] = "changed"
  schema.repeat_last_char_inputs.default[1] = "changed"
  list_same({ "\r" }, policy.default("repeat_last_char_inputs"))
end)

test("Policy typed accessors validate semantic values", function()
  local host = MemoryHost.new({
    configuration = {
      search_current_line_only = true,
      chars_match_any_signs = ";:",
      repeat_last_char_inputs = { "x", "" },
      mark_cursor_color = "IncSearch",
      repeat_timeout_ms = 25,
    },
  })
  local service = policy.new(host)

  truthy(service:get_boolean("search_current_line_only"))
  same(";:", service:get_string("chars_match_any_signs"))
  list_same({ "x", "" }, service:get_string_list("repeat_last_char_inputs"))
  same("IncSearch", service:get_optional_group_name("mark_cursor_color"))
  same(25, service:get_nonnegative_integer("repeat_timeout_ms"))
  same(nil, service:get_optional_group_name("mark_char_color"))

  local inputs = service:get_string_list("repeat_last_char_inputs")
  inputs[1] = "changed"
  same("x", service:get_string_list("repeat_last_char_inputs")[1])

  host:set_configuration("ignore_case", 1)
  fails(function()
    service:get_boolean("ignore_case")
  end, "must be a Boolean")
  host:set_configuration("chars_match_any_signs", {})
  fails(function()
    service:get_string("chars_match_any_signs")
  end, "must be a string")
  host:set_configuration("repeat_last_char_inputs", { "x", 2 })
  fails(function()
    service:get_string_list("repeat_last_char_inputs")
  end, "list of strings")
  host:set_configuration("mark_cursor_color", "")
  fails(function()
    service:get_optional_group_name("mark_cursor_color")
  end, "optional group name")
  host:set_configuration("repeat_timeout_ms", -1)
  fails(function()
    service:get_nonnegative_integer("repeat_timeout_ms")
  end, "nonnegative integer")
  host:set_configuration("repeat_timeout_ms", 1.5)
  fails(function()
    service:get_nonnegative_integer("repeat_timeout_ms")
  end, "nonnegative integer")
  fails(function()
    service:get_string("ignore_case")
  end, "does not have type string")
  fails(function()
    service:get("unknown")
  end, "unknown policy setting")
end)

test("Default-map suppression is presence-based and activation values are retained", function()
  local absent_host = MemoryHost.new()
  local absent_policy = policy.new(absent_host)
  falsy(absent_policy:default_maps_suppressed())
  truthy(absent_policy:capture_activation().install_default_mappings)

  for _, sentinel_value in ipairs({ false, 0 }) do
    local configuration = {
      clean_labels_eagerly = false,
    }
    configuration[policy.DEFAULT_MAP_SUPPRESSION_SENTINEL] = sentinel_value
    local host = MemoryHost.new({ configuration = configuration })
    local service = policy.new(host)

    host:clear_operations()
    truthy(service:default_maps_suppressed())
    local operations = host:operations()
    same(1, #operations)
    same("configuration_present", operations[1].operation)

    local activation = service:capture_activation()
    falsy(activation.install_default_mappings)
    falsy(activation.clean_labels_eagerly)

    host:unset_configuration(policy.DEFAULT_MAP_SUPPRESSION_SENTINEL)
    host:set_configuration("clean_labels_eagerly", true)
    local retained = service:capture_activation()
    falsy(retained.install_default_mappings)
    falsy(retained.clean_labels_eagerly)

    local next_activation = policy.new(host):capture_activation()
    truthy(next_activation.install_default_mappings)
    truthy(next_activation.clean_labels_eagerly)
  end
end)

test("Runtime policy samples read current behavior values", function()
  local host = MemoryHost.new()
  local service = policy.new(host)
  local target = domain.TargetValue.character("a", 97)

  same(domain.SearchScope.BUFFER, service:sample_search().search_scope)
  same(domain.CaseMode.SENSITIVE, service:sample_match(target).case_mode)
  falsy(service:sample_direction().fix_key_direction)
  falsy(service:sample_acquisition().show_prompt)
  truthy(service:sample_markers().mark_char)
  same(0, service:sample_timeouts().repeat_timeout_ms)
  list_same({ "\r" }, service:sample_previous_input().repeat_last_char_inputs)

  host:set_configuration("search_current_line_only", true)
  host:set_configuration("ignore_case", true)
  host:set_configuration("smart_case", true)
  host:set_configuration("use_migemo", true)
  host:set_configuration("chars_match_any_signs", ";")
  host:set_configuration("fix_key_direction", true)
  host:set_configuration("show_prompt", true)
  host:set_configuration("mark_cursor", false)
  host:set_configuration("hide_cursor_on_cmdline", false)
  host:set_configuration("mark_char", false)
  host:set_configuration("mark_direct", true)
  host:set_configuration("repeat_timeout_ms", 75)
  host:set_configuration("highlight_timeout_ms", 125)
  host:set_configuration("repeat_last_char_inputs", { "x", "yz" })

  local search = service:sample_search()
  truthy(search.search_current_line_only)
  same(domain.SearchScope.CURRENT_LINE, search.search_scope)

  local match = service:sample_match(target)
  truthy(match.ignore_case)
  truthy(match.smart_case)
  truthy(match.use_migemo)
  same(";", match.chars_match_any_signs)
  same(domain.CaseMode.INSENSITIVE, match.case_mode)
  truthy(service:sample_direction().fix_key_direction)

  local acquisition = service:sample_acquisition()
  truthy(acquisition.show_prompt)
  falsy(acquisition.mark_cursor)
  falsy(acquisition.hide_cursor_on_cmdline)
  truthy(acquisition.mark_direct)

  local markers = service:sample_markers()
  falsy(markers.mark_cursor)
  falsy(markers.mark_char)
  truthy(markers.mark_direct)

  local timeouts = service:sample_timeouts()
  same(75, timeouts.repeat_timeout_ms)
  same(125, timeouts.highlight_timeout_ms)
  list_same({ "x", "yz" }, service:sample_previous_input().repeat_last_char_inputs)
end)

test("Highlight policy is reevaluated with live feature and color values", function()
  local host = MemoryHost.new()
  local service = policy.new(host)
  service:capture_activation()

  local initial = service:evaluate_highlight_links()
  truthy(initial.CleverFCursor.enabled)
  same(nil, initial.CleverFCursor.configured_target)
  same("Cursor", initial.CleverFCursor.target)
  truthy(initial.CleverFChar.enabled)
  same("CleverFDefaultLabel", initial.CleverFChar.target)
  falsy(initial.CleverFDirect.enabled)
  same("CleverFDefaultLabel", initial.CleverFDirect.target)

  host:set_configuration("mark_cursor", false)
  host:set_configuration("mark_cursor_color", "Search")
  host:set_configuration("mark_char", false)
  host:set_configuration("mark_char_color", "ErrorMsg")
  host:set_configuration("mark_direct", true)
  host:set_configuration("mark_direct_color", "IncSearch")

  local refreshed = service:evaluate_highlight_links()
  falsy(refreshed.CleverFCursor.enabled)
  same("Search", refreshed.CleverFCursor.target)
  falsy(refreshed.CleverFChar.enabled)
  same("ErrorMsg", refreshed.CleverFChar.target)
  truthy(refreshed.CleverFDirect.enabled)
  same("IncSearch", refreshed.CleverFDirect.target)
end)

test("Case policy resolves an explicit mode for each target", function()
  local host = MemoryHost.new()
  local service = policy.new(host)
  local lower = domain.TargetValue.character("a", 97)
  local upper = domain.TargetValue.character("A", 65)
  local multibyte = domain.TargetValue.character("\227\129\130", 0x3042)

  same(domain.CaseMode.SENSITIVE, service:case_mode(lower))
  host:set_configuration("smart_case", true)
  same(domain.CaseMode.INSENSITIVE, service:case_mode(lower))
  same(domain.CaseMode.SENSITIVE, service:case_mode(upper))
  same(domain.CaseMode.SENSITIVE, service:case_mode(multibyte))
  host:set_configuration("ignore_case", true)
  same(domain.CaseMode.INSENSITIVE, service:case_mode(upper))

  local match = service:sample_match(upper)
  local plan = domain.TargetPlan.new({
    target = upper,
    kind = domain.TargetPlanKind.LITERAL,
    case_mode = match.case_mode,
    matcher = function()
      return true
    end,
  })
  same(domain.CaseMode.INSENSITIVE, plan.case_mode)
end)

test("Unsupported Migemo policy mutation updates live configuration", function()
  local host = MemoryHost.new({
    configuration = { use_migemo = true },
  })
  local service = policy.new(host)
  truthy(service:get_boolean("use_migemo"))

  host:clear_operations()
  service:disable_migemo_for_unsupported_encoding()
  local operations = host:operations()
  same(1, #operations)
  same("write_configuration", operations[1].operation)
  same("use_migemo", operations[1].name)
  same(false, operations[1].value)
  falsy(service:get_boolean("use_migemo"))

  host:set_configuration("use_migemo", true)
  truthy(service:get_boolean("use_migemo"))
end)

for _, item in ipairs(tests) do
  local ok, failure = xpcall(item.body, debug.traceback)
  if not ok then
    io.stderr:write("FAIL: " .. item.name .. "\n" .. tostring(failure) .. "\n")
    os.exit(1)
  end
  passed = passed + 1
end

io.stdout:write(string.format("Phase 3: %d tests passed\n", passed))