zc2 0.0.51

P2P compute broker with yen-denominated billing, WAL, and broker mesh support
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
//! What `zc atelier run --dataset FILE` sends the hub to upload a local file
//! as a private dataset (ZC-0028, hub MKT-0022 as amended 2026-11-16).
//!
//! Everything here is local: telling a file from a dataset reference, one
//! pass over the file (its SHA-256, each part's SHA-256 when it goes up in
//! parts, its row count and header), the manifest the hub's
//! `POST /api/datasets/uploads` takes, and reading the hub's answer. The
//! requests themselves are made in `atelier_cli`.
//!
//! The numbers below are the hub's (`api/multipart.py`), which only checks a
//! client's choice of part size and never derives one.

use std::io::Read;
use std::path::{Path, PathBuf};

use base64::Engine;
use serde_json::{json, Value};

const MIB: u64 = 1024 * 1024;
/// A file larger than this goes up in parts; at or below it, one PUT.
pub const MULTIPART_THRESHOLD: u64 = 64 * MIB;
pub const DEFAULT_PART_SIZE: u64 = 32 * MIB;
const MAX_PARTS: u64 = 10_000;
/// The hub reads this much of a file back to check a declared header; a first
/// line longer than this is not declared.
const HEADER_LIMIT: usize = 1 << 20;

/// What a `--dataset` value names.
#[derive(Debug, PartialEq, Eq)]
pub enum DatasetArg {
    /// A hub dataset reference or a URL, sent to the hub as it is.
    Ref(String),
    /// A file on this machine, uploaded first.
    File(PathBuf),
}

/// A value with a scheme (`zc://`, `hf://`, `https://`, `s3://`, `file://`)
/// is a reference whatever is on disk. Otherwise it is a file when a regular
/// file exists at that path, and a reference (a dataset id) when not: a
/// directory or a path that does not exist is sent to the hub unchanged, as
/// before.
pub fn classify_dataset(value: &str) -> DatasetArg {
    if value.contains("://") {
        return DatasetArg::Ref(value.to_string());
    }
    let path = Path::new(value);
    if path.is_file() {
        DatasetArg::File(path.to_path_buf())
    } else {
        DatasetArg::Ref(value.to_string())
    }
}

/// One file, read once: what the hub is told about it.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FilePlan {
    /// The dataset's name on the hub: the file's stem and the start of its
    /// hash, so the same bytes land in the same dataset and different bytes
    /// never become a second version of it (a job reads a dataset's newest
    /// version).
    pub name: String,
    /// The file's path inside the dataset: its file name.
    pub path: String,
    pub size: u64,
    pub sha256: String,
    /// Set, with one hash per part, when the file goes up in parts.
    pub part_size: Option<u64>,
    pub part_sha256s: Vec<String>,
    /// Data rows and header, when the file reads as CSV or JSONL.
    pub tabular: Option<(u64, Vec<String>)>,
}

/// The part size for a file of `size` bytes, or `None` for a single PUT.
/// Grown past the default, to a whole MiB, when the default would need more
/// than 10 000 parts.
pub fn part_size_for(size: u64, threshold: u64, default_part: u64) -> Option<u64> {
    if size <= threshold {
        return None;
    }
    if size.div_ceil(default_part) <= MAX_PARTS {
        return Some(default_part);
    }
    Some(size.div_ceil(MAX_PARTS).div_ceil(MIB) * MIB)
}

/// `(part number from 1, offset, length)` for each part.
pub fn part_ranges(size: u64, part_size: u64) -> Vec<(u64, u64, u64)> {
    let mut out = Vec::new();
    let mut offset = 0;
    while offset < size {
        let length = part_size.min(size - offset);
        out.push((out.len() as u64 + 1, offset, length));
        offset += length;
    }
    out
}

/// A dataset name from a file name and its hash.
pub fn dataset_name(file_name: &str, sha256: &str) -> String {
    let stem = Path::new(file_name)
        .file_stem()
        .and_then(|s| s.to_str())
        .unwrap_or("");
    let mut slug = String::new();
    for c in stem.chars() {
        if c.is_ascii_alphanumeric() || matches!(c, '_' | '.') {
            slug.push(c);
        } else if !slug.ends_with('-') {
            slug.push('-');
        }
    }
    let slug: String = slug.trim_matches(['-', '.']).chars().take(96).collect();
    let slug = if slug.is_empty() { "dataset" } else { &slug };
    format!("{slug}-{}", &sha256[..sha256.len().min(12)])
}

/// The value of `x-amz-checksum-sha256` for a hex SHA-256: base64 of the raw
/// digest, which is what the hub bound into the presigned URL.
pub fn checksum_header(sha256_hex: &str) -> Result<String, String> {
    let raw = hex::decode(sha256_hex).map_err(|_| "a digest is not hex".to_string())?;
    Ok(base64::engine::general_purpose::STANDARD.encode(raw))
}

/// Row count and header, gathered in the pass that hashes the file. The same
/// rule as the hub's web uploader (`web/src/upload/tabular.ts`): the hub
/// checks a declared header against the uploaded bytes and refuses a
/// disagreement, and never checks the row count.
#[derive(Default)]
pub struct TabularScanner {
    first: Option<Vec<u8>>,
    line: Vec<u8>,
    too_long: bool,
    content: bool,
    lines: u64,
    binary: bool,
    any: bool,
}

impl TabularScanner {
    pub fn update(&mut self, chunk: &[u8]) {
        if self.binary {
            return;
        }
        for &b in chunk {
            self.any = true;
            match b {
                0 => {
                    self.binary = true;
                    return;
                }
                b'\n' => self.end_line(),
                _ => {
                    if self.first.is_none() && !self.too_long {
                        if self.line.len() < HEADER_LIMIT {
                            self.line.push(b);
                        } else {
                            self.too_long = true;
                        }
                    }
                    if !matches!(b, b'\r' | b' ' | b'\t') {
                        self.content = true;
                    }
                }
            }
        }
    }

    fn end_line(&mut self) {
        if self.first.is_none() {
            self.first = Some(std::mem::take(&mut self.line));
        }
        if self.content {
            self.lines += 1;
        }
        self.content = false;
    }

    /// `(data rows, header)`, or `None` when the file has no header to
    /// declare (binary, empty, or a first line that is not one).
    pub fn finish(mut self) -> Option<(u64, Vec<String>)> {
        if self.binary || !self.any {
            return None;
        }
        // A last line without a line ending is still a row.
        if self.content {
            self.lines += 1;
        }
        let first = self.first.take().unwrap_or(self.line);
        if self.too_long {
            return None;
        }
        let jsonl = first.iter().find(|b| !matches!(b, b' ' | b'\t')) == Some(&b'{');
        let columns = header_columns(&first)?;
        // A CSV's first line names the columns; every JSONL line is a record.
        let records = self.lines.saturating_sub(u64::from(!jsonl));
        Some((records, columns))
    }
}

/// An object's keys in the order they are written, which is the order the hub
/// reads them in (`serde_json::Value` would sort them).
struct KeysInOrder(Vec<String>);

impl<'de> serde::Deserialize<'de> for KeysInOrder {
    fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
        struct V;
        impl<'de> serde::de::Visitor<'de> for V {
            type Value = KeysInOrder;
            fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
                f.write_str("a JSON object")
            }
            fn visit_map<A: serde::de::MapAccess<'de>>(
                self,
                mut map: A,
            ) -> Result<KeysInOrder, A::Error> {
                let mut keys: Vec<String> = Vec::new();
                while let Some(k) = map.next_key::<String>()? {
                    map.next_value::<serde::de::IgnoredAny>()?;
                    // A repeated key keeps its first position, as a dict does.
                    if !keys.contains(&k) {
                        keys.push(k);
                    }
                }
                Ok(KeysInOrder(keys))
            }
        }
        d.deserialize_map(V)
    }
}

/// Column names from a file's first line: a JSONL record's keys, else the
/// cells of a CSV header.
pub fn header_columns(first_line: &[u8]) -> Option<Vec<String>> {
    let line = std::str::from_utf8(first_line).ok()?;
    let line = line.trim_start_matches('\u{feff}');
    let line = line.strip_suffix('\r').unwrap_or(line);
    if line.trim().is_empty() {
        return None;
    }
    let lead = line.trim_start();
    if lead.starts_with('{') {
        let keys = serde_json::from_str::<KeysInOrder>(line).ok()?.0;
        return (!keys.is_empty()).then_some(keys);
    }
    if lead.starts_with('[') {
        return None;
    }
    split_csv(line)
}

/// One CSV line into trimmed cells, honouring double quotes.
fn split_csv(line: &str) -> Option<Vec<String>> {
    let mut out = Vec::new();
    let mut field = String::new();
    let mut quoted = false;
    let mut chars = line.chars().peekable();
    while let Some(c) = chars.next() {
        if quoted {
            if c == '"' {
                if chars.peek() == Some(&'"') {
                    field.push('"');
                    chars.next();
                } else {
                    quoted = false;
                }
            } else {
                field.push(c);
            }
        } else if c == '"' {
            quoted = true;
        } else if c == ',' {
            out.push(field.trim().to_string());
            field.clear();
        } else {
            field.push(c);
        }
    }
    out.push(field.trim().to_string());
    out.iter().any(|c| !c.is_empty()).then_some(out)
}

/// Read `path` once and describe it for the hub.
pub fn plan_file(path: &Path) -> Result<FilePlan, String> {
    plan_file_with(path, MULTIPART_THRESHOLD, DEFAULT_PART_SIZE)
}

/// `plan_file` with the multipart numbers given, so a test can cut a small
/// file into parts.
pub fn plan_file_with(path: &Path, threshold: u64, default_part: u64) -> Result<FilePlan, String> {
    let shown = path.display();
    let file_name = path
        .file_name()
        .and_then(|n| n.to_str())
        .ok_or_else(|| format!("{shown}: the file name is not valid UTF-8"))?
        .to_string();
    if file_name.contains('\\') || file_name.len() > 512 {
        return Err(format!(
            "{shown}: the hub does not take this file name (a backslash, or over 512 bytes)"
        ));
    }
    let mut file = std::fs::File::open(path).map_err(|e| format!("{shown}: {e}"))?;
    let size = file.metadata().map_err(|e| format!("{shown}: {e}"))?.len();
    if size == 0 {
        return Err(format!("{shown}: the file is empty"));
    }
    let part_size = part_size_for(size, threshold, default_part);

    let mut whole = ring::digest::Context::new(&ring::digest::SHA256);
    let mut part = ring::digest::Context::new(&ring::digest::SHA256);
    let mut in_part = 0u64;
    let mut part_sha256s = Vec::new();
    let mut scanner = TabularScanner::default();
    let mut buf = vec![0u8; 1 << 16];
    let mut total = 0u64;
    loop {
        let n = file.read(&mut buf).map_err(|e| format!("{shown}: {e}"))?;
        if n == 0 {
            break;
        }
        total += n as u64;
        whole.update(&buf[..n]);
        scanner.update(&buf[..n]);
        let mut rest = &buf[..n];
        while let (Some(ps), false) = (part_size, rest.is_empty()) {
            let take = ((ps - in_part) as usize).min(rest.len());
            part.update(&rest[..take]);
            in_part += take as u64;
            rest = &rest[take..];
            if in_part == ps {
                let done =
                    std::mem::replace(&mut part, ring::digest::Context::new(&ring::digest::SHA256));
                part_sha256s.push(hex::encode(done.finish()));
                in_part = 0;
            }
        }
    }
    if total != size {
        return Err(format!("{shown}: the file changed while it was read"));
    }
    if part_size.is_some() && in_part > 0 {
        part_sha256s.push(hex::encode(part.finish()));
    }
    let sha256 = hex::encode(whole.finish());
    Ok(FilePlan {
        name: dataset_name(&file_name, &sha256),
        path: file_name,
        size,
        sha256,
        part_size,
        part_sha256s,
        tabular: scanner.finish(),
    })
}

/// The body of `POST /api/datasets/uploads`: one private dataset, one file.
pub fn open_body(plan: &FilePlan) -> Value {
    let mut file = json!({
        "path": plan.path,
        "sha256": plan.sha256,
        "size_bytes": plan.size,
    });
    if let Some(part_size) = plan.part_size {
        file["part_size"] = json!(part_size);
        file["parts"] = plan
            .part_sha256s
            .iter()
            .enumerate()
            .map(|(i, sha)| json!({"part_number": i + 1, "sha256": sha}))
            .collect();
    }
    if let Some((records, columns)) = &plan.tabular {
        file["meta"] = json!({"records": records, "columns": columns});
    }
    json!({"name": plan.name, "visibility": "private", "files": [file]})
}

/// Where the hub wants the file's bytes.
#[derive(Debug, PartialEq, Eq)]
pub enum Target {
    /// The bytes are already in storage: nothing to send.
    Landed,
    /// One presigned PUT of the whole file.
    Single(String),
    /// One presigned PUT per part: `(part number, url)`.
    Parts(Vec<(u64, String)>),
}

/// The hub's answer to an open.
#[derive(Debug, PartialEq, Eq)]
pub struct Opened {
    pub upload_id: String,
    pub dataset_id: String,
    pub target: Target,
}

/// An id the hub returned, checked before it is put in a path.
fn plain_id(v: &Value, key: &str) -> Result<String, String> {
    let id = v.get(key).and_then(Value::as_str).unwrap_or("");
    if id.is_empty() || !id.chars().all(|c| c.is_ascii_alphanumeric() || c == '-') {
        return Err(format!("the hub's answer has no usable {key}"));
    }
    Ok(id.to_string())
}

/// Read the hub's answer to an open against what was declared.
pub fn parse_open(answer: &Value, plan: &FilePlan) -> Result<Opened, String> {
    let upload_id = plain_id(answer, "upload_id")?;
    let dataset_id = plain_id(answer, "dataset_id")?;
    let entry = answer
        .get("files")
        .and_then(Value::as_array)
        .and_then(|files| {
            files
                .iter()
                .find(|f| f.get("path").and_then(Value::as_str) == Some(plan.path.as_str()))
        });
    let target = match entry {
        None => Target::Landed,
        Some(f) => {
            if let Some(url) = f.get("url").and_then(Value::as_str) {
                Target::Single(url.to_string())
            } else {
                let parts: Vec<(u64, String)> = f
                    .get("parts")
                    .and_then(Value::as_array)
                    .into_iter()
                    .flatten()
                    .filter_map(|p| {
                        Some((
                            p.get("part_number")?.as_u64()?,
                            p.get("url")?.as_str()?.to_string(),
                        ))
                    })
                    .collect();
                // The hub leaves out a part that already landed (an earlier,
                // interrupted upload). Assembling the file needs every part's
                // ETag, and only the request that sent a part was given it.
                let wanted: Vec<u64> = (1..=plan.part_sha256s.len() as u64).collect();
                let offered: Vec<u64> = parts.iter().map(|(n, _)| *n).collect();
                if plan.part_size.is_none()
                    || f.get("part_size").and_then(Value::as_u64) != plan.part_size
                    || offered != wanted
                {
                    return Err(format!(
                        "the hub holds an unfinished upload of these bytes that this command \
                         cannot resume (parts offered: {} of {}); finish or remove it on the \
                         hub's Datasets page, or retry after it expires (24 h)",
                        offered.len(),
                        wanted.len()
                    ));
                }
                Target::Parts(parts)
            }
        }
    };
    Ok(Opened {
        upload_id,
        dataset_id,
        target,
    })
}

/// What a spec's `data.uri` is for a hub dataset version: the hub hands the
/// runner exactly these files and the runner checks them against the digest.
pub fn dataset_uri(dataset_id: &str, digest: &str) -> String {
    format!("zc://dataset/{dataset_id}@sha256:{digest}")
}

/// `spec` with the `uri` of its top-level `data:` block set to `uri`, or
/// `None` when it has no such key written in block style (a spec with no
/// `data` block names its dataset by `dataset_ref` alone). Only that one line
/// changes: the spec is sent as text, comments and all.
pub fn set_data_uri(spec: &str, uri: &str) -> Option<String> {
    let indent = |l: &str| l.len() - l.trim_start_matches([' ', '\t']).len();
    let key_only = |l: &str, key: &str| {
        l.strip_prefix(key)
            .is_some_and(|rest| rest.trim().is_empty() || rest.trim_start().starts_with('#'))
    };
    let mut out = String::with_capacity(spec.len() + uri.len());
    let mut in_data = false;
    let mut child: Option<usize> = None;
    let mut done = false;
    for line in spec.split_inclusive('\n') {
        let body = line.trim_end_matches(['\n', '\r']);
        let blank = body.trim().is_empty() || body.trim_start().starts_with('#');
        if !done && !blank {
            if indent(body) == 0 {
                in_data = key_only(body, "data:");
                child = None;
            } else if in_data {
                let depth = *child.get_or_insert(indent(body));
                if indent(body) == depth && body[depth..].starts_with("uri:") {
                    out.push_str(&body[..depth]);
                    out.push_str(&format!("uri: \"{uri}\""));
                    out.push_str(&line[body.len()..]);
                    done = true;
                    continue;
                }
            }
        }
        out.push_str(line);
    }
    done.then_some(out)
}

#[cfg(test)]
mod tests {
    use super::*;

    fn temp(tag: &str, bytes: &[u8]) -> PathBuf {
        let dir = std::env::temp_dir().join(format!(
            "zc-atelier-upload-{tag}-{}-{:?}",
            std::process::id(),
            std::thread::current().id()
        ));
        let _ = std::fs::remove_dir_all(&dir);
        std::fs::create_dir_all(&dir).unwrap();
        let path = dir.join("train set.csv");
        std::fs::write(&path, bytes).unwrap();
        path
    }

    fn sha(bytes: &[u8]) -> String {
        hex::encode(ring::digest::digest(&ring::digest::SHA256, bytes))
    }

    #[test]
    fn an_existing_file_is_uploaded_and_everything_else_is_a_reference() {
        let file = temp("classify", b"a,b\n1,2\n");
        let s = file.to_str().unwrap();
        assert_eq!(classify_dataset(s), DatasetArg::File(file.clone()));
        // A dataset id, a URL and a hub ref go to the hub as they are.
        for r in [
            "e629e662-1d32-4e52-88e9-b0e83416c852",
            "zc://alice/mine",
            "hf://datasets/org/name",
            "https://example.test/data.csv",
            "s3://bucket/key.csv",
        ] {
            assert_eq!(classify_dataset(r), DatasetArg::Ref(r.to_string()), "{r}");
        }
        // A scheme wins over what is on disk; a directory and a missing path
        // are not files.
        let url = format!("file://{s}");
        assert_eq!(classify_dataset(&url), DatasetArg::Ref(url.clone()));
        let dir = file.parent().unwrap().to_str().unwrap();
        assert_eq!(classify_dataset(dir), DatasetArg::Ref(dir.to_string()));
        let missing = format!("{dir}/nope.csv");
        assert_eq!(classify_dataset(&missing), DatasetArg::Ref(missing.clone()));
    }

    #[test]
    fn a_small_file_is_one_put_and_declares_its_rows_and_header() {
        let bytes = b"id,\"last, first\",score\r\n1,\"a, b\",0.5\r\n2,\"c, d\",0.7\r\n\r\n";
        let path = temp("small", bytes);
        let plan = plan_file(&path).unwrap();
        assert_eq!(plan.path, "train set.csv");
        assert_eq!(plan.size, bytes.len() as u64);
        assert_eq!(plan.sha256, sha(bytes));
        assert_eq!(plan.name, format!("train-set-{}", &sha(bytes)[..12]));
        assert_eq!((plan.part_size, plan.part_sha256s.len()), (None, 0));
        assert_eq!(
            open_body(&plan),
            json!({
                "name": plan.name,
                "visibility": "private",
                "files": [{
                    "path": "train set.csv",
                    "sha256": sha(bytes),
                    "size_bytes": bytes.len(),
                    "meta": {"records": 2, "columns": ["id", "last, first", "score"]},
                }],
            })
        );
    }

    #[test]
    fn a_file_over_the_threshold_declares_each_parts_own_hash() {
        let bytes: Vec<u8> = (0..2500u32).map(|i| (i % 251) as u8).collect();
        let path = temp("parts", &bytes);
        let plan = plan_file_with(&path, 1000, 1000).unwrap();
        assert_eq!(plan.part_size, Some(1000));
        assert_eq!(
            plan.part_sha256s,
            vec![
                sha(&bytes[..1000]),
                sha(&bytes[1000..2000]),
                sha(&bytes[2000..])
            ]
        );
        assert_eq!(plan.sha256, sha(&bytes));
        // Binary: nothing tabular is declared.
        assert_eq!(plan.tabular, None);
        let body = open_body(&plan);
        let file = &body["files"][0];
        assert_eq!(file["part_size"], 1000);
        assert_eq!(
            file["parts"][2],
            json!({"part_number": 3, "sha256": sha(&bytes[2000..])})
        );
        assert!(file.get("meta").is_none());
        // Exactly at the threshold is still one PUT; a whole number of parts
        // leaves no empty last one.
        assert_eq!(plan_file_with(&path, 2500, 1000).unwrap().part_size, None);
        assert_eq!(
            plan_file_with(&path, 100, 500).unwrap().part_sha256s.len(),
            5
        );
    }

    #[test]
    fn part_sizes_follow_the_hubs_rules() {
        assert_eq!(
            part_size_for(64 * MIB, MULTIPART_THRESHOLD, DEFAULT_PART_SIZE),
            None
        );
        assert_eq!(
            part_size_for(64 * MIB + 1, MULTIPART_THRESHOLD, DEFAULT_PART_SIZE),
            Some(32 * MIB)
        );
        // 10 000 parts of 32 MiB is the most the default covers.
        let big = 400_000 * MIB;
        let grown = part_size_for(big, MULTIPART_THRESHOLD, DEFAULT_PART_SIZE).unwrap();
        assert_eq!(grown % MIB, 0);
        assert!(big.div_ceil(grown) <= 10_000 && grown > 32 * MIB);
        assert_eq!(
            part_ranges(2500, 1000),
            vec![(1, 0, 1000), (2, 1000, 1000), (3, 2000, 500)]
        );
        assert_eq!(part_ranges(2000, 1000).len(), 2);
    }

    #[test]
    fn rows_and_header_are_read_as_the_hub_reads_them() {
        let scan = |bytes: &[u8], chunk: usize| {
            let mut s = TabularScanner::default();
            for c in bytes.chunks(chunk) {
                s.update(c);
            }
            s.finish()
        };
        let cols = |v: &[&str]| v.iter().map(|s| s.to_string()).collect::<Vec<_>>();
        // The last row needs no line ending; a chunk boundary changes nothing.
        for chunk in [1, 3, 1000] {
            assert_eq!(
                scan(b"\xef\xbb\xbfa, b ,c\n1,2,3\n\n4,5,6", chunk),
                Some((2, cols(&["a", "b", "c"])))
            );
        }
        // Every JSONL line is a record, and keys keep their written order.
        assert_eq!(
            scan(b"{\"z\": 1, \"a\": {\"k\": 2}}\n{\"z\": 3, \"a\": 4}\n", 5),
            Some((2, cols(&["z", "a"])))
        );
        assert_eq!(
            scan(b"only,a,header", 4),
            Some((0, cols(&["only", "a", "header"])))
        );
        // Nothing to declare: binary, empty, a JSON array, a blank first line.
        assert_eq!(scan(b"PK\x03\x04\x00\x00data\n", 3), None);
        assert_eq!(scan(b"", 3), None);
        assert_eq!(scan(b"[1, 2]\n[3, 4]\n", 3), None);
        assert_eq!(scan(b"\n a,b\n", 3), None);
        assert_eq!(scan(b"{not json\n", 3), None);
        assert_eq!(scan(b"\xff\xfe,a\n1,2\n", 3), None);
    }

    #[test]
    fn a_dataset_is_named_after_the_file_and_its_bytes() {
        let h = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";
        assert_eq!(dataset_name("train.csv", h), "train-0123456789ab");
        assert_eq!(
            dataset_name("My Data (v2).tar.gz", h),
            "My-Data-v2-.tar-0123456789ab"
        );
        assert_eq!(dataset_name("données.csv", h), "donn-es-0123456789ab");
        assert_eq!(dataset_name(".csv", h), "csv-0123456789ab");
        assert_eq!(dataset_name("---", h), "dataset-0123456789ab");
        assert!(dataset_name(&"x".repeat(400), h).len() <= 128);
    }

    #[test]
    fn the_checksum_header_is_base64_of_the_raw_digest() {
        // sha256 of the empty string.
        assert_eq!(
            checksum_header("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")
                .unwrap(),
            "47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU="
        );
        assert!(checksum_header("zz").is_err());
    }

    #[test]
    fn the_specs_data_uri_is_pointed_at_the_uploaded_version_and_nothing_else_moves() {
        let uri = dataset_uri("d-1", &"a".repeat(64));
        assert_eq!(uri, format!("zc://dataset/d-1@sha256:{}", "a".repeat(64)));
        let spec = "# my job\ntask: tabular\ndata:\n  # where\n  format: csv\n  uri: ./train.csv  # local\r\n  target:\n    uri: keep\nmodel:\n  uri: keep-too\n";
        let want = format!("# my job\ntask: tabular\ndata:\n  # where\n  format: csv\n  uri: \"{uri}\"\r\n  target:\n    uri: keep\nmodel:\n  uri: keep-too\n");
        assert_eq!(set_data_uri(spec, &uri).as_deref(), Some(want.as_str()));
        // The last line needs no line ending; a comment after `data:` is fine.
        assert_eq!(
            set_data_uri("data: # in\n    uri: x", "U").as_deref(),
            Some("data: # in\n    uri: \"U\"")
        );
        // Nothing to set: no data block, no uri in it, flow style, or a
        // `uri` that belongs to another block or sits deeper.
        for spec in [
            "task: llm\nmodel_ref: m\n",
            "data:\n  format: csv\n",
            "data: {uri: x}\n",
            "model:\n  uri: x\n",
            "data:\n  source:\n    uri: x\n",
            "metadata:\n  data:\n    uri: x\n",
            "",
        ] {
            assert_eq!(set_data_uri(spec, "U"), None, "{spec:?}");
        }
    }

    fn plan(parts: usize) -> FilePlan {
        FilePlan {
            name: "d-abc".into(),
            path: "d.csv".into(),
            size: 10,
            sha256: "a".repeat(64),
            part_size: (parts > 0).then_some(1000),
            part_sha256s: vec!["b".repeat(64); parts],
            tabular: None,
        }
    }

    #[test]
    fn the_hubs_answer_says_where_the_bytes_go() {
        let ids = json!({"upload_id": "u-1", "dataset_id": "d-1", "digest": "x"});
        let with = |files: Value| {
            let mut v = ids.clone();
            v["files"] = files;
            v
        };
        let single = with(json!([{"path": "d.csv", "url": "https://s3.test/put?sig=1"}]));
        assert_eq!(
            parse_open(&single, &plan(0)).unwrap(),
            Opened {
                upload_id: "u-1".into(),
                dataset_id: "d-1".into(),
                target: Target::Single("https://s3.test/put?sig=1".into()),
            }
        );
        // No entry for the file: its bytes are already there.
        assert_eq!(
            parse_open(&with(json!([])), &plan(0)).unwrap().target,
            Target::Landed
        );
        let parts = with(json!([{"path": "d.csv", "part_size": 1000, "parts": [
            {"part_number": 1, "url": "https://s3.test/p1"},
            {"part_number": 2, "url": "https://s3.test/p2"}]}]));
        assert_eq!(
            parse_open(&parts, &plan(2)).unwrap().target,
            Target::Parts(vec![
                (1, "https://s3.test/p1".into()),
                (2, "https://s3.test/p2".into())
            ])
        );
        // A part the hub no longer offers landed in an earlier attempt.
        let err = parse_open(&parts, &plan(3)).unwrap_err();
        assert!(
            err.contains("cannot resume") && err.contains("2 of 3"),
            "{err}"
        );
        // An id is put in a path, so it must be one.
        let mut bad = single.clone();
        bad["upload_id"] = json!("../x");
        assert!(parse_open(&bad, &plan(0))
            .unwrap_err()
            .contains("upload_id"));
        assert!(parse_open(&json!({}), &plan(0)).is_err());
    }
}