coremlit 0.1.2

Safe, synchronous CoreML runtime for macOS (CPU/GPU/Neural Engine) with opt-in on-device multimodal pipelines: speech (Whisper STT, forced alignment, speaker diarization, Silero VAD), AudioSet sound-event tagging, and audio/text/image embeddings (CLAP, granite, SigLIP)
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
use std::path::{Path, PathBuf};

use tokenizers::Tokenizer;

use super::*;
use crate::audio::align::acoustic::{
  AcousticGeometry, Granularity, LetterCase, OutputKind, Tokenization,
};

/// The chordai base960h CTC vocabulary, byte-for-byte from
/// `Models/alignkit/base960h_dict.json` (SHA-256
/// `ef41495ab958d4416ad2f81ea51a77d4a3c79cace96e92e978c443c7bfbdd2e5`, the
/// same file `tests/model_io.rs` pins), transcribed per this module's `#
/// Generator note`, id-ascending, one entry per dict key. Not re-read from
/// `Models/` at test time so these tests stay hermetic — no
/// `ALIGNKIT_TEST_MODELS` download required.
const DICT_ENTRIES: [(&str, u32); VOCAB_SIZE] = [
  ("-", 0),
  ("|", 1),
  ("E", 2),
  ("T", 3),
  ("A", 4),
  ("O", 5),
  ("N", 6),
  ("I", 7),
  ("H", 8),
  ("S", 9),
  ("R", 10),
  ("D", 11),
  ("L", 12),
  ("U", 13),
  ("M", 14),
  ("W", 15),
  ("C", 16),
  ("F", 17),
  ("G", 18),
  ("Y", 19),
  ("P", 20),
  ("B", 21),
  ("V", 22),
  ("K", 23),
  ("'", 24),
  ("X", 25),
  ("J", 26),
  ("Q", 27),
  ("Z", 28),
];

/// Path to the committed asset on disk. `CARGO_MANIFEST_DIR` is a
/// compile-time constant naming this crate's own source tree, which is
/// exactly what test binaries run against — unlike `tokenizer_json_bytes`'s
/// rustdoc caution about using it as a *runtime* asset path, that concern
/// doesn't apply here.
fn asset_path() -> PathBuf {
  Path::new(env!("CARGO_MANIFEST_DIR"))
    .join("src/audio/align/assets/chordai_base960h_tokenizer.json")
}

/// Parses the embedded asset. Same call asry's `load_tokenizer_with_compat`
/// makes on its fast path (`asry/src/runner/aligner/aligner.rs:1203`):
/// `Tokenizer::from_bytes`, never `from_file`.
fn load_tokenizer() -> Tokenizer {
  Tokenizer::from_bytes(tokenizer_json_bytes()).expect("embedded asset must parse")
}

#[test]
fn vocab_size_is_29() {
  assert_eq!(VOCAB_SIZE, 29);
  assert_eq!(DICT_ENTRIES.len(), 29);
}

#[test]
fn blank_id_is_zero() {
  assert_eq!(BLANK_ID, 0);
}

#[test]
fn word_delimiter_is_pipe() {
  assert_eq!(WORD_DELIMITER, "|");
}

#[test]
fn embedded_bytes_match_the_committed_file_on_disk() {
  let disk_bytes = std::fs::read(asset_path()).expect("committed asset must be readable");
  assert_eq!(
    disk_bytes,
    tokenizer_json_bytes(),
    "include_bytes! must reflect the committed asset exactly"
  );
}

/// Mirrors asry's exact tokenizer-loading call shape end to end
/// (`asry/src/runner/aligner/aligner.rs:1198-1206`,
/// `load_tokenizer_with_compat`): read the path to bytes with
/// `std::fs::read`, then `Tokenizer::from_bytes`. The asset parses on the
/// first attempt — no compat-patch retry — because it already declares
/// `"model": {"type": "WordLevel", ...}` explicitly (see this module's `#
/// Generator note`).
#[test]
fn on_disk_asset_round_trips_through_asrys_loader_shape() {
  let bytes = std::fs::read(asset_path()).expect("read tokenizer asset");
  let tok = Tokenizer::from_bytes(&bytes).expect("Tokenizer::from_bytes parses the asset");
  assert_eq!(tok.get_vocab_size(true), VOCAB_SIZE);
}

/// Mirrors asry's `validate_vocab_dim`
/// (in `asry/src/runner/aligner/algorithm/encode.rs`): the tokenizer's
/// vocab size — with and without added tokens; this asset's added tokens are
/// entries of its table, at their own ids, so both must agree — has to equal
/// `VOCAB_SIZE` EXACTLY.
#[test]
fn tokenizer_vocab_size_matches_vocab_size_exactly() {
  let tok = load_tokenizer();
  assert_eq!(tok.get_vocab_size(true), VOCAB_SIZE);
  assert_eq!(tok.get_vocab_size(false), VOCAB_SIZE);
}

#[test]
fn blank_token_resolves_to_blank_id() {
  let tok = load_tokenizer();
  assert_eq!(tok.token_to_id("-"), Some(BLANK_ID));
}

/// Mirrors asry's `validate_word_delimiter_present`
/// (`asry/src/runner/aligner/aligner.rs:1125-1143`): `token_to_id("|")`
/// must resolve — asry looks the delimiter up dynamically rather than
/// assuming a fixed id, so this test checks the resolved id, not just
/// presence.
#[test]
fn word_delimiter_resolves_via_token_to_id() {
  let tok = load_tokenizer();
  assert_eq!(tok.token_to_id(WORD_DELIMITER), Some(1));
}

/// Round-trip: every dict entry resolves to its exact id through the
/// loaded tokenizer, and the loaded vocab has no extra entries beyond
/// those 29 — the property `Aligner::from_paths`-style construction
/// (design spec §6) depends on to align model output columns with vocab
/// tokens correctly.
#[test]
fn every_dict_entry_round_trips_through_token_to_id() {
  let tok = load_tokenizer();
  for (token, expected_id) in DICT_ENTRIES {
    assert_eq!(
      tok.token_to_id(token),
      Some(expected_id),
      "token {token:?} must resolve to id {expected_id}"
    );
  }
  assert_eq!(
    tok.get_vocab(true).len(),
    VOCAB_SIZE,
    "vocab must contain exactly these 29 entries, no more"
  );
}

// --- Mutation checks -------------------------------------------------
//
// The tests above would pass vacuously if the asset were, say, empty and
// every assertion happened to short-circuit past a parse failure before
// reaching a real check. These prove the loader and the checks above
// actually discriminate a corrupted asset from a valid one. Both mutate
// the embedded bytes in memory (`tokenizer_json_bytes()`), not a temp-dir
// copy — no filesystem needed, so these stay hermetic too.

/// Structural corruption: truncate the asset mid-object. Valid UTF-8 (the
/// cut lands on an ASCII byte) but no longer valid JSON, so
/// `Tokenizer::from_bytes` — the same call the loader shape above uses —
/// must reject it outright.
#[test]
fn truncated_asset_is_rejected_by_tokenizer_from_bytes() {
  let bytes = tokenizer_json_bytes();
  let truncated = &bytes[..bytes.len() / 2];
  assert!(
    std::str::from_utf8(truncated).is_ok(),
    "fixture assumption: the halfway point must still land on an ASCII byte"
  );
  assert!(
    Tokenizer::from_bytes(truncated).is_err(),
    "truncated JSON must not parse as a valid tokenizer"
  );
}

/// Semantic corruption #1: shift the `|` delimiter's id from 1 to 91 while
/// keeping the JSON otherwise well-formed (vocab count unchanged). This is
/// exactly the class of bug `validate_word_delimiter_present`-style checks
/// exist to catch: the file still "loads", but the delimiter resolves to
/// the wrong id, which would silently misalign every CTC frame around word
/// boundaries.
#[test]
fn corrupted_delimiter_id_is_caught_by_the_round_trip_check() {
  let text = std::str::from_utf8(tokenizer_json_bytes()).expect("asset is UTF-8");
  let needle = "\"|\": 1,";
  assert!(
    text.contains(needle),
    "fixture assumption: the delimiter's exact `{needle}` line must be present in the asset \
     for this mutation to actually corrupt it"
  );
  let mutated = text.replacen(needle, "\"|\": 91,", 1);
  let tok = Tokenizer::from_bytes(mutated.as_bytes()).expect("still structurally valid JSON");
  // Vocab count is unaffected by the id shift...
  assert_eq!(tok.get_vocab_size(true), VOCAB_SIZE);
  // ...but the id our checks require is no longer what's stored, so the
  // real round-trip assertion this test mirrors
  // (`word_delimiter_resolves_via_token_to_id`) would now fail:
  assert_ne!(tok.token_to_id(WORD_DELIMITER), Some(1));
  assert_eq!(tok.token_to_id(WORD_DELIMITER), Some(91));
}

/// Semantic corruption #2: delete one vocab entry (`"Q": 27,`) outright.
/// Still structurally valid JSON (the entry sits between two others, so
/// removing its whole line doesn't orphan a comma), but the tokenizer's
/// vocab size drops to 28 — exactly the "V != expected_v" case asry's
/// `validate_vocab_dim` exists to reject before it can corrupt an
/// alignment.
#[test]
fn corrupted_vocab_entry_removal_is_caught_by_vocab_size_check() {
  let text = std::str::from_utf8(tokenizer_json_bytes()).expect("asset is UTF-8");
  let needle = "\"Q\": 27,\n";
  assert!(
    text.contains(needle),
    "fixture assumption: the `{needle:?}` line must be present in the asset for this \
     mutation to actually corrupt it"
  );
  let mutated = text.replacen(needle, "", 1);
  let tok = Tokenizer::from_bytes(mutated.as_bytes()).expect("still structurally valid JSON");
  let corrupted_size = tok.get_vocab_size(true);
  assert_ne!(
    corrupted_size, VOCAB_SIZE,
    "removing an entry must change the observed vocab size"
  );
  assert_eq!(corrupted_size, VOCAB_SIZE - 1);
  assert_eq!(tok.token_to_id("Q"), None);
}

// --- Vocabulary: a model's own table, read at run time -----------------

/// SHA-256 of `Models/alignkit/base960h_dict.json`, the pin this module's
/// `# Generator note` records.
const STAGED_DICT_SHA256: &str = "ef41495ab958d4416ad2f81ea51a77d4a3c79cace96e92e978c443c7bfbdd2e5";

/// `base960h_dict.json`'s exact bytes, rebuilt from [`DICT_ENTRIES`] in the
/// file's own spelling — one line, `"token": id` pairs joined by `", "`, no
/// trailing newline — and proved byte-identical by its SHA-256.
fn staged_dict() -> Vec<u8> {
  let entries: Vec<String> = DICT_ENTRIES
    .iter()
    .map(|(token, id)| format!("\"{token}\": {id}"))
    .collect();
  let dict = format!("{{{}}}", entries.join(", ")).into_bytes();
  assert_eq!(
    sha256_hex(&dict),
    STAGED_DICT_SHA256,
    "the rebuilt table must be the staged file, byte for byte"
  );
  dict
}

fn sha256_hex(bytes: &[u8]) -> String {
  use sha2::{Digest, Sha256};
  Sha256::digest(bytes)
    .iter()
    .map(|byte| format!("{byte:02x}"))
    .collect()
}

fn json(bytes: &[u8]) -> serde_json::Value {
  serde_json::from_slice(bytes).expect("JSON")
}

/// **The staged model's own table reads back as the bundled one.** Read
/// through [`Vocabulary::from_json`], `base960h_dict.json` has the bundled
/// table's 29 entries, and the tokenizer document written for it under the
/// staged contract is the committed asset, field for field: the run-time road
/// and the committed asset are the same generator rule set. The model-gated
/// half aligns the staged model through both and compares the words
/// (`tests/align/align_chunk.rs`).
#[test]
fn the_staged_dict_reads_back_as_the_bundled_table() {
  let vocabulary = Vocabulary::from_json(&staged_dict()).expect("the staged table reads");
  assert_eq!(vocabulary.size().get(), VOCAB_SIZE);
  let document = vocabulary.tokenizer_json(&AcousticContract::BASE960H);
  assert_eq!(
    json(&document),
    json(tokenizer_json_bytes()),
    "the written document must be the committed asset"
  );

  let tok = Tokenizer::from_bytes(&document).expect("the document parses");
  for (token, id) in DICT_ENTRIES {
    assert_eq!(tok.token_to_id(token), Some(id), "{token:?}");
  }
}

/// The special added tokens `tokenizer` declares: `(id, content)`, in id order.
fn declared_specials(tokenizer: &Tokenizer) -> Vec<(u32, String)> {
  let mut specials: Vec<(u32, String)> = tokenizer
    .get_added_vocabulary()
    .get_added_tokens_decoder()
    .iter()
    .filter(|(_, token)| token.special)
    .map(|(&id, token)| (id, token.content.clone()))
    .collect();
  specials.sort_unstable();
  specials
}

/// **On the staged contract the document declares exactly the contract's
/// specials as special added tokens**: the blank `-` at id 0 and the
/// delimiter `|` at id 1, the staged contract naming no further special —
/// each at its own vocabulary id, so the table's 29 ids and its size are as
/// they were. asry reads its reserved ids off `added_tokens[].special`, so no
/// transcript character is spelled onto either column. The committed asset
/// and the document the aligner writes say the same.
///
/// Plant: writing the added tokens without their `special` flag leaves the
/// document declaring nothing special, and this test fails (asry still
/// reserves this blank and this delimiter by their own statements to the
/// builder; `aligner::tests` records what the flag alone reserves).
#[test]
fn the_bundled_document_declares_exactly_the_staged_contracts_specials() {
  let staged = [(0u32, "-".to_owned()), (1, "|".to_owned())];
  let written = Vocabulary::bundled().tokenizer_json(&AcousticContract::BASE960H);
  for document in [written.as_slice(), tokenizer_json_bytes()] {
    let value = json(document);
    let added = value["added_tokens"].as_array().expect("an array");
    assert_eq!(added.len(), 2);
    for (entry, (id, content)) in added.iter().zip(&staged) {
      assert_eq!(entry["id"], *id);
      assert_eq!(entry["content"], content.as_str());
      assert_eq!(entry["special"], true);
    }
    let tok = Tokenizer::from_bytes(document).expect("the document parses");
    assert_eq!(declared_specials(&tok), staged);
    assert_eq!(tok.get_vocab_size(true), VOCAB_SIZE);
    assert_eq!(tok.token_to_id("-"), Some(0));
    assert_eq!(tok.token_to_id("|"), Some(1));
  }
  assert_eq!(json(&written), json(tokenizer_json_bytes()));
}

/// A contract of a model's own: blank `blank`, `delimiter`, upper case, the
/// staged geometry, and `specials`.
fn contract_with(
  blank: u32,
  delimiter: WordDelimiter,
  specials: &'static [&'static str],
) -> AcousticContract {
  AcousticContract::new(
    blank,
    AcousticGeometry::WAV2VEC2,
    Tokenization::new(
      delimiter,
      LetterCase::Upper,
      Granularity::Character,
      specials,
    ),
    OutputKind::Logits,
  )
}

/// **A contract's document declares its non-lexical tokens by its own
/// statements**: the token at its blank id, its delimiter, and every special it
/// names that the table spells — HuggingFace's 32-class table under a contract
/// naming `<s>`, `</s>` and `<unk>` declares those, its `<pad>` blank and its
/// `|`, and nothing else; a named special the table does not spell names
/// nothing. Under a contract with no delimiter and no specials the same table
/// declares its blank alone: `|`, `<s>` and `<unk>` are special because a
/// contract says so, never because of how they are spelled.
#[test]
fn a_contracts_document_declares_its_non_lexical_tokens_by_statement() {
  let table = br#"{"<pad>": 0, "<s>": 1, "</s>": 2, "<unk>": 3, "|": 4, "E": 5, "T": 6,
    "A": 7, "O": 8, "N": 9, "I": 10, "H": 11, "S": 12, "R": 13, "D": 14, "L": 15, "U": 16,
    "M": 17, "W": 18, "C": 19, "F": 20, "G": 21, "Y": 22, "P": 23, "B": 24, "V": 25, "K": 26,
    "'": 27, "X": 28, "J": 29, "Q": 30, "Z": 31}"#;
  let vocabulary = Vocabulary::from_json(table).expect("the table reads");
  let parse = |contract: &AcousticContract| {
    Tokenizer::from_bytes(vocabulary.tokenizer_json(contract)).expect("the document parses")
  };

  let hf = parse(&contract_with(
    0,
    WordDelimiter::Pipe,
    &["<s>", "</s>", "<unk>", "<mask>"],
  ));
  assert_eq!(
    declared_specials(&hf),
    [
      (0, "<pad>".to_owned()),
      (1, "<s>".to_owned()),
      (2, "</s>".to_owned()),
      (3, "<unk>".to_owned()),
      (4, "|".to_owned()),
    ]
  );
  assert_eq!(hf.get_vocab_size(true), 32);

  let bare = parse(&contract_with(0, WordDelimiter::Absent, &[]));
  assert_eq!(declared_specials(&bare), [(0, "<pad>".to_owned())]);
  assert_eq!(bare.get_vocab_size(true), 32);
}

/// **The parse drops a special the table spells as the empty string**, the
/// premise of `TokenizationError::EmptySpecial`: the `tokenizers` crate skips
/// an added token whose content is empty (`AddedVocabulary::add_tokens`). The
/// document declares the empty entry at id 3 special beside the blank and the
/// delimiter; parsed, it holds the other two alone, and the table still spells
/// the empty token at 3. Should a `tokenizers` release keep empty added tokens,
/// this law fails, and the refusal can be relaxed.
#[test]
fn the_parse_drops_a_special_spelled_as_the_empty_string() {
  let vocabulary =
    Vocabulary::from_json(br#"{"<pad>": 0, "|": 1, "A": 2, "": 3}"#).expect("the table reads");
  let document = vocabulary.tokenizer_json(&contract_with(0, WordDelimiter::Pipe, &[""]));
  let value = json(&document);
  let declared: Vec<(u64, &str)> = value["added_tokens"]
    .as_array()
    .expect("an array")
    .iter()
    .map(|entry| {
      (
        entry["id"].as_u64().expect("an id"),
        entry["content"].as_str().expect("a content"),
      )
    })
    .collect();
  assert_eq!(declared, [(0, "<pad>"), (1, "|"), (3, "")]);

  let tok = Tokenizer::from_bytes(&document).expect("the document parses");
  assert_eq!(
    declared_specials(&tok),
    [(0, "<pad>".to_owned()), (1, "|".to_owned())]
  );
  assert_eq!(tok.token_to_id(""), Some(3), "the table still spells it");
}

/// The bundled table's token list, which the tokenization check reads, is the
/// committed asset's table in id order — and a table read from JSON keeps its
/// tokens in id order too.
#[test]
fn bundled_tokens_are_the_committed_tables() {
  let vocabulary = Vocabulary::bundled();
  let bundled: Vec<&str> = vocabulary.tokens().collect();
  let staged: Vec<&str> = DICT_ENTRIES.iter().map(|(token, _)| *token).collect();
  assert_eq!(bundled, staged);
  let read = Vocabulary::from_json(&staged_dict()).expect("the staged table reads");
  assert_eq!(read.tokens().collect::<Vec<_>>(), staged);
  assert!(read.contains("|") && read.contains("A") && !read.contains("a"));
}

#[test]
fn bundled_is_the_committed_table() {
  let bundled = Vocabulary::bundled();
  assert_eq!(bundled.size().get(), VOCAB_SIZE);
  assert_eq!(
    json(&bundled.tokenizer_json(&AcousticContract::BASE960H)),
    json(tokenizer_json_bytes())
  );
}

/// **A table carries no blank, whatever its names.** A flat table does not say
/// which column the head scores as "no token", and names are no answer: a table
/// can hold `<blank>` at 0 and an ordinary `<pad>` at 1, a `-` that is the
/// hyphen, or no conventional name at all. Every one of these reads as the
/// plain table it is — each token at its own id, none singled out — and the
/// blank is left to the model's contract, which the aligner checks against the
/// table's ids at load (`aligner::tests`). Written for a contract that states
/// its last entry the blank, no delimiter and no special, a table's document
/// declares that entry special and nothing else, whatever the others are named.
#[test]
fn a_table_carries_no_blank_whatever_its_names() {
  for (table, size) in [
    (
      r#"{"<pad>": 0, "<s>": 1, "</s>": 2, "<unk>": 3, "|": 4, "A": 5}"#,
      6,
    ),
    (r#"{"a": 0, "b": 1, "|": 2, "[UNK]": 3, "[PAD]": 4}"#, 5),
    (r#"{"<blank>": 0, "<pad>": 1, "a": 2}"#, 3),
    (r#"{"-": 0, "<pad>": 1, "a": 2}"#, 3),
    (r#"{"a": 0, "-": 1, "b": 2}"#, 3),
    (r#"{"a": 0, "b": 1}"#, 2),
  ] {
    let vocabulary = Vocabulary::from_json(table.as_bytes()).expect("the table reads");
    assert_eq!(vocabulary.size().get(), size, "{table}");
    let last = size - 1;
    let contract = contract_with(
      u32::try_from(last).expect("a small id"),
      WordDelimiter::Absent,
      &[],
    );
    let document = json(&vocabulary.tokenizer_json(&contract));
    assert_eq!(
      document["model"]["vocab"],
      json(table.as_bytes()),
      "{table}: every token at its own id"
    );
    let added = document["added_tokens"].as_array().expect("an array");
    assert_eq!(added.len(), 1, "{table}: the stated blank alone");
    assert_eq!(added[0]["id"], last, "{table}");
    assert_eq!(
      added[0]["content"],
      vocabulary.tokens().nth(last).expect("the last entry"),
      "{table}"
    );
  }
}

/// **A table that does not name every id once is refused by name.** A CTC
/// head has one column per class and an id is the column its token is scored
/// in, so a table that skips an id leaves a column unnamed, and one that gives
/// two tokens the same id reads one column for both. Both are
/// `VocabularyError::MissingId`, naming the lowest id no token holds.
#[test]
fn a_table_that_skips_or_repeats_an_id_is_refused_by_name() {
  for (table, id) in [
    (r#"{"-": 0, "|": 2}"#, 1),
    (r#"{"-": 0, "a": 0}"#, 1),
    (r#"{"<pad>": 1, "a": 2}"#, 0),
  ] {
    let Err(VocabularyError::MissingId(missing)) = Vocabulary::from_json(table.as_bytes()) else {
      panic!("{table} must be refused as a missing id");
    };
    assert_eq!((missing.id(), missing.entries()), (id, 2), "{table}");
  }
}

/// **A token the object names twice is refused by name.** JSON leaves a
/// repeated key's meaning to the reader — one keeps the first id, another the
/// last — and a map built by insertion silently kept one:
/// `{"<pad>": 0, "A": 0, "A": 1}` collapsed into a valid two-entry table that
/// scored `A` from a column its own file never settled. The object is read
/// entry by entry now, so the repetition is seen and named, even when the ids
/// agree and even when the second spelling is an escape.
#[test]
fn a_token_named_twice_is_refused_by_name() {
  for (table, token) in [
    (r#"{"<pad>": 0, "A": 0, "A": 1}"#, "A"),
    (r#"{"-": 0, "a": 1, "a": 1}"#, "a"),
    (r#"{"<pad>": 0, "A": 1, "A": 2}"#, "A"),
  ] {
    let Err(VocabularyError::DuplicateToken(repeated)) = Vocabulary::from_json(table.as_bytes())
    else {
      panic!("{table} must be refused as a repeated token");
    };
    assert_eq!(repeated, token, "{table}");
  }
}

/// A table that names no token is no vocabulary: a CTC head has at least one
/// class, its blank.
#[test]
fn an_empty_table_is_refused_by_name() {
  assert!(matches!(
    Vocabulary::from_json(b"{}"),
    Err(VocabularyError::Empty)
  ));
}

/// Anything but a flat object of token → non-negative `u32` id is not a table
/// — the bundled `tokenizer.json` document included, whose `version` is a
/// string.
#[test]
fn bytes_that_are_not_a_token_table_are_refused_by_name() {
  let inputs: [&[u8]; 7] = [
    b"[1, 2]",
    br#"{"-": -1}"#,
    br#"{"-": "0"}"#,
    br#"{"-": 1.5}"#,
    br#"{"-": 4294967296}"#,
    b"not json",
    tokenizer_json_bytes(),
  ];
  for input in inputs {
    assert!(
      matches!(Vocabulary::from_json(input), Err(VocabularyError::Parse(_))),
      "{}",
      String::from_utf8_lossy(input)
    );
  }
}

/// [`Vocabulary::from_file`] reads the table that ships beside a model, and a
/// file it cannot read is refused naming that file, with the I/O failure as its
/// source.
#[test]
fn from_file_reads_the_table_beside_a_model() {
  let dir = tempfile::tempdir().expect("a temporary directory");
  let path = dir.path().join("base960h_dict.json");
  std::fs::write(&path, staged_dict()).expect("write the table");
  let vocabulary = Vocabulary::from_file(&path).expect("the table reads");
  assert_eq!(vocabulary.size().get(), VOCAB_SIZE);

  let missing = dir.path().join("absent_dict.json");
  let Err(VocabularyError::Read(read)) = Vocabulary::from_file(&missing) else {
    panic!("an absent file must be refused as a read failure");
  };
  assert_eq!(read.path(), missing);
  let source = std::error::Error::source(&read)
    .and_then(|source| source.downcast_ref::<std::io::Error>())
    .expect("the I/O failure is the source");
  assert_eq!(source.kind(), std::io::ErrorKind::NotFound);
}

#[test]
fn debug_names_the_size() {
  assert_eq!(
    format!("{:?}", Vocabulary::bundled()),
    "Vocabulary { size: 29, .. }"
  );
}