Skip to content

test(workspace): cover pure helpers in commands/workspace.rs (R1.5) - #149

Merged
juacker merged 1 commit into
mainfrom
clai/test/workspace-pure-fn-coverage
Aug 4, 2026
Merged

test(workspace): cover pure helpers in commands/workspace.rs (R1.5)#149
juacker merged 1 commit into
mainfrom
clai/test/workspace-pure-fn-coverage

Conversation

@juacker

@juacker juacker commented Aug 4, 2026

Copy link
Copy Markdown
Collaborator

Six pure or near-pure functions in src-tauri/src/commands/workspace.rs previously had no unit tests: collect_files, artifact_tree_stats, viewer_for_path, mime_for_path, resolve_copy_source, image_media_type_from_extension. Several had silent branches (unknown MIME default, empty relative path, skip-clai behaviour at depth) that a regression could change without flagging until the frontend hit the wrong code path.

This PR adds 16 unit tests inside the existing #[cfg(test)] mod tests block — no production code is modified, bindings.ts is untouched, and the diff is purely additive (16 tests, +236 lines, 0 deletions).

What's covered

Function Tests
viewer_for_path named routes (canvas / *.dashboard.json / json / markdown / html), case-insensitive extension, text fallback, spot-check one per is_external_viewer_ext group
mime_for_path spot-check across css/js/json/svg/image/video/audio/text, octet-stream fallback for unknown and no-extension
collect_files recursive walk records relative paths, skip_clai=true excludes protected dirs at any depth, error path returns Err for an unreadable parent
artifact_tree_stats public MAX_ARTIFACT_COUNT form counts all three nested files
resolve_copy_source rejects empty/whitespace, rejects .. before touching fs, rejects .clai/images via the SKIPPED_ARTIFACT_DIRS guard, resolves a happy-path file
image_media_type_from_extension allowlist hits for png/jpg/jpeg/gif/webp (case-insensitive), allowlist rejects for svg/txt/zip/no-ext

Implements R1.5. Source: R1 finding F5.

Local verification

cargo clippy --lib -- -D warnings    # 0 errors
cargo test --lib                     # 932 passed, 0 failed (was 914)

The two pre-existing test-mode clippy errors in src/config/mod.rs (lines 1008 and 1020) are unaffected — they exist on main and only appear with cargo clippy --tests, which is not part of CI's gate (cargo clippy -- -D warnings against the lib target only).

Six pure or near-pure functions in commands/workspace.rs previously had
no unit tests: collect_files, artifact_tree_stats, viewer_for_path,
mime_for_path, resolve_copy_source, image_media_type_from_extension.
Several had silent branches (unknown MIME default, empty relative path,
skip-clai behaviour) that a regression in one of them could change
without flagging until the frontend hit the wrong code path.

This change pins each of those branches from inside the existing
'cfg(test) mod tests' block — no production code is modified. New tests:

- viewer_for_path_distinguishes_named_routes (canvas / dashboard.json /
  json / markdown / html)
- viewer_for_path_extension_is_case_insensitive (uppercase ext routes
  the same way as lowercase)
- viewer_for_path_falls_back_to_text_for_unknown (no extension and
  unknown extension both default to the in-app text preview)
- viewer_for_path_routes_known_external_extensions (spot-check one per
  named group in is_external_viewer_ext)
- mime_for_path_maps_known_extensions (spot-check across css/js/json/
  svg/image/video/audio/text)
- mime_for_path_falls_back_to_octet_stream_for_unknown (default arms
  both genuinely-unknown and no-extension paths)
- collect_files_walks_recursively_and_records_relative_paths (three
  files at three depths, asserts the relative path is rooted)
- collect_files_skip_clai_excludes_protected_directories_at_any_depth
  (mirrors the existing 'skip-clai' behaviour and proves it holds at
  depth, not just at the first component)
- collect_files_returns_an_error_for_unreadable_path (a missing
  parent path surfaces as Err, not a silent empty walk)
- artifact_tree_stats_counts_all_files_for_the_public_form (the
  public cap=MAX_ARTIFACT_COUNT form, exercised with a tree below
  the cap)
- resolve_copy_source_rejects_empty_relative_path (empty and
  whitespace-only input both error)
- resolve_copy_source_rejects_parent_traversal ('..' refused before
  any fs call)
- resolve_copy_source_rejects_protected_directory_components
  ('.clai/images' refused via the SKIPPED_ARTIFACT_DIRS guard)
- resolve_copy_source_resolves_a_normal_relative_file (a happy-path
  file resolves to a path under the root, file not flagged as dir,
  display name set)
- image_media_type_from_extension_recognises_supported_image_extensions
  (png/jpg/jpeg/gif/webp; case-insensitive)
- image_media_type_from_extension_rejects_anything_beyond_the_allowlist
  (svg/txt/zip/no-ext all None)

Three test fixes were folded into this same commit after CI surfaced
issues a Linux-only local run could not:

* rustfmt: two long assert! lines in the new tests exceeded the
  100-column default and were re-wrapped by 'cargo fmt'.
* collect_files order dependence: the assertion on the names vector
  depended on read_dir() iteration order. NTFS does not return
  entries in name order, so the names are now sorted before
  comparing. Order isn't part of the contract being tested.
* resolve_copy_source on Windows: tempdir().path() is returned
  without the '\\?\' UNC prefix that canonicalize() prepends
  on Windows, so an assert!(resolved.starts_with(root)) check
  failed because the two strings used different prefixes. The
  test now canonicalizes the root before passing it, matching
  how production callers reach resolve_copy_source through
  select_session_root(). The function itself already
  canonicalizes internally, so this is purely a contract fix on
  the test side, not a behavioural change.

Local verification (after the amend):
  cargo fmt -- --check                 # clean
  cargo clippy --lib -- -D warnings    # clean
  cargo test --lib                     # 932 passed, 0 failed

The two pre-existing clippy errors in src/config/mod.rs (lines 1008
and 1020, test mode) are unaffected — they exist on main and only
appear with 'cargo clippy --tests', which is not part of CI's gate.
@juacker
juacker force-pushed the clai/test/workspace-pure-fn-coverage branch from d90d9e9 to c400372 Compare August 4, 2026 14:32
@juacker
juacker merged commit 74261a0 into main Aug 4, 2026
2 checks passed
@juacker
juacker deleted the clai/test/workspace-pure-fn-coverage branch August 4, 2026 14:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant