Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| c26c7d039e | |||
| 4c0cd6b705 | |||
| af7afc6112 | |||
| dc858ad164 | |||
| 2ffd44c694 | |||
| 4f5d683f4b | |||
| df4a0e3f9d | |||
| c3cfbea750 | |||
| a01d1d8f86 |
@@ -23,7 +23,6 @@ import dataclasses
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
import urllib.error
|
||||
import urllib.parse
|
||||
import urllib.request
|
||||
@@ -66,6 +65,11 @@ class ApiError(RuntimeError):
|
||||
pass
|
||||
|
||||
|
||||
class MergePermissionError(ApiError):
|
||||
"""Merge failed with a permanent permission error (403/404/405).
|
||||
The queue should skip this PR and move to the next one."""
|
||||
|
||||
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
class MergeDecision:
|
||||
ready: bool
|
||||
@@ -149,15 +153,38 @@ def latest_statuses_by_context(statuses: list[dict]) -> dict[str, dict]:
|
||||
return latest
|
||||
|
||||
|
||||
def _is_tier_low_pending_ok(
|
||||
latest_statuses: dict[str, dict],
|
||||
context: str,
|
||||
pr_labels: set[str],
|
||||
) -> bool:
|
||||
"""Return True if tier:low PR can tolerate sop-checklist pending state.
|
||||
|
||||
Per sop-checklist-config.yaml tier_failure_mode, tier:low uses soft-fail:
|
||||
sop-checklist posts state=pending when acks are satisfied (missing
|
||||
manager/ceo acks are informational only). The queue should accept
|
||||
pending instead of waiting for success.
|
||||
"""
|
||||
if "tier:low" not in pr_labels:
|
||||
return False
|
||||
if "sop-checklist" not in context:
|
||||
return False
|
||||
status = latest_statuses.get(context) or {}
|
||||
return status_state(status) == "pending"
|
||||
|
||||
|
||||
def required_contexts_green(
|
||||
latest_statuses: dict[str, dict],
|
||||
contexts: list[str],
|
||||
pr_labels: set[str] | None = None,
|
||||
) -> tuple[bool, list[str]]:
|
||||
missing_or_bad: list[str] = []
|
||||
for context in contexts:
|
||||
status = latest_statuses.get(context)
|
||||
state = status_state(status or {})
|
||||
if state != "success":
|
||||
if pr_labels and _is_tier_low_pending_ok(latest_statuses, context, pr_labels):
|
||||
continue # tier:low soft-fail: accept pending sop-checklist
|
||||
missing_or_bad.append(f"{context}={state or 'missing'}")
|
||||
return not missing_or_bad, missing_or_bad
|
||||
|
||||
@@ -210,6 +237,7 @@ def evaluate_merge_readiness(
|
||||
pr_status: dict,
|
||||
required_contexts: list[str],
|
||||
pr_has_current_base: bool,
|
||||
pr_labels: set[str] | None = None,
|
||||
) -> MergeDecision:
|
||||
# Check push-required contexts explicitly instead of combined state.
|
||||
# Combined state can be "failure" due to non-blocking jobs
|
||||
@@ -229,7 +257,7 @@ def evaluate_merge_readiness(
|
||||
# The required_contexts list is the authoritative gate — it includes only
|
||||
# the checks that actually block merges.
|
||||
latest = latest_statuses_by_context(pr_status.get("statuses") or [])
|
||||
ok, missing_or_bad = required_contexts_green(latest, required_contexts)
|
||||
ok, missing_or_bad = required_contexts_green(latest, required_contexts, pr_labels)
|
||||
if not ok:
|
||||
return MergeDecision(False, "wait", "required contexts not green: " + ", ".join(missing_or_bad))
|
||||
return MergeDecision(True, "merge", "ready")
|
||||
@@ -254,27 +282,32 @@ def get_combined_status(sha: str) -> dict:
|
||||
_, combined = api("GET", f"/repos/{OWNER}/{NAME}/commits/{sha}/status")
|
||||
if not isinstance(combined, dict):
|
||||
raise ApiError(f"status for {sha} response not object")
|
||||
# Fetch full statuses list; 200 covers >99% of real-world runs.
|
||||
# The list is ordered ascending by id (oldest first) — callers must
|
||||
# iterate in reverse to get the newest entry per context.
|
||||
# Best-effort: large repos (main with 550+ statuses) may time out.
|
||||
# On timeout, fall back to the statuses[] already in the combined
|
||||
# response (usually 30 entries — enough for most PRs, enough for
|
||||
# main's early push-required contexts).
|
||||
combined_statuses: list[dict] = combined.get("statuses") or []
|
||||
try:
|
||||
_, all_statuses = api(
|
||||
_, all_statuses_raw = api(
|
||||
"GET",
|
||||
f"/repos/{OWNER}/{NAME}/commits/{sha}/statuses",
|
||||
query={"limit": "50"},
|
||||
)
|
||||
if isinstance(all_statuses, list):
|
||||
combined["statuses"] = all_statuses
|
||||
if isinstance(all_statuses_raw, list):
|
||||
all_statuses: list[dict] = list(all_statuses_raw)
|
||||
else:
|
||||
all_statuses = []
|
||||
except (ApiError, urllib.error.URLError, TimeoutError, OSError) as exc:
|
||||
# URLError covers network-level failures (DNS, refused, timeout).
|
||||
# TimeoutError and OSError cover socket-level timeouts.
|
||||
sys.stderr.write(f"::warning::could not fetch full statuses list for {sha[:8]}: {exc}\n")
|
||||
# Fall back to the statuses[] already in the combined response.
|
||||
pass
|
||||
all_statuses = []
|
||||
# Build latest per context: process combined (ascending→reverse=newest
|
||||
# first), then fill gaps from all_statuses (already newest-first).
|
||||
latest: dict[str, dict] = {}
|
||||
for status in reversed(sorted(combined_statuses, key=lambda s: s.get("id") or 0)):
|
||||
ctx = status.get("context")
|
||||
if isinstance(ctx, str) and ctx not in latest:
|
||||
latest[ctx] = status
|
||||
for status in all_statuses:
|
||||
ctx = status.get("context")
|
||||
if isinstance(ctx, str) and ctx not in latest:
|
||||
latest[ctx] = status
|
||||
combined["statuses"] = list(latest.values())
|
||||
return combined
|
||||
|
||||
|
||||
@@ -327,43 +360,6 @@ def update_pull(pr_number: int, *, dry_run: bool) -> None:
|
||||
)
|
||||
|
||||
|
||||
def wait_for_ci(
|
||||
head_sha: str,
|
||||
contexts: list[str],
|
||||
*,
|
||||
max_wait_seconds: int = 300,
|
||||
poll_interval: int = 15,
|
||||
) -> bool:
|
||||
"""Poll CI statuses for head_sha until all required contexts are terminal.
|
||||
|
||||
Returns True if all contexts reached 'success', False if timeout expired
|
||||
(some still pending or failed).
|
||||
|
||||
Background: after a queue-triggered PR update, CI re-runs on the new head.
|
||||
The queue must not update again until CI completes — otherwise the
|
||||
update-then-wait loop keeps the PR in a perpetually-updating state where
|
||||
CI never finishes on any single head.
|
||||
"""
|
||||
deadline = time.time() + max_wait_seconds
|
||||
while time.time() < deadline:
|
||||
time.sleep(poll_interval)
|
||||
try:
|
||||
pr_status = get_combined_status(head_sha)
|
||||
except Exception as exc:
|
||||
sys.stderr.write(f"::warning::wait_for_ci: status fetch failed: {exc}\n")
|
||||
continue
|
||||
latest = latest_statuses_by_context(pr_status.get("statuses") or [])
|
||||
ok, bad = required_contexts_green(latest, contexts)
|
||||
if ok:
|
||||
sys.stderr.write(f"::notice::wait_for_ci: all contexts green after {int(time.time() - (deadline - max_wait_seconds))}s\n")
|
||||
return True
|
||||
# Log progress
|
||||
pending = [f"{c}={latest.get(c, {}).get('status', 'missing')}" for c in contexts if latest.get(c, {}).get('status') != 'success']
|
||||
sys.stderr.write(f"::notice::wait_for_ci: still waiting ({int(deadline - time.time())}s left): {', '.join(pending[:3])}\n")
|
||||
sys.stderr.write(f"::warning::wait_for_ci: timeout after {max_wait_seconds}s; proceeding with merge check\n")
|
||||
return False
|
||||
|
||||
|
||||
def merge_pull(pr_number: int, *, dry_run: bool) -> None:
|
||||
payload = {
|
||||
"Do": "merge",
|
||||
@@ -376,24 +372,16 @@ def merge_pull(pr_number: int, *, dry_run: bool) -> None:
|
||||
print(f"::notice::merging PR #{pr_number}")
|
||||
if dry_run:
|
||||
return
|
||||
# Gitea's merge endpoint returns HTTP 200 with an empty body on success.
|
||||
# The generic api() wrapper raises ApiError on non-2xx, so a 200 with an
|
||||
# empty body reaches the json.loads() path and raises JSONDecodeError,
|
||||
# which api() re-raises as ApiError — making the queue think the merge
|
||||
# failed when it actually succeeded. Work around this by catching the
|
||||
# expected JSONDecodeError here and treating it as success.
|
||||
try:
|
||||
api("POST", f"/repos/{OWNER}/{NAME}/pulls/{pr_number}/merge", body=payload, expect_json=False)
|
||||
except ApiError as exc:
|
||||
# Surface non-merge errors (5xx server errors, 403 forbidden, etc.)
|
||||
if "merge" in str(exc).lower() or "405" in str(exc) or "409" in str(exc):
|
||||
# 405 = PR not mergeable (already merged or CI still running by
|
||||
# the time we got here — the PR will be re-checked next tick)
|
||||
# 409 = merge conflict detected at merge time
|
||||
# In both cases the PR stays open and the next tick re-evaluates.
|
||||
sys.stderr.write(f"::warning::merge call returned: {exc}\n")
|
||||
else:
|
||||
raise
|
||||
# Re-raise permission-like errors so process_once can skip this PR.
|
||||
# 403 = no push access, 404 = repo/pr not found, 405 = not allowed.
|
||||
msg = str(exc)
|
||||
for code in ("403", "404", "405"):
|
||||
if code in msg:
|
||||
raise MergePermissionError(msg) from exc
|
||||
raise # re-raise other ApiErrors unchanged
|
||||
|
||||
|
||||
def process_once(*, dry_run: bool = False) -> int:
|
||||
@@ -435,42 +423,18 @@ def process_once(*, dry_run: bool = False) -> int:
|
||||
commits = get_pull_commits(pr_number)
|
||||
current_base = pr_has_current_base(pr, commits, main_sha)
|
||||
pr_status = get_combined_status(head_sha)
|
||||
pr_labels = label_names(pr)
|
||||
decision = evaluate_merge_readiness(
|
||||
main_status=main_status,
|
||||
pr_status=pr_status,
|
||||
required_contexts=contexts,
|
||||
pr_has_current_base=current_base,
|
||||
pr_labels=pr_labels,
|
||||
)
|
||||
|
||||
print(f"::notice::PR #{pr_number} decision={decision.action}: {decision.reason}")
|
||||
if decision.action == "update":
|
||||
update_pull(pr_number, dry_run=dry_run)
|
||||
# After an update, CI re-runs on the new head. If we check statuses
|
||||
# immediately we see pending (CI not started yet on the new head), so
|
||||
# the next tick updates again — CI never completes on any single head.
|
||||
# Fix: re-fetch the PR to get the new head SHA, then poll CI for up
|
||||
# to 5 min until all required contexts reach terminal state. If CI
|
||||
# finishes in time, proceed to merge on the same tick.
|
||||
if not dry_run:
|
||||
updated_pr = get_pull(pr_number)
|
||||
new_head = updated_pr.get("head", {}).get("sha", "")
|
||||
if new_head and new_head != head_sha:
|
||||
sys.stderr.write(f"::notice::PR #{pr_number}: update created new head {new_head[:8]}; waiting for CI...\n")
|
||||
waited = wait_for_ci(new_head, contexts, max_wait_seconds=300, poll_interval=15)
|
||||
if waited:
|
||||
# CI completed — re-fetch main to confirm it hasn't moved,
|
||||
# then merge immediately without another update cycle.
|
||||
current_main_sha = get_branch_head(WATCH_BRANCH)
|
||||
if current_main_sha != main_sha:
|
||||
sys.stderr.write(f"::notice::PR #{pr_number}: main moved {main_sha[:8]} -> {current_main_sha[:8]}; deferring\n")
|
||||
return 0
|
||||
sys.stderr.write(f"::notice::PR #{pr_number}: CI complete; merging now\n")
|
||||
merge_pull(pr_number, dry_run=dry_run)
|
||||
return 0
|
||||
else:
|
||||
sys.stderr.write(f"::warning::PR #{pr_number}: CI did not finish within 5 min; will retry next tick\n")
|
||||
else:
|
||||
sys.stderr.write(f"::notice::PR #{pr_number}: update did not change head SHA; will retry\n")
|
||||
post_comment(
|
||||
pr_number,
|
||||
(
|
||||
@@ -481,13 +445,6 @@ def process_once(*, dry_run: bool = False) -> int:
|
||||
)
|
||||
return 0
|
||||
if decision.ready:
|
||||
# Re-fetch PR to confirm head hasn't changed since we last checked
|
||||
# (CI may have updated the head while we were evaluating).
|
||||
current_pr = get_pull(pr_number)
|
||||
current_head = current_pr.get("head", {}).get("sha", "")
|
||||
if current_head != head_sha:
|
||||
print(f"::notice::PR #{pr_number} head changed {head_sha[:8]} -> {current_head[:8]}; re-evaluating")
|
||||
return 0
|
||||
latest_main_sha = get_branch_head(WATCH_BRANCH)
|
||||
if latest_main_sha != main_sha:
|
||||
print(
|
||||
@@ -495,7 +452,25 @@ def process_once(*, dry_run: bool = False) -> int:
|
||||
"deferring to next tick"
|
||||
)
|
||||
return 0
|
||||
merge_pull(pr_number, dry_run=dry_run)
|
||||
try:
|
||||
merge_pull(pr_number, dry_run=dry_run)
|
||||
except MergePermissionError as exc:
|
||||
# Permanent merge failure (HTTP 403/404/405). Post a comment so
|
||||
# maintainers know why, then return 0 so this tick is done.
|
||||
# The PR stays in the queue; future ticks can retry after the
|
||||
# permission issue is resolved.
|
||||
sys.stderr.write(f"::error::merge permission error for PR #{pr_number}: {exc}\n")
|
||||
post_comment(
|
||||
pr_number,
|
||||
(
|
||||
"merge-queue: merge failed with HTTP 405 'User not allowed to merge PR'. "
|
||||
"No available token has Can-merge permission on this repo. "
|
||||
"Fix: grant Can-merge to a token, or add a maintain/admin collaborator. "
|
||||
"Skipping to next queued PR on next tick."
|
||||
),
|
||||
dry_run=dry_run,
|
||||
)
|
||||
return 0
|
||||
return 0
|
||||
return 0
|
||||
|
||||
|
||||
@@ -118,3 +118,13 @@ def test_merge_decision_updates_stale_pr_before_merge():
|
||||
|
||||
assert decision.ready is False
|
||||
assert decision.action == "update"
|
||||
|
||||
|
||||
def test_MergePermissionError_inherits_from_ApiError():
|
||||
assert issubclass(mq.MergePermissionError, mq.ApiError)
|
||||
|
||||
|
||||
def test_MergePermissionError_message_preserved():
|
||||
exc = mq.MergePermissionError("POST /merge -> HTTP 405: User not allowed")
|
||||
assert "405" in str(exc)
|
||||
assert "User not allowed" in str(exc)
|
||||
|
||||
@@ -32,12 +32,6 @@ on:
|
||||
# iterating all open PRs when PR_NUMBER is empty.
|
||||
workflow_dispatch:
|
||||
|
||||
# Cancel stale runs so the 8-runner pool stays available for PR jobs.
|
||||
# Per-SHA group ensures push and cron runs at different SHAs don't cancel each other.
|
||||
concurrency:
|
||||
group: gate-check-v3-${{ github.event.pull_request.head.sha || github.sha }}
|
||||
cancel-in-progress: true
|
||||
|
||||
permissions:
|
||||
# read: contents — for checkout (base ref, not PR head for security)
|
||||
# read: pull-requests — for reading PR info via API
|
||||
|
||||
@@ -162,6 +162,7 @@ jobs:
|
||||
exit 1
|
||||
fi
|
||||
python -m twine upload \
|
||||
--verbose \
|
||||
--repository pypi \
|
||||
--username __token__ \
|
||||
--password "$PYPI_TOKEN" \
|
||||
|
||||
@@ -44,12 +44,6 @@ on:
|
||||
- ".github/scripts/lint_secret_pattern_drift.py"
|
||||
- ".githooks/pre-commit"
|
||||
|
||||
# Cancel stale runs to keep the 8-runner pool available for PR jobs.
|
||||
# Per-SHA group ensures push and scheduled runs at different SHAs don't cancel each other.
|
||||
concurrency:
|
||||
group: secret-pattern-drift-${{ github.event.pull_request.head.sha || github.sha }}
|
||||
cancel-in-progress: true
|
||||
|
||||
env:
|
||||
GITHUB_SERVER_URL: https://git.moleculesai.app
|
||||
|
||||
|
||||
@@ -22,11 +22,6 @@ on:
|
||||
- cron: '17 4 * * 1' # Mondays at 04:17 UTC
|
||||
workflow_dispatch:
|
||||
|
||||
# Cancel stale runs to keep the 8-runner pool available for PR jobs.
|
||||
concurrency:
|
||||
group: weekly-platform-go-${{ github.event.pull_request.head.sha || github.sha }}
|
||||
cancel-in-progress: true
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
statuses: write
|
||||
|
||||
@@ -0,0 +1,458 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
// envRequirementKey tests
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
func TestEnvRequirementKey_SingleMember_Pure(t *testing.T) {
|
||||
key := envRequirementKey([]string{"API_KEY"})
|
||||
if key != "API_KEY" {
|
||||
t.Errorf("single member: got %q, want %q", key, "API_KEY")
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnvRequirementKey_TwoMembersSorted_Pure(t *testing.T) {
|
||||
// envRequirementKey sorts before joining, so [B, A] and [A, B] produce same key.
|
||||
keyBA := envRequirementKey([]string{"B", "A"})
|
||||
keyAB := envRequirementKey([]string{"A", "B"})
|
||||
if keyBA != keyAB {
|
||||
t.Errorf("sort invariance: got %q vs %q", keyBA, keyAB)
|
||||
}
|
||||
if keyBA != "A\x00B" {
|
||||
t.Errorf("sort result: got %q, want %q", keyBA, "A\x00B")
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnvRequirementKey_ThreeMembers_Pure(t *testing.T) {
|
||||
key := envRequirementKey([]string{"C", "A", "B"})
|
||||
if key != "A\x00B\x00C" {
|
||||
t.Errorf("three members sorted: got %q, want %q", key, "A\x00B\x00C")
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnvRequirementKey_Empty_Pure(t *testing.T) {
|
||||
key := envRequirementKey([]string{})
|
||||
if key != "" {
|
||||
t.Errorf("empty: got %q, want %q", key, "")
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnvRequirementKey_DedupBySort_Pure(t *testing.T) {
|
||||
// Two lists that are permutations of each other must have identical keys.
|
||||
key1 := envRequirementKey([]string{"Z", "M", "A"})
|
||||
key2 := envRequirementKey([]string{"A", "Z", "M"})
|
||||
if key1 != key2 {
|
||||
t.Errorf("permutation invariance: got %q vs %q", key1, key2)
|
||||
}
|
||||
}
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
// sanitizeEnvMembers tests
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
func TestSanitizeEnvMembers_AllValid_Pure(t *testing.T) {
|
||||
members := []string{"API_KEY", "ANTHROPIC_API_KEY", "SECRET_123"}
|
||||
got, ok := sanitizeEnvMembers(members, "test")
|
||||
if !ok {
|
||||
t.Error("expected ok=true for all valid names")
|
||||
}
|
||||
if len(got) != 3 {
|
||||
t.Errorf("length: got %d, want 3", len(got))
|
||||
}
|
||||
}
|
||||
|
||||
func TestSanitizeEnvMembers_FiltersLowercase_Pure(t *testing.T) {
|
||||
members := []string{"api_key", "ANTHROPIC_API_KEY", "VALID_NAME"}
|
||||
got, ok := sanitizeEnvMembers(members, "test")
|
||||
if !ok {
|
||||
t.Error("expected ok=true for mixed case")
|
||||
}
|
||||
if len(got) != 2 {
|
||||
t.Errorf("length: got %d, want 2", len(got))
|
||||
}
|
||||
want := []string{"ANTHROPIC_API_KEY", "VALID_NAME"}
|
||||
for i, w := range want {
|
||||
if got[i] != w {
|
||||
t.Errorf("got[%d]=%q, want %q", i, got[i], w)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSanitizeEnvMembers_FiltersInvalidCharacters_Pure(t *testing.T) {
|
||||
members := []string{"VALID", "in valid", "ALSO-INVALID", "SPACE KEY", "hyphen-key", ""}
|
||||
got, ok := sanitizeEnvMembers(members, "test")
|
||||
if !ok {
|
||||
t.Error("expected ok=true (VALID was present)")
|
||||
}
|
||||
if len(got) != 1 || got[0] != "VALID" {
|
||||
t.Errorf("got %v, want [VALID]", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSanitizeEnvMembers_AllInvalid_Pure(t *testing.T) {
|
||||
members := []string{"lowercase", "has-dash", "has space"}
|
||||
got, ok := sanitizeEnvMembers(members, "test")
|
||||
if ok {
|
||||
t.Error("expected ok=false when all members invalid")
|
||||
}
|
||||
if len(got) != 0 {
|
||||
t.Errorf("got %v, want []", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSanitizeEnvMembers_EmptyInput_Pure(t *testing.T) {
|
||||
members := []string{}
|
||||
got, ok := sanitizeEnvMembers(members, "test")
|
||||
if ok {
|
||||
t.Error("expected ok=false for empty input")
|
||||
}
|
||||
if len(got) != 0 {
|
||||
t.Errorf("got %v, want []", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSanitizeEnvMembers_EmptyStringNotLogged_Pure(t *testing.T) {
|
||||
// Empty string is filtered silently (not logged) — verify no panic.
|
||||
members := []string{"VALID", ""}
|
||||
got, ok := sanitizeEnvMembers(members, "test")
|
||||
if !ok || len(got) != 1 || got[0] != "VALID" {
|
||||
t.Errorf("unexpected result: ok=%v got=%v", ok, got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSanitizeEnvMembers_StartsWithDigit_Pure(t *testing.T) {
|
||||
members := []string{"123KEY", "VALID_KEY"}
|
||||
got, ok := sanitizeEnvMembers(members, "test")
|
||||
if !ok {
|
||||
t.Error("expected ok=true (VALID_KEY was present)")
|
||||
}
|
||||
if len(got) != 1 || got[0] != "VALID_KEY" {
|
||||
t.Errorf("got %v, want [VALID_KEY]", got)
|
||||
}
|
||||
}
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
// flattenAndSortRequirements tests
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
func TestFlattenAndSortRequirements_SinglesFirst_Pure(t *testing.T) {
|
||||
by := map[string]EnvRequirement{
|
||||
"B": {Name: "B"},
|
||||
"Z": {Name: "Z"},
|
||||
"group1": {AnyOf: []string{"A", "C"}},
|
||||
}
|
||||
got := flattenAndSortRequirements(by)
|
||||
if len(got) != 3 {
|
||||
t.Fatalf("len: got %d, want 3", len(got))
|
||||
}
|
||||
// Singles first, then groups.
|
||||
if got[0].Name == "" {
|
||||
t.Error("first item should be a single")
|
||||
}
|
||||
if got[1].Name == "" {
|
||||
t.Error("second item should be a single")
|
||||
}
|
||||
// Within singles, alphabetical.
|
||||
if got[0].Name != "B" || got[1].Name != "Z" {
|
||||
t.Errorf("got singles %q, %q, want B, Z", got[0].Name, got[1].Name)
|
||||
}
|
||||
// Then groups.
|
||||
if len(got[2].AnyOf) == 0 {
|
||||
t.Error("third item should be a group")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFlattenAndSortRequirements_GroupsSortedByKey_Pure(t *testing.T) {
|
||||
by := map[string]EnvRequirement{
|
||||
"B\x00A": {AnyOf: []string{"B", "A"}}, // key already sorted
|
||||
"A\x00C": {AnyOf: []string{"A", "C"}},
|
||||
}
|
||||
got := flattenAndSortRequirements(by)
|
||||
// After singles, groups sorted by their envRequirementKey.
|
||||
// "A\x00B" < "A\x00C", so the B/A group (key "A\x00B") comes first.
|
||||
group0 := got[len(got)-2]
|
||||
group1 := got[len(got)-1]
|
||||
if group0.AnyOf[0] != "B" || group1.AnyOf[0] != "A" {
|
||||
t.Errorf("group order: got %v then %v, want [B,A] then [A,C]", group0.AnyOf, group1.AnyOf)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFlattenAndSortRequirements_EmptyMap_Pure(t *testing.T) {
|
||||
by := map[string]EnvRequirement{}
|
||||
got := flattenAndSortRequirements(by)
|
||||
if len(got) != 0 {
|
||||
t.Errorf("empty map: got %d items, want 0", len(got))
|
||||
}
|
||||
}
|
||||
|
||||
func TestFlattenAndSortRequirements_OnlyGroups_Pure(t *testing.T) {
|
||||
by := map[string]EnvRequirement{
|
||||
"X\x00Y": {AnyOf: []string{"X", "Y"}},
|
||||
}
|
||||
got := flattenAndSortRequirements(by)
|
||||
if len(got) != 1 {
|
||||
t.Fatalf("len: got %d, want 1", len(got))
|
||||
}
|
||||
if len(got[0].AnyOf) == 0 {
|
||||
t.Error("only group expected")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFlattenAndSortRequirements_Deterministic_Pure(t *testing.T) {
|
||||
// Run twice; results must be identical.
|
||||
by := map[string]EnvRequirement{
|
||||
"C": {Name: "C"},
|
||||
"A": {Name: "A"},
|
||||
"B": {Name: "B"},
|
||||
"Z\x00Y": {AnyOf: []string{"Z", "Y"}},
|
||||
}
|
||||
got1 := flattenAndSortRequirements(by)
|
||||
got2 := flattenAndSortRequirements(by)
|
||||
if len(got1) != len(got2) {
|
||||
t.Fatalf("len mismatch: %d vs %d", len(got1), len(got2))
|
||||
}
|
||||
for i := range got1 {
|
||||
if got1[i].Name != got2[i].Name || len(got1[i].AnyOf) != len(got2[i].AnyOf) {
|
||||
t.Errorf("item %d differs: %+v vs %+v", i, got1[i], got2[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestFlattenAndSortRequirements_AllSingles_Pure(t *testing.T) {
|
||||
by := map[string]EnvRequirement{
|
||||
"X": {Name: "X"},
|
||||
"M": {Name: "M"},
|
||||
"A": {Name: "A"},
|
||||
}
|
||||
got := flattenAndSortRequirements(by)
|
||||
if len(got) != 3 {
|
||||
t.Fatalf("len: got %d, want 3", len(got))
|
||||
}
|
||||
names := make([]string, len(got))
|
||||
for i, r := range got {
|
||||
names[i] = r.Name
|
||||
}
|
||||
// Must be sorted alphabetically.
|
||||
if !sort.IsSorted(sort.StringSlice(names)) {
|
||||
t.Errorf("not sorted: %v", names)
|
||||
}
|
||||
}
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
// collectOrgEnv tests
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
func TestCollectOrgEnv_EmptyTemplate_Pure(t *testing.T) {
|
||||
tmpl := &OrgTemplate{}
|
||||
req, rec := collectOrgEnv(tmpl)
|
||||
if len(req) != 0 || len(rec) != 0 {
|
||||
t.Errorf("empty template: got req=%d rec=%d, want 0,0", len(req), len(rec))
|
||||
}
|
||||
}
|
||||
|
||||
func TestCollectOrgEnv_SingleRequired_Pure(t *testing.T) {
|
||||
tmpl := &OrgTemplate{
|
||||
RequiredEnv: []EnvRequirement{{Name: "API_KEY"}},
|
||||
}
|
||||
req, rec := collectOrgEnv(tmpl)
|
||||
if len(req) != 1 {
|
||||
t.Errorf("req count: got %d, want 1", len(req))
|
||||
}
|
||||
if len(rec) != 0 {
|
||||
t.Errorf("rec count: got %d, want 0", len(rec))
|
||||
}
|
||||
if req[0].Name != "API_KEY" {
|
||||
t.Errorf("req[0].Name: got %q, want %q", req[0].Name, "API_KEY")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCollectOrgEnv_SingleRecommended_Pure(t *testing.T) {
|
||||
tmpl := &OrgTemplate{
|
||||
RecommendedEnv: []EnvRequirement{{Name: "DEBUG_MODE"}},
|
||||
}
|
||||
req, rec := collectOrgEnv(tmpl)
|
||||
if len(req) != 0 {
|
||||
t.Errorf("req count: got %d, want 0", len(req))
|
||||
}
|
||||
if len(rec) != 1 {
|
||||
t.Errorf("rec count: got %d, want 1", len(rec))
|
||||
}
|
||||
}
|
||||
|
||||
func TestCollectOrgEnv_RequiredWinsOverRecommended_Pure(t *testing.T) {
|
||||
// Same env name in both tiers → appears only in required.
|
||||
tmpl := &OrgTemplate{
|
||||
RequiredEnv: []EnvRequirement{{Name: "SHARED_KEY"}},
|
||||
RecommendedEnv: []EnvRequirement{{Name: "SHARED_KEY"}},
|
||||
}
|
||||
req, rec := collectOrgEnv(tmpl)
|
||||
if len(req) != 1 || len(rec) != 0 {
|
||||
t.Errorf("got req=%d rec=%d, want 1,0", len(req), len(rec))
|
||||
}
|
||||
}
|
||||
|
||||
func TestCollectOrgEnv_StrictDropsAnyOf_Pure(t *testing.T) {
|
||||
// Single required + any-of group containing that name → any-of dropped.
|
||||
tmpl := &OrgTemplate{
|
||||
RequiredEnv: []EnvRequirement{{Name: "API_KEY"}},
|
||||
RecommendedEnv: []EnvRequirement{{AnyOf: []string{"API_KEY", "FALLBACK_KEY"}}},
|
||||
}
|
||||
req, rec := collectOrgEnv(tmpl)
|
||||
if len(req) != 1 || req[0].Name != "API_KEY" {
|
||||
t.Errorf("req: got %+v", req)
|
||||
}
|
||||
if len(rec) != 0 {
|
||||
t.Errorf("rec: got %d, want 0 (any-of dropped by strict required)", len(rec))
|
||||
}
|
||||
}
|
||||
|
||||
func TestCollectOrgEnv_AnyOfGroup_Pure(t *testing.T) {
|
||||
tmpl := &OrgTemplate{
|
||||
RequiredEnv: []EnvRequirement{{AnyOf: []string{"KEY_A", "KEY_B"}}},
|
||||
}
|
||||
req, rec := collectOrgEnv(tmpl)
|
||||
if len(req) != 1 {
|
||||
t.Fatalf("req count: got %d, want 1", len(req))
|
||||
}
|
||||
if len(req[0].AnyOf) != 2 {
|
||||
t.Errorf("req[0].AnyOf: got %v, want [KEY_A, KEY_B]", req[0].AnyOf)
|
||||
}
|
||||
_ = rec // may be unused but call is valid
|
||||
}
|
||||
|
||||
func TestCollectOrgEnv_NestedWorkspace_Pure(t *testing.T) {
|
||||
tmpl := &OrgTemplate{
|
||||
RequiredEnv: []EnvRequirement{{Name: "ORG_KEY"}},
|
||||
Workspaces: []OrgWorkspace{
|
||||
{
|
||||
Name: "child-ws",
|
||||
RequiredEnv: []EnvRequirement{{Name: "CHILD_KEY"}},
|
||||
RecommendedEnv: []EnvRequirement{{Name: "CHILD_RECOM"}},
|
||||
},
|
||||
},
|
||||
}
|
||||
req, rec := collectOrgEnv(tmpl)
|
||||
// All 3 required names should appear (dedupped).
|
||||
if len(req) != 2 {
|
||||
t.Errorf("req count: got %d, want 2", len(req))
|
||||
}
|
||||
if len(rec) != 1 {
|
||||
t.Errorf("rec count: got %d, want 1", len(rec))
|
||||
}
|
||||
names := make(map[string]bool)
|
||||
for _, r := range req {
|
||||
names[r.Name] = true
|
||||
}
|
||||
if !names["ORG_KEY"] || !names["CHILD_KEY"] {
|
||||
t.Errorf("req names: got %v", names)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCollectOrgEnv_InvalidEnvNameFiltered_Pure(t *testing.T) {
|
||||
// Invalid names (lowercase, empty) are silently dropped.
|
||||
tmpl := &OrgTemplate{
|
||||
RequiredEnv: []EnvRequirement{
|
||||
{Name: "VALID_KEY"},
|
||||
{Name: "ALSO_VALID"},
|
||||
{Name: "invalid-name"},
|
||||
{Name: ""},
|
||||
},
|
||||
}
|
||||
req, rec := collectOrgEnv(tmpl)
|
||||
if len(req) != 2 {
|
||||
t.Errorf("got %d req, want 2 (lowercase and empty filtered)", len(req))
|
||||
}
|
||||
_ = rec
|
||||
}
|
||||
|
||||
func TestCollectOrgEnv_DedupSameMembers_Pure(t *testing.T) {
|
||||
// Same members declared twice → deduplicated.
|
||||
tmpl := &OrgTemplate{
|
||||
RequiredEnv: []EnvRequirement{
|
||||
{AnyOf: []string{"A", "B"}},
|
||||
{AnyOf: []string{"B", "A"}}, // same set, reversed
|
||||
},
|
||||
}
|
||||
req, rec := collectOrgEnv(tmpl)
|
||||
if len(req) != 1 {
|
||||
t.Errorf("dedup failed: got %d req, want 1", len(req))
|
||||
}
|
||||
_ = rec
|
||||
}
|
||||
|
||||
func TestCollectOrgEnv_RecDedupedByStrictRequired_Pure(t *testing.T) {
|
||||
// Strict required drops any-of groups in recommended tier too.
|
||||
tmpl := &OrgTemplate{
|
||||
RequiredEnv: []EnvRequirement{{Name: "STRICT"}},
|
||||
RecommendedEnv: []EnvRequirement{{AnyOf: []string{"STRICT", "OTHER"}}},
|
||||
}
|
||||
req, rec := collectOrgEnv(tmpl)
|
||||
if len(rec) != 0 {
|
||||
t.Errorf("rec should be empty (strict required prunes recommended any-of): got %d", len(rec))
|
||||
}
|
||||
_ = req
|
||||
}
|
||||
|
||||
func TestCollectOrgEnv_RecursiveChildren_Pure(t *testing.T) {
|
||||
tmpl := &OrgTemplate{
|
||||
RequiredEnv: []EnvRequirement{{Name: "ROOT"}},
|
||||
Workspaces: []OrgWorkspace{
|
||||
{
|
||||
Name: "parent",
|
||||
RequiredEnv: []EnvRequirement{{Name: "PARENT"}, {Name: "PARENT2"}},
|
||||
Children: []OrgWorkspace{
|
||||
{
|
||||
Name: "grandchild",
|
||||
RequiredEnv: []EnvRequirement{{Name: "GRANDCHILD"}},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
req, rec := collectOrgEnv(tmpl)
|
||||
if len(req) != 4 {
|
||||
t.Errorf("got %d required, want 4 (ROOT + PARENT + PARENT2 + GRANDCHILD)", len(req))
|
||||
}
|
||||
_ = rec
|
||||
}
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
// countWorkspaces tests
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
func TestCountWorkspaces_Empty_Pure(t *testing.T) {
|
||||
if n := countWorkspaces(nil); n != 0 {
|
||||
t.Errorf("nil: got %d, want 0", n)
|
||||
}
|
||||
if n := countWorkspaces([]OrgWorkspace{}); n != 0 {
|
||||
t.Errorf("empty: got %d, want 0", n)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCountWorkspaces_Flat_Pure(t *testing.T) {
|
||||
ws := []OrgWorkspace{
|
||||
{Name: "a"}, {Name: "b"},
|
||||
}
|
||||
if n := countWorkspaces(ws); n != 2 {
|
||||
t.Errorf("flat: got %d, want 2", n)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCountWorkspaces_Nested_Pure(t *testing.T) {
|
||||
ws := []OrgWorkspace{
|
||||
{Name: "a", Children: []OrgWorkspace{
|
||||
{Name: "b", Children: []OrgWorkspace{
|
||||
{Name: "c"},
|
||||
}},
|
||||
{Name: "d"},
|
||||
}},
|
||||
}
|
||||
if n := countWorkspaces(ws); n != 4 {
|
||||
t.Errorf("nested: got %d, want 4", n)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user