solverforge-cli 2.2.3

CLI for scaffolding and managing SolverForge constraint solver projects
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
use super::*;
use crate::test_support::lock_cwd;
use tempfile::tempdir;

#[test]
fn parse_toml_value_parses_integer_float_bool_and_string() {
    assert_eq!(parse_toml_value("60"), toml::Value::Integer(60));
    assert_eq!(parse_toml_value("1.5"), toml::Value::Float(1.5));
    assert_eq!(parse_toml_value("true"), toml::Value::Boolean(true));
    assert_eq!(parse_toml_value("false"), toml::Value::Boolean(false));
    assert_eq!(
        parse_toml_value("construction"),
        toml::Value::String("construction".to_string())
    );
}

#[test]
fn set_toml_key_sets_top_level_key() {
    let mut doc: toml::Value =
        toml::from_str("[termination]\nseconds_spent_limit = 30\n").expect("valid toml");

    set_toml_key(&mut doc, "name", "demo").expect("top-level set should succeed");

    assert_eq!(doc["name"], toml::Value::String("demo".to_string()));
    assert_eq!(
        doc["termination"]["seconds_spent_limit"],
        toml::Value::Integer(30)
    );
}

#[test]
fn set_toml_key_creates_nested_tables_and_preserves_siblings() {
    let mut doc: toml::Value =
        toml::from_str("[termination]\nscore_calculation_count_limit = 10\n").expect("valid toml");

    set_toml_key(&mut doc, "termination.seconds_spent_limit", "60")
        .expect("nested set should succeed");

    assert_eq!(
        doc["termination"]["seconds_spent_limit"],
        toml::Value::Integer(60)
    );
    assert_eq!(
        doc["termination"]["score_calculation_count_limit"],
        toml::Value::Integer(10)
    );
}

#[test]
fn set_toml_key_updates_array_table_by_index() {
    let mut doc: toml::Value = toml::from_str(
        r#"
[[phases]]
type = "local_search"

[phases.termination]
step_count_limit = 10
"#,
    )
    .expect("valid toml");

    set_toml_key(&mut doc, "phases[0].termination.step_count_limit", "20")
        .expect("array-index set should succeed");

    assert_eq!(
        doc["phases"][0]["termination"]["step_count_limit"],
        toml::Value::Integer(20)
    );
}

#[test]
fn set_toml_key_errors_when_array_index_is_missing() {
    let mut doc: toml::Value =
        toml::from_str("[[phases]]\ntype = \"local_search\"\n").expect("valid toml");

    let err = set_toml_key(&mut doc, "phases[1].termination.step_count_limit", "20")
        .expect_err("missing array index should fail");

    assert_eq!(err.to_string(), "solver.toml `phases` has no index 1");
}

#[test]
fn set_toml_key_errors_when_root_is_not_a_table() {
    let mut doc = toml::Value::String("not-a-table".to_string());

    let err = set_toml_key(&mut doc, "termination.seconds_spent_limit", "60")
        .expect_err("scalar root should fail");

    assert_eq!(err.to_string(), "solver.toml root is not a TOML table");
}

#[test]
fn set_toml_key_errors_when_intermediate_value_is_not_a_table() {
    let mut doc: toml::Value = toml::from_str("termination = 5").expect("valid toml");

    let err = set_toml_key(&mut doc, "termination.seconds_spent_limit", "60")
        .expect_err("scalar intermediate should fail");

    assert_eq!(err.to_string(), "solver.toml root is not a TOML table");
}

#[test]
fn run_set_errors_when_solver_toml_is_missing() {
    let _cwd_guard = lock_cwd();
    let tmp = tempdir().expect("failed to create temp dir");
    let original_dir = std::env::current_dir().expect("failed to read current dir");
    std::env::set_current_dir(tmp.path()).expect("failed to enter temp dir");

    let result = run_set("termination.seconds_spent_limit", "60");

    std::env::set_current_dir(original_dir).expect("failed to restore current dir");

    match result.expect_err("missing solver.toml should fail") {
        CliError::NotInProject { missing } => assert_eq!(missing, "solver.toml"),
        other => panic!("expected NotInProject, got {}", other),
    }
}

