Skip to content

Commit 234fa8f

Browse files
test(exec): add tilde-expansion edge-case tests for ExecApprovalsStore (#750)
Add two tests that cover previously-untested branches of ExpandHomePrefix: 1. ResolveAsync_TildeOnlyStateDir_ResolvesToEffectiveHome - stateDirOverride == "~" (tilde alone, no trailing separator) must resolve to the effectiveHome directory. This exercises the `path == "~"` branch of ExpandHomePrefix that is distinct from the `path.StartsWith("~/")` branch covered by existing tests. 2. ResolveAsync_TildePrefixedOpenClawHome_ExpandsRelativeToOsHome - openClawHomeOverride == "~/subdir" is itself tilde-prefixed and must be expanded relative to osHomeOverride before it is used as the base for further tilde expansion in stateDirOverride. Both tests also verify that file migration from the legacy data path to the resolved state path succeeds and that the migrated file content is preserved. Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Copilot <[email protected]>
1 parent 429be9b commit 234fa8f

1 file changed

Lines changed: 57 additions & 0 deletions

File tree

‎tests/OpenClaw.Shared.Tests/ExecApprovalsStoreTests.cs‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -826,6 +826,63 @@ public void ResolveReadOnly_LegacyAskFallback_MapsToSecurity(string legacyValue,
826826
Assert.Equal(expected, resolved.Defaults.AskFallback);
827827
}
828828

829+
// ── State dir: tilde-only expansion ──────────────────────────────────────
830+
831+
/// <summary>
832+
/// A stateDirOverride of exactly "~" (no trailing separator) must resolve to the
833+
/// effective home directory. This exercises the path == "~" branch of ExpandHomePrefix.
834+
/// </summary>
835+
[Fact]
836+
public async Task ResolveAsync_TildeOnlyStateDir_ResolvesToEffectiveHome()
837+
{
838+
// Legacy file at _dir; stateDir = effectiveHome (via "~")
839+
WriteFile(MinimalFileWithAgent("main", "full"));
840+
var openClawHome = Path.Combine(_dir, "effective-home");
841+
var osHome = Path.Combine(_dir, "os-home");
842+
843+
var store = new ExecApprovalsStore(
844+
_dir,
845+
_log,
846+
stateDirOverride: "~",
847+
openClawHomeOverride: openClawHome,
848+
osHomeOverride: osHome);
849+
850+
var resolved = await store.ResolveAsync("main");
851+
852+
// Migration should have moved the legacy file to effectiveHome.
853+
Assert.Equal(ExecSecurity.Full, resolved.Defaults.Security);
854+
Assert.True(File.Exists(Path.Combine(openClawHome, "exec-approvals.json")),
855+
"exec-approvals.json should have been migrated to the effective-home state dir");
856+
}
857+
858+
/// <summary>
859+
/// When openClawHomeOverride itself starts with "~/" it is expanded relative to
860+
/// osHomeOverride before being used as the base for further tilde expansion in
861+
/// stateDirOverride.
862+
/// </summary>
863+
[Fact]
864+
public async Task ResolveAsync_TildePrefixedOpenClawHome_ExpandsRelativeToOsHome()
865+
{
866+
WriteFile(MinimalFileWithAgent("main", "allowlist"));
867+
var osHome = Path.Combine(_dir, "os-home");
868+
var sep = Path.DirectorySeparatorChar;
869+
870+
var store = new ExecApprovalsStore(
871+
_dir,
872+
_log,
873+
stateDirOverride: $"~{sep}custom-state",
874+
openClawHomeOverride: $"~{sep}.openclaw", // should expand to osHome/.openclaw
875+
osHomeOverride: osHome);
876+
877+
var resolved = await store.ResolveAsync("main");
878+
879+
// effectiveHome = osHome/.openclaw; stateDir = osHome/.openclaw/custom-state
880+
var expectedFile = Path.Combine(osHome, ".openclaw", "custom-state", "exec-approvals.json");
881+
Assert.Equal(ExecSecurity.Allowlist, resolved.Defaults.Security);
882+
Assert.True(File.Exists(expectedFile),
883+
$"exec-approvals.json should be at {expectedFile}");
884+
}
885+
829886
// ── Helpers ───────────────────────────────────────────────────────────────
830887

831888
private static string MinimalFile() => """{"version":1,"agents":{}}""";

0 commit comments

Comments
 (0)