Files
flow/tests/test_domain_remote.py
Tomas Mirchev 6b7a48bb20 refactor: post-review hardening pass
Independent re-audit surfaced 11 follow-ups across two layers of review
(my fresh-eyes read + a parallel agent pass). Bundled into a single
commit because changes are small and intertwined.

Symlink / state consistency:
- FileSystem.same_symlink now uses raw readlink() instead of resolve().
  Aligns the three sites that ask "is this our link?" (_load_state,
  _check_overwrite_safe, remove_symlink) on a single rule: exact-readlink
  match. Following symlink chains would let externally-modified links
  pass as ours and be silently overwritten.
- LinkedState.from_dict raises ConfigError on missing required fields
  instead of .get(..., False) silent defaults. Matches InstalledState.
- LinkOp.source is now consistently None for remove_link ops; the
  service derives expected_source from current.links. Removes the
  asymmetry between in-state and orphan-broken removal ops.
- _apply_plan: rename shadowing local from link_target to spec.

Fail loud:
- _xdg() now treats XDG_CONFIG_HOME="" the same as unset. Previously
  an empty env var produced Path("") and state files were written to
  $PWD instead of ~/.local/state/flow.
- _resolve_target raises PlanConflict when a package contains a bare
  _root entry (no path components) instead of silently dropping it.
- _strip_prefix raises FlowError when a declared install path does not
  start with its section's expected prefix (e.g. etc/foo under install.bin).

Speculative abstraction removed (CLAUDE.md):
- core.template.substitute (the $VAR form) had no production callers --
  deleted along with its tests; only the {{var}} form remains.
- SetupModule base class -- five subclasses, no shared behaviour, no
  polymorphic call site. Deleted.
- Profile.arch -- parsed but never read. Deleted.
- PackagePlan.pm_command -- set but never read. Deleted (service
  recomputes pm_install_command at the call site).
- FileSystem.ensure_dir(mode=...), .copy_file(sudo=...), .read_text(
  default=...) -- no callers. Deleted along with their test.
- bootstrap _execute_action: the upfront `phase not in VALID_PHASES`
  check duplicated the trailing exhaustive raise. Kept the trailing
  raise as the single source of truth; phase set still documented in
  VALID_PHASES.

Completion ctx threading:
- Removed _config()/_manifest() helpers that re-loaded from disk on
  every completion call. _list_targets, _list_namespaces, _list_platforms,
  _list_bootstrap_profiles, _list_manifest_packages now take ctx and
  read from ctx.config / ctx.manifest.

Test coverage and e2e:
- e2e container test exercises a real `flow dotfiles link` (no dry-run)
  and asserts the resulting symlinks point into the dotfiles dir;
  reruns to verify idempotency.
- New tests: LinkedState corrupt-state ConfigError, LinkedState bad-version
  ConfigError, bare-_root PlanConflict, service-level _root path routing
  + skip semantics.
- 11 stale test imports removed (pyflakes clean across src/ + tests/).

357 unit tests + 1 e2e (gated) all pass.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-14 00:23:06 +03:00

86 lines
2.8 KiB
Python

"""Tests for remote domain."""
import pytest
from flow.core.config import TargetConfig
from flow.core.errors import FlowError
from flow.domain.remote.models import Target
from flow.domain.remote.resolution import (
build_ssh_command,
list_targets,
parse_target,
resolve_target,
)
class TestParseTarget:
def test_valid_spec(self):
user, ns, plat = parse_target("personal@orb")
assert user is None
assert ns == "personal"
assert plat == "orb"
def test_valid_spec_with_user(self):
user, ns, plat = parse_target("alice@personal@orb")
assert user == "alice"
assert ns == "personal"
assert plat == "orb"
def test_missing_at_raises(self):
with pytest.raises(FlowError):
parse_target("invalid")
def test_empty_parts_raises(self):
with pytest.raises(FlowError):
parse_target("@orb")
class TestResolveTarget:
def test_found(self):
targets = [TargetConfig(namespace="personal", platform="orb", host="personal.orb")]
result = resolve_target("personal@orb", targets, default_user="tomas")
assert result.host == "personal.orb"
assert result.label == "personal@orb"
assert result.user == "tomas"
def test_not_found(self):
with pytest.raises(FlowError, match="Unknown target"):
resolve_target("missing@host", [], default_user="tomas")
def test_falls_back_to_host_template(self):
result = resolve_target("personal@orb", [], default_user="tomas")
assert result.host == "personal.orb"
class TestBuildSSHCommand:
def test_basic(self):
target = Target(namespace="personal", platform="orb", host="personal.orb", user="tomas")
cmd = build_ssh_command(target)
assert "ssh" in cmd.argv
assert cmd.destination == "tomas@personal.orb"
assert cmd.env["DF_NAMESPACE"] == "personal"
assert "tmux" in cmd.argv
def test_with_identity(self):
target = Target(namespace="work", platform="ec2", host="work.ec2", identity="~/.ssh/id_work", user="tomas")
cmd = build_ssh_command(target)
assert "-i" in cmd.argv
assert "~/.ssh/id_work" in cmd.argv
def test_without_tmux(self):
target = Target(namespace="p", platform="o", host="h", user="tomas")
cmd = build_ssh_command(target, no_tmux=True)
assert "tmux" not in cmd.argv
assert cmd.destination == "tomas@h"
class TestListTargets:
def test_converts_configs(self):
configs = [
TargetConfig(namespace="a", platform="b", host="a.b"),
TargetConfig(namespace="c", platform="d", host="c.d"),
]
targets = list_targets(configs)
assert len(targets) == 2
assert targets[0].label == "a@b"