fix: fully clean project adoption artifacts

This commit is contained in:
thanhnv
2026-07-24 11:40:29 +07:00
parent 114340c719
commit e359989a74
5 changed files with 194 additions and 23 deletions
@@ -180,6 +180,12 @@ PID=$(python3 -c 'import json;print(json.load(open("'"$PROJ2"'/.casan/config.jso
[[ "$PID" =~ ^[a-z][a-z0-9-]{1,62}$ ]] && pass "defaulted project id is sanitized ($PID)" || fail "bad default project id ($PID)"
MODE=$(python3 -c 'import json;print(json.load(open("'"$PROJ2"'/.casan/config.json"))["enforcement_mode"])' 2>/dev/null)
[[ "$MODE" == "enforce" ]] && pass "production init defaults to enforce mode" || fail "default mode is not enforce ($MODE)"
DEFAULT_LEVEL=$(python3 -c 'import json;print(json.load(open("'"$PROJ2"'/.casan/config.json"))["target_level"])' 2>/dev/null)
[[ "$DEFAULT_LEVEL" == "1" ]] \
&& [ ! -d "$PROJ2/.gitea" ] \
&& [ ! -d "$PROJ2/apps" ] \
&& pass "existing-project init defaults to core without CI/domain scaffold" \
|| fail "default init did not stay at core (level=$DEFAULT_LEVEL)"
echo "===== ⑦ init MERGES into an existing shell (agents/skills/hooks preserved) ====="
EXP="$WORK/existing"; mkdir -p "$EXP/.claude/agents" "$EXP/.claude/skills" "$EXP/.codex"
@@ -240,6 +246,13 @@ L4RC=0; L4="$WORK/l4"; mkdir -p "$L4"; ( cd "$L4" && "$DKC" init --level 4 --pro
[ "$L4RC" -eq 3 ] && pass "init --level 4 (enterprise) refused (rc=3)" || fail "L4 init not refused (rc=$L4RC)"
LVL=$( ( cd "$L2" && "$DKC" level show --json ) | python3 -c 'import json,sys;print(json.load(sys.stdin)["project_target_level"])' 2>/dev/null)
[ "$LVL" = "2" ] && pass "casan level show reports project target level" || fail "level show wrong ($LVL)"
( cd "$L2" && "$DKC" init --project l2 --client none --non-interactive >/dev/null )
LVL_DOWN=$( ( cd "$L2" && "$DKC" level show --json ) | python3 -c 'import json,sys;print(json.load(sys.stdin)["project_target_level"])' 2>/dev/null)
[ "$LVL_DOWN" = "1" ] \
&& [ ! -d "$L2/.gitea" ] \
&& [ ! -d "$L2/apps" ] \
&& pass "default init downgrades existing DevKit adoption to clean core" \
|| fail "default init left Level 2 artifacts after core downgrade"
echo "===== ⑨ SAFETY: init refuses to adopt a CASAN source hub into itself ====="
HUB="$WORK/fakehub"
@@ -353,7 +366,7 @@ mkdir -p "$UN/.claude"
cat > "$UN/.claude/settings.json" <<'EOF'
{"model":"claude-opus-4-8","hooks":{"PreToolUse":[{"matcher":"*","hooks":[{"type":"command","command":"my-user-hook.sh"}]}]}}
EOF
INIT_OUT=$(cd "$UN" && "$DKC" init --project uninstall-project --client claude --non-interactive)
INIT_OUT=$(cd "$UN" && "$DKC" init --level devkit --project uninstall-project --client claude --non-interactive)
echo "$INIT_OUT" | grep -q "CASAN initialized" \
&& pass "init defaults to concise human-readable output" \
|| fail "init human output missing ($INIT_OUT)"
@@ -366,6 +379,7 @@ fi
'import json,sys; d=json.load(sys.stdin); assert d["status"] == "ready"' \
&& pass "doctor --json preserves the machine-readable contract" \
|| fail "doctor --json is not valid/ready"
echo "project-owned requirement" > "$UN/apps/uninstall-project/domain/input/requirement.md"
UN_OUT=$(cd "$UN" && "$DKC" uninstall)
echo "$UN_OUT" | grep -q "CASAN removed from project" \
&& pass "uninstall emits a concise completion summary" \
@@ -381,6 +395,14 @@ if py_check "$UN/.claude/settings.json" "casan-hook.py"; then
else
pass "uninstall removes only the CASAN hook"
fi
[ ! -e "$UN/.gitea/workflows/casan-ci.yml" ] \
&& [ ! -d "$UN/.gitea" ] \
&& pass "uninstall removes CASAN Gitea workflow and prunes empty .gitea" \
|| fail "uninstall left CASAN-owned .gitea content"
[ -f "$UN/apps/uninstall-project/domain/input/requirement.md" ] \
&& [ ! -f "$UN/apps/uninstall-project/domain/input/architecture.md" ] \
&& pass "uninstall removes unchanged scaffold files but preserves modified project content" \
|| fail "uninstall ownership checksum handling is unsafe"
[ -d "$UN/.specify" ] \
&& pass "uninstall retains runtime evidence unless --purge is explicit" \
|| fail "uninstall removed evidence without --purge"
@@ -389,6 +411,27 @@ fi
&& pass "a follow-up uninstall --purge removes retained evidence" \
|| fail "uninstall --purge did not remove retained evidence"
UN_KEEP="$WORK/uninstall-user-workflow"
mkdir -p "$UN_KEEP/.gitea/workflows"
echo "name: user-workflow" > "$UN_KEEP/.gitea/workflows/user-ci.yml"
( cd "$UN_KEEP" && "$DKC" init --level devkit --project uninstall-user-workflow --client none --non-interactive >/dev/null )
python3 - "$UN_KEEP/.casan/init-manifest.json" <<'PY'
import json
import sys
path = sys.argv[1]
data = json.load(open(path, encoding="utf-8"))
data.pop("manifest_version", None)
data.pop("owned_file_hashes", None)
with open(path, "w", encoding="utf-8") as handle:
json.dump(data, handle, indent=2)
handle.write("\n")
PY
( cd "$UN_KEEP" && "$DKC" uninstall >/dev/null )
[ -f "$UN_KEEP/.gitea/workflows/user-ci.yml" ] \
&& [ ! -f "$UN_KEEP/.gitea/workflows/casan-ci.yml" ] \
&& pass "legacy uninstall preserves user Gitea workflow and removes CASAN workflow" \
|| fail "uninstall removed a user workflow or retained the CASAN workflow"
echo ""
echo "===== HYBRID INSTALL SUMMARY: PASS=$PASS FAIL=$FAIL ====="
[[ "$FAIL" -eq 0 ]] || exit 1