opencrabs 0.3.65

The autonomous, self-improving AI agent. Single Rust binary. Every channel. Install with: cargo install opencrabs
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
//! Tests for the `generate_document` PDF backend (#357).
//!
//! Generated PDFs are read back with `pdf-extract`, the same crate the
//! read side uses, so the round trip proves our own tooling can consume
//! what we write.

use crate::brain::tools::doc_gen::docx::BlockSpec;
use crate::brain::tools::doc_gen::pdf::{StyleSpec, write_pdf};
use serde_json::json;

fn blocks(v: serde_json::Value) -> Vec<BlockSpec> {
    serde_json::from_value(v).expect("valid block specs")
}

#[test]
fn pdf_contains_heading_paragraph_list_and_table_text() {
    let dir = tempfile::tempdir().expect("tempdir");
    let path = dir.path().join("report.pdf");
    let summary = write_pdf(
        &path,
        &blocks(json!([
            {"type": "heading", "text": "Annual Summary", "level": 1},
            {"type": "paragraph", "text": "All systems operated normally."},
            {"type": "list", "items": ["uptime held", "costs flat"], "ordered": true},
            {"type": "table", "rows": [["Metric", "Value"], ["Requests", 12345]]}
        ])),
        "Annual Summary",
        &StyleSpec::default(),
    )
    .expect("pdf written");
    assert!(summary.contains("1 page(s)"));

    let text = pdf_extract::extract_text(&path).expect("pdf extracts");
    assert!(text.contains("Annual Summary"), "text: {text}");
    assert!(text.contains("All systems operated normally."));
    assert!(text.contains("uptime held"));
    assert!(text.contains("1."));
    assert!(text.contains("Requests"));
    assert!(text.contains("12345"));
}

#[test]
fn long_content_breaks_across_pages() {
    let dir = tempfile::tempdir().expect("tempdir");
    let path = dir.path().join("long.pdf");
    let many: Vec<serde_json::Value> = (0..120)
        .map(|i| json!({"type": "paragraph", "text": format!("Paragraph number {i} of filler content.")}))
        .collect();
    let summary =
        write_pdf(&path, &blocks(json!(many)), "Long", &StyleSpec::default()).expect("pdf written");
    let pages: usize = summary
        .split_whitespace()
        .next()
        .and_then(|n| n.parse().ok())
        .expect("summary starts with page count");
    assert!(
        pages >= 2,
        "expected multiple pages, got summary: {summary}"
    );

    let text = pdf_extract::extract_text(&path).expect("pdf extracts");
    assert!(text.contains("Paragraph number 0"));
    assert!(text.contains("Paragraph number 119"));
}

#[test]
fn long_paragraph_wraps_instead_of_overflowing() {
    let dir = tempfile::tempdir().expect("tempdir");
    let path = dir.path().join("wrap.pdf");
    let long_text = "word ".repeat(200);
    let summary = write_pdf(
        &path,
        &blocks(json!([{"type": "paragraph", "text": long_text}])),
        "Wrap",
        &StyleSpec::default(),
    )
    .expect("pdf written");
    // 200 short words cannot fit one line: the layout must emit many lines.
    let lines: usize = summary
        .split("page(s), ")
        .nth(1)
        .and_then(|rest| rest.split_whitespace().next())
        .and_then(|n| n.parse().ok())
        .expect("summary includes line count");
    assert!(lines > 5, "expected wrapped lines, got summary: {summary}");
}

#[test]
fn unicode_text_renders_through_bundled_font() {
    // The bundled DejaVu faces carry real Unicode: punctuation and accents
    // pass through, emoji map to glyphs or tags, and nothing renders as
    // WinAnsi mojibake.
    let dir = tempfile::tempdir().expect("tempdir");
    let path = dir.path().join("unicode.pdf");
    write_pdf(
        &path,
        &blocks(json!([
            {"type": "heading", "text": "Audit — Results ✅"},
            {"type": "paragraph", "text": "It’s “done”… naïve café → next 📱"}
        ])),
        "Audit",
        &StyleSpec::default(),
    )
    .expect("pdf written");
    let text = pdf_extract::extract_text(&path).expect("pdf extracts");
    assert!(text.contains("Audit — Results ✓"), "text: {text}");
    assert!(text.contains("naïve café → next ?"), "text: {text}");
    // No WinAnsi mojibake artifacts.
    assert!(!text.contains("â"), "text: {text}");
}

#[test]
fn table_with_uneven_cell_heights_keeps_all_text() {
    // Column 0 shorter than column 2 used to skip the baseline advance and
    // stack lines on top of each other; every cell line must still land.
    let dir = tempfile::tempdir().expect("tempdir");
    let path = dir.path().join("uneven.pdf");
    write_pdf(
        &path,
        &blocks(json!([
            {"type": "table", "rows": [
                ["Feature", "Status", "Notes"],
                ["A", "ok", "a very long explanation cell that definitely wraps across multiple lines in its column"],
                ["B", "ok", "short"]
            ]}
        ])),
        "Uneven",
        &StyleSpec::default(),
    )
    .expect("pdf written");
    let text = pdf_extract::extract_text(&path).expect("pdf extracts");
    assert!(text.contains("Feature"));
    assert!(text.contains("definitely"));
    assert!(text.contains("wraps"));
    assert!(text.contains("short"));
}

#[test]
fn unbroken_long_words_hard_split() {
    let dir = tempfile::tempdir().expect("tempdir");
    let path = dir.path().join("longword.pdf");
    let long_path = format!("/very/long/{}/file.rs", "sub/".repeat(40));
    write_pdf(
        &path,
        &blocks(json!([{"type": "paragraph", "text": long_path}])),
        "Longword",
        &StyleSpec::default(),
    )
    .expect("pdf written");
    let text = pdf_extract::extract_text(&path).expect("pdf extracts");
    assert!(text.contains("/very/long/"));
    assert!(text.contains("file.rs"));
}

#[test]
fn audit_table_shape_every_wrapped_line_gets_its_own_baseline() {
    // Regression proof for the overlapping-table garble: reproduce the
    // audit-report shape (4 columns, bold header, cells wrapping to
    // different heights) and simulate the emitter's y accounting. Any two
    // lines in the SAME column must land on DIFFERENT baselines; lines on a
    // shared baseline must be in different columns.
    use crate::brain::tools::doc_gen::pdf::{LayoutItem, layout};

    let specs = blocks(json!([
        {"type": "table", "rows": [
            ["Feature", "Adi says OpenClaw", "OpenCrabs actually", "Verdict"],
            ["Basic send/reply/edit/delete", "Yes", "Yes - handler.rs", "Match"],
            ["Photo/Doc/Location/Poll", "Yes", "Yes - telegram_send.rs", "Match"],
            ["Rich messages (Bot API 10.1)", "Yes - tables, formulas",
             "Yes - rich/api.rs with tables, headings, math", "WRONG - WE HAVE IT"],
            ["Streaming preview + reasoning", "Yes - preview stream",
             "Yes - draft_streaming, ProviderStream", "WRONG - WE HAVE IT"]
        ]}
    ]));
    let items = layout(&specs, &StyleSpec::default(), false).expect("layout");

    // Simulate the emitter: negative gap = same baseline, else advance.
    let mut y = 297.0f32 - 20.0;
    let mut placed: Vec<(f32, f32, String)> = Vec::new(); // (y, x, text)
    for item in &items {
        match item {
            LayoutItem::Gap(mm) => y -= mm,
            LayoutItem::Rule { .. } | LayoutItem::RowBg { .. } | LayoutItem::Image { .. } => {}
            LayoutItem::Text(l) => {
                let h = l.size * 0.352_778 * 1.35;
                if l.gap_before_mm >= 0.0 {
                    y -= l.gap_before_mm + h;
                }
                placed.push((y, l.indent_mm, l.text.clone()));
            }
        }
    }
    assert!(placed.len() > 10, "table must produce many lines");
    for (i, a) in placed.iter().enumerate() {
        for b in placed.iter().skip(i + 1) {
            if (a.0 - b.0).abs() < 0.01 {
                assert!(
                    (a.1 - b.1).abs() > 0.01,
                    "two lines share baseline y={} AND column x={}: {:?} vs {:?}",
                    a.0,
                    a.1,
                    a.2,
                    b.2
                );
            }
        }
    }
}

#[test]
fn table_columns_size_to_content() {
    // A single-digit "#" column must stay narrow while verbose columns get
    // the freed space; no column may exceed the max share cap.
    use crate::brain::tools::doc_gen::pdf::{LayoutItem, layout};

    let specs = blocks(json!([
        {"type": "table", "rows": [
            ["#", "Feature", "Verdict"],
            ["1", "Multiple bot accounts", "TRUE GAP"],
            ["2", "Rich messages (image+text)", "WRONG: rich/api.rs"],
            ["17", "Slash commands", "WRONG: commands.toml"]
        ]}
    ]));
    let items = layout(&specs, &StyleSpec::default(), false).expect("layout");
    // Collect the distinct x offsets used by table text: these are the
    // column starts. The second column's start is the first column's width.
    let mut xs: Vec<f32> = items
        .iter()
        .filter_map(|i| match i {
            LayoutItem::Text(l) => Some(l.indent_mm),
            _ => None,
        })
        .collect();
    xs.sort_by(|a, b| a.partial_cmp(b).unwrap());
    xs.dedup_by(|a, b| (*a - *b).abs() < 0.01);
    assert_eq!(xs.len(), 3, "three column starts, got {xs:?}");
    let body = 210.0 - 40.0;
    let col0_width = xs[1] - xs[0];
    assert!(
        col0_width < body * 0.15,
        "digit column must stay narrow, got {col0_width}mm of {body}mm"
    );
    // No column exceeds the cap (45% of body width).
    let col1_width = xs[2] - xs[1];
    assert!(col1_width <= body * 0.45 + 0.1);
}

#[test]
fn tables_emit_header_separator_and_row_rules() {
    use crate::brain::tools::doc_gen::pdf::{LayoutItem, RuleStyle, layout};

    let specs = blocks(json!([
        {"type": "table", "rows": [
            ["A", "B"],
            ["1", "2"],
            ["3", "4"]
        ]}
    ]));
    let items = layout(&specs, &StyleSpec::default(), false).expect("layout");
    let heavy = items
        .iter()
        .filter(|i| {
            matches!(
                i,
                LayoutItem::Rule {
                    style: RuleStyle::HeaderSep,
                    ..
                }
            )
        })
        .count();
    let light = items
        .iter()
        .filter(|i| {
            matches!(
                i,
                LayoutItem::Rule {
                    style: RuleStyle::RowLight,
                    ..
                }
            )
        })
        .count();
    assert_eq!(heavy, 1, "exactly one header separator");
    assert_eq!(light, 1, "one light rule between the two data rows");
}

#[test]
fn styled_pdf_renders_furniture_and_page_numbers() {
    let dir = tempfile::tempdir().expect("tempdir");
    let path = dir.path().join("styled.pdf");
    let style: StyleSpec = serde_json::from_value(json!({
        "accent_color": "#0A84FF",
        "text_color": "#222222",
        "page_header": {"text": "OpenCrabs Research"},
        "page_footer": {"text": "confidential", "page_numbers": true},
        "zebra_rows": true
    }))
    .expect("style parses");
    write_pdf(
        &path,
        &blocks(json!([
            {"type": "heading", "text": "Branded Report", "level": 1},
            {"type": "table", "rows": [["A", "B"], ["1", "2"], ["3", "4"], ["5", "6"]]}
        ])),
        "Branded",
        &style,
    )
    .expect("styled pdf written");
    let text = pdf_extract::extract_text(&path).expect("pdf extracts");
    assert!(text.contains("Branded Report"));
    assert!(text.contains("OpenCrabs Research"));
    assert!(text.contains("confidential"));
    assert!(text.contains("Page 1 of 1"));
}

#[test]
fn invalid_hex_colors_fall_back_to_defaults() {
    let dir = tempfile::tempdir().expect("tempdir");
    let path = dir.path().join("badhex.pdf");
    let style: StyleSpec = serde_json::from_value(json!({
        "accent_color": "not-a-color",
        "text_color": "#zzzzzz"
    }))
    .expect("style parses");
    write_pdf(
        &path,
        &blocks(json!([{"type": "heading", "text": "Still Works"}])),
        "Bad hex",
        &style,
    )
    .expect("bad hex never fails generation");
    let text = pdf_extract::extract_text(&path).expect("pdf extracts");
    assert!(text.contains("Still Works"));
}

#[test]
fn logo_embeds_when_readable_and_skips_when_missing() {
    // A tiny valid PNG (1x1 white pixel).
    const PNG_1X1: &[u8] = &[
        0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0x00, 0x00, 0x00, 0x0D, 0x49, 0x48, 0x44,
        0x52, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x08, 0x02, 0x00, 0x00, 0x00, 0x90,
        0x77, 0x53, 0xDE, 0x00, 0x00, 0x00, 0x0C, 0x49, 0x44, 0x41, 0x54, 0x08, 0xD7, 0x63, 0xF8,
        0xFF, 0xFF, 0x3F, 0x00, 0x05, 0xFE, 0x02, 0xFE, 0xDC, 0xCC, 0x59, 0xE7, 0x00, 0x00, 0x00,
        0x00, 0x49, 0x45, 0x4E, 0x44, 0xAE, 0x42, 0x60, 0x82,
    ];
    let dir = tempfile::tempdir().expect("tempdir");
    let logo_path = dir.path().join("logo.png");
    std::fs::write(&logo_path, PNG_1X1).expect("logo written");

    let path = dir.path().join("withlogo.pdf");
    let style: StyleSpec = serde_json::from_value(json!({
        "page_header": {"text": "Brand", "logo_path": logo_path.to_string_lossy()},
    }))
    .expect("style parses");
    write_pdf(
        &path,
        &blocks(json!([{"type": "paragraph", "text": "content"}])),
        "Logo",
        &style,
    )
    .expect("pdf with logo written");
    // The PDF must embed an image XObject.
    let raw = std::fs::read(&path).expect("pdf readable");
    let hay = String::from_utf8_lossy(&raw);
    assert!(
        hay.contains("/XObject") || hay.contains("/Image"),
        "image embedded"
    );

    // Missing logo file: generation still succeeds, just without the image.
    let path2 = dir.path().join("nologo.pdf");
    let style2: StyleSpec = serde_json::from_value(json!({
        "page_header": {"text": "Brand", "logo_path": "/nonexistent/logo.png"},
    }))
    .expect("style parses");
    write_pdf(
        &path2,
        &blocks(json!([{"type": "paragraph", "text": "content"}])),
        "Logo",
        &style2,
    )
    .expect("missing logo never fails generation");
    let text = pdf_extract::extract_text(&path2).expect("pdf extracts");
    assert!(text.contains("content"));
}

const PNG_1X1_DOC: &[u8] = &[
    0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0x00, 0x00, 0x00, 0x0D, 0x49, 0x48, 0x44, 0x52,
    0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x08, 0x02, 0x00, 0x00, 0x00, 0x90, 0x77, 0x53,
    0xDE, 0x00, 0x00, 0x00, 0x0C, 0x49, 0x44, 0x41, 0x54, 0x08, 0xD7, 0x63, 0xF8, 0xFF, 0xFF, 0x3F,
    0x00, 0x05, 0xFE, 0x02, 0xFE, 0xDC, 0xCC, 0x59, 0xE7, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4E,
    0x44, 0xAE, 0x42, 0x60, 0x82,
];

#[test]
fn image_block_embeds_and_captions_in_pdf() {
    let dir = tempfile::tempdir().expect("tempdir");
    let img = dir.path().join("chart.png");
    std::fs::write(&img, PNG_1X1_DOC).expect("png written");
    let path = dir.path().join("visual.pdf");
    write_pdf(
        &path,
        &blocks(json!([
            {"type": "heading", "text": "Visual Report"},
            {"type": "image", "path": img.to_string_lossy(), "width_mm": 60.0,
             "caption": "Bracket diagram"},
            {"type": "paragraph", "text": "After the image."}
        ])),
        "Visual",
        &StyleSpec::default(),
    )
    .expect("pdf with image written");
    let raw = std::fs::read(&path).expect("pdf readable");
    let hay = String::from_utf8_lossy(&raw);
    assert!(
        hay.contains("/XObject") || hay.contains("/Image"),
        "image embedded"
    );
    let text = pdf_extract::extract_text(&path).expect("pdf extracts");
    assert!(text.contains("Bracket diagram"), "caption rendered: {text}");
    assert!(text.contains("After the image."));
    // The decode must actually succeed: printpdf's `images` feature is
    // required (#426). Without it every embed fell into the visible
    // placeholder branch, and this test's XObject check missed it.
    assert!(
        !text.contains("could not be embedded"),
        "image decoded, not placeholder: {text}"
    );
}

#[test]
fn missing_image_path_fails_with_clear_error() {
    let dir = tempfile::tempdir().expect("tempdir");
    let path = dir.path().join("broken.pdf");
    let err = write_pdf(
        &path,
        &blocks(json!([{"type": "image", "path": "/nonexistent/chart.png"}])),
        "Broken",
        &StyleSpec::default(),
    )
    .expect_err("missing image must fail generation");
    let msg = err.to_string();
    assert!(
        msg.contains("/nonexistent/chart.png"),
        "error names the path: {msg}"
    );
}