test(workspace): cover pure helpers in commands/workspace.rs (R1.5) - #149
Merged
Conversation
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
force-pushed
the
clai/test/workspace-pure-fn-coverage
branch
from
August 4, 2026 14:32
d90d9e9 to
c400372
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Six pure or near-pure functions in
src-tauri/src/commands/workspace.rspreviously 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 testsblock — no production code is modified,bindings.tsis untouched, and the diff is purely additive (16 tests, +236 lines, 0 deletions).What's covered
viewer_for_pathcanvas/*.dashboard.json/json/markdown/html), case-insensitive extension,textfallback, spot-check one peris_external_viewer_extgroupmime_for_pathoctet-streamfallback for unknown and no-extensioncollect_filesskip_clai=trueexcludes protected dirs at any depth, error path returnsErrfor an unreadable parentartifact_tree_statsMAX_ARTIFACT_COUNTform counts all three nested filesresolve_copy_source..before touching fs, rejects.clai/imagesvia the SKIPPED_ARTIFACT_DIRS guard, resolves a happy-path fileimage_media_type_from_extensionImplements R1.5. Source: R1 finding F5.
Local verification
The two pre-existing test-mode clippy errors in
src/config/mod.rs(lines 1008 and 1020) are unaffected — they exist onmainand only appear withcargo clippy --tests, which is not part of CI's gate (cargo clippy -- -D warningsagainst the lib target only).