package main

import "testing"

func TestExtractExecutionIDs(t *testing.T) {
	// Real tool-result phrasings that should yield an execution id.
	pos := map[string]string{
		`"pct_create on host:strong auto-approved via assent window — execution 019f4b19-eafd-74ed-baa6-d24a27b3f52c running."`: "019f4b19-eafd-74ed-baa6-d24a27b3f52c",
		`"run on lxc:caddy requires approval (risk: config_mutation) — execution 019f4af7-7eff-7723-b38c-b540b267f407 queued."`:   "019f4af7-7eff-7723-b38c-b540b267f407",
		`"apt_upgrade on host:hubris auto-approved via assent window — execution 019f4b58-c88c-7767-87dd-044608ced913 running."`:  "019f4b58-c88c-7767-87dd-044608ced913",
	}
	for in, want := range pos {
		ids := extractExecutionIDs(in)
		if len(ids) != 1 || ids[0].String() != want {
			t.Errorf("extractExecutionIDs(%q) = %v, want [%s]", in, ids, want)
		}
	}

	// Synchronous auto-run and read-only results carry no "execution <uuid>"
	// phrasing — they've already completed inline and must NOT be linked for
	// continuation.
	neg := []string{
		`"run on host:strong (read_only, auto): 09:30 up 8 days"`,
		`"run on lxc:caddy (config_mutation, auto via assent window): done"`,
		`[{"slug":"lxc:caddy","health":"healthy"}]`,
		`"target not found: lxc:nope"`,
	}
	for _, in := range neg {
		if ids := extractExecutionIDs(in); len(ids) != 0 {
			t.Errorf("extractExecutionIDs(%q) = %v, want none", in, ids)
		}
	}

	// De-dupes repeated ids in one result.
	dup := `execution 019f4b19-eafd-74ed-baa6-d24a27b3f52c queued ... execution 019f4b19-eafd-74ed-baa6-d24a27b3f52c running`
	if ids := extractExecutionIDs(dup); len(ids) != 1 {
		t.Errorf("expected de-dup to 1 id, got %v", ids)
	}
}