#[test]
fn run_set_updates_solver_toml_with_typed_nested_value() {
    let _cwd_guard = lock_cwd();
    let tmp = tempdir().expect("failed to create temp dir");
    let original_dir = std::env::current_dir().expect("failed to read current dir");
    std::env::set_current_dir(tmp.path()).expect("failed to enter temp dir");
    fs::write(
        "solver.toml",
        "[termination]\nscore_calculation_count_limit = 10\n",
    )
    .expect("failed to write solver.toml");

    run_set("termination.seconds_spent_limit", "60").expect("run_set should succeed");

    let saved = fs::read_to_string("solver.toml").expect("failed to read solver.toml");
    std::env::set_current_dir(original_dir).expect("failed to restore current dir");

    let saved_doc: toml::Value = toml::from_str(&saved).expect("saved toml should be valid");
    assert_eq!(
        saved_doc["termination"]["seconds_spent_limit"],
        toml::Value::Integer(60)
    );
    assert_eq!(
        saved_doc["termination"]["score_calculation_count_limit"],
        toml::Value::Integer(10)
    );
}

#[test]
fn run_set_updates_nested_value_in_inline_table() {
    let _cwd_guard = lock_cwd();
    let tmp = tempdir().expect("failed to create temp dir");
    let original_dir = std::env::current_dir().expect("failed to read current dir");
    std::env::set_current_dir(tmp.path()).expect("failed to enter temp dir");
    fs::write(
        "solver.toml",
        "termination = { seconds_spent_limit = 30, score_calculation_count_limit = 10 }\n",
    )
    .expect("failed to write solver.toml");

    run_set("termination.seconds_spent_limit", "60").expect("run_set should succeed");

    let saved = fs::read_to_string("solver.toml").expect("failed to read solver.toml");
    std::env::set_current_dir(original_dir).expect("failed to restore current dir");

    let saved_doc: toml::Value = toml::from_str(&saved).expect("saved toml should be valid");
    assert_eq!(
        saved_doc["termination"]["seconds_spent_limit"],
        toml::Value::Integer(60)
    );
    assert_eq!(
        saved_doc["termination"]["score_calculation_count_limit"],
        toml::Value::Integer(10)
    );
}

#[test]
fn run_set_enables_bounded_candidate_trace() {
    let _cwd_guard = lock_cwd();
    let tmp = tempdir().expect("failed to create temp dir");
    let original_dir = std::env::current_dir().expect("failed to read current dir");
    std::env::set_current_dir(tmp.path()).expect("failed to enter temp dir");
    fs::write("solver.toml", "[termination]\nseconds_spent_limit = 30\n")
        .expect("failed to write solver.toml");

    run_set("candidate_trace.max_entries", "1024").expect("trace setting should succeed");

    let saved = fs::read_to_string("solver.toml").expect("failed to read solver.toml");
    std::env::set_current_dir(original_dir).expect("failed to restore current dir");
    let saved_doc: toml::Value = toml::from_str(&saved).expect("saved toml should be valid");
    assert_eq!(
        saved_doc["candidate_trace"]["max_entries"],
        toml::Value::Integer(1024)
    );
}

#[test]
fn run_set_rejects_zero_candidate_trace_capacity_without_writing() {
    let _cwd_guard = lock_cwd();
    let tmp = tempdir().expect("failed to create temp dir");
    let original_dir = std::env::current_dir().expect("failed to read current dir");
    std::env::set_current_dir(tmp.path()).expect("failed to enter temp dir");
    let original = "[termination]\nseconds_spent_limit = 30\n";
    fs::write("solver.toml", original).expect("failed to write solver.toml");

    let error =
        run_set("candidate_trace.max_entries", "0").expect_err("zero trace capacity should fail");

    let saved = fs::read_to_string("solver.toml").expect("failed to read solver.toml");
    std::env::set_current_dir(original_dir).expect("failed to restore current dir");
    assert_eq!(saved, original);
    assert!(error
        .to_string()
        .contains("candidate_trace.max_entries must be a positive TOML integer"));
}

#[test]
fn run_set_stores_string_values_as_strings() {
    let _cwd_guard = lock_cwd();
    let tmp = tempdir().expect("failed to create temp dir");
    let original_dir = std::env::current_dir().expect("failed to read current dir");
    std::env::set_current_dir(tmp.path()).expect("failed to enter temp dir");
    fs::write("solver.toml", "[phase]\nenabled = true\n").expect("failed to write solver.toml");

    run_set("phase.name", "construction").expect("run_set should succeed");

    let saved = fs::read_to_string("solver.toml").expect("failed to read solver.toml");
    std::env::set_current_dir(original_dir).expect("failed to restore current dir");

    let saved_doc: toml::Value = toml::from_str(&saved).expect("saved toml should be valid");
    assert_eq!(
        saved_doc["phase"]["name"],
        toml::Value::String("construction".to_string())
    );
    assert_eq!(saved_doc["phase"]["enabled"], toml::Value::Boolean(true));
}

#[test]
fn run_set_reports_parse_errors_for_invalid_toml() {
    let _cwd_guard = lock_cwd();
    let tmp = tempdir().expect("failed to create temp dir");
    let original_dir = std::env::current_dir().expect("failed to read current dir");
    std::env::set_current_dir(tmp.path()).expect("failed to enter temp dir");
    fs::write("solver.toml", "[termination").expect("failed to write invalid solver.toml");

    let err =
        run_set("termination.seconds_spent_limit", "60").expect_err("invalid toml should fail");

    std::env::set_current_dir(original_dir).expect("failed to restore current dir");

    assert!(err.to_string().starts_with("failed to parse solver.toml:"));
}

#[test]
fn run_show_errors_when_solver_toml_is_missing() {
    let _cwd_guard = lock_cwd();
    let tmp = tempdir().expect("failed to create temp dir");
    let original_dir = std::env::current_dir().expect("failed to read current dir");
    std::env::set_current_dir(tmp.path()).expect("failed to enter temp dir");

    let result = run_show();

    std::env::set_current_dir(original_dir).expect("failed to restore current dir");

    match result.expect_err("missing solver.toml should fail") {
        CliError::NotInProject { missing } => assert_eq!(missing, "solver.toml"),
        other => panic!("expected NotInProject, got {}", other),
    }
}

#[test]
fn run_show_succeeds_when_solver_toml_exists() {
    let _cwd_guard = lock_cwd();
    let tmp = tempdir().expect("failed to create temp dir");
    let original_dir = std::env::current_dir().expect("failed to read current dir");
    std::env::set_current_dir(tmp.path()).expect("failed to enter temp dir");
    fs::write("solver.toml", "[termination]\nseconds_spent_limit = 60\n")
        .expect("failed to write solver.toml");

    let result = run_show();

    std::env::set_current_dir(original_dir).expect("failed to restore current dir");

    assert!(
        result.is_ok(),
        "run_show should succeed when solver.toml exists"
    );
}

#[test]
fn run_set_preserves_managed_solver_config_blocks_for_non_phase_keys() {
    let _cwd_guard = lock_cwd();
    let tmp = tempdir().expect("failed to create temp dir");
    let original_dir = std::env::current_dir().expect("failed to read current dir");
    std::env::set_current_dir(tmp.path()).expect("failed to enter temp dir");
    fs::write(
        "solver.toml",
        r#"
[termination]
score_calculation_count_limit = 10

# @solverforge:begin solver-config
# @solverforge:owner scalar-group required_assignment construction
[[phases]]
type = "construction_heuristic"
construction_heuristic_type = "first_fit"
construction_obligation = "assign_when_candidate_exists"
group_name = "required_assignment"
# @solverforge:end solver-config
"#,
    )
    .expect("failed to write solver.toml");

    run_set("termination.seconds_spent_limit", "60").expect("run_set should succeed");

    let saved = fs::read_to_string("solver.toml").expect("failed to read solver.toml");
    std::env::set_current_dir(original_dir).expect("failed to restore current dir");

    assert!(saved.contains("seconds_spent_limit = 60"));
    assert!(saved.contains("# @solverforge:begin solver-config"));
    assert!(saved.contains("# @solverforge:owner scalar-group required_assignment construction"));
    assert!(saved.contains("group_name = \"required_assignment\""));
    assert!(saved.contains("# @solverforge:end solver-config"));
}

#[test]
fn run_set_preserves_interleaved_managed_solver_phase_order_for_non_phase_keys() {
    let _cwd_guard = lock_cwd();
    let tmp = tempdir().expect("failed to create temp dir");
    let original_dir = std::env::current_dir().expect("failed to read current dir");
    std::env::set_current_dir(tmp.path()).expect("failed to enter temp dir");
    fs::write(
        "solver.toml",
        r#"
[[phases]]
type = "construction_heuristic"
construction_heuristic_type = "first_fit"

# @solverforge:begin solver-config
# @solverforge:owner scalar-group required_assignment construction
[[phases]]
type = "construction_heuristic"
construction_heuristic_type = "first_fit"
construction_obligation = "assign_when_candidate_exists"
group_name = "required_assignment"
# @solverforge:owner scalar-group required_assignment search
[[phases]]
type = "local_search"

[phases.move_selector]
type = "grouped_scalar_move_selector"
group_name = "required_assignment"
require_hard_improvement = true
# @solverforge:end solver-config

[[phases]]
type = "local_search"

[phases.acceptor]
type = "late_acceptance"
late_acceptance_size = 400

[termination]
score_calculation_count_limit = 10
"#,
    )
    .expect("failed to write solver.toml");
    let before = fs::read_to_string("solver.toml").expect("failed to read solver.toml");

    run_set("termination.seconds_spent_limit", "60").expect("run_set should succeed");

    let saved = fs::read_to_string("solver.toml").expect("failed to read solver.toml");
    std::env::set_current_dir(original_dir).expect("failed to restore current dir");

    let user_construction = saved
        .find("construction_heuristic_type = \"first_fit\"")
        .expect("user construction should remain");
    let managed_region = saved
        .find("# @solverforge:begin solver-config")
        .expect("managed region should remain");
    let user_search = saved
        .rfind("late_acceptance_size = 400")
        .expect("user local search should remain");
    assert!(user_construction < managed_region);
    assert!(managed_region < user_search);
    assert!(saved.contains("seconds_spent_limit = 60"));
    assert!(before.contains("# @solverforge:begin solver-config"));
}

#[test]
fn run_set_rejects_phase_edits() {
    let _cwd_guard = lock_cwd();
    let tmp = tempdir().expect("failed to create temp dir");
    let original_dir = std::env::current_dir().expect("failed to read current dir");
    std::env::set_current_dir(tmp.path()).expect("failed to enter temp dir");
    fs::write(
        "solver.toml",
        r#"
[[phases]]
type = "local_search"
"#,
    )
    .expect("failed to write solver.toml");
    let before = fs::read_to_string("solver.toml").expect("failed to read solver.toml");

    let err =
        run_set("phases[0].type", "construction_heuristic").expect_err("phase edits should fail");

    assert!(err
        .to_string()
        .contains("cannot edit ordered solver.toml `phases`"));
    assert_eq!(
        fs::read_to_string("solver.toml").expect("failed to read solver.toml"),
        before
    );
    std::env::set_current_dir(original_dir).expect("failed to restore current dir");
}