package api import ( "errors" "net/http" "os" "strings" "testing" "gitea.dooplex.hu/admin/felhom-controller/internal/stacks" "gitea.dooplex.hu/admin/felhom-controller/internal/util" ) // R-553 — the deploy endpoint's status code must survive the day its Hungarian words are translated. // // Until v0.251.0 the handler read the refusal's TEXT: „kötelező", „memória", "does not exist", // "already deployed". Localisation slice 2 translates exactly those words, and every empty required // field would then answer 500 instead of 400 — a server error for something the customer can fix. // // The table below therefore passes messages that are ALREADY translated: the words are gone, only the // kind remains. RED-PROOF (REPORT): restore the strings.Contains chain in deployStack and this fails // on every translated row. func TestR553_Deploy_DecisionSurvivesWordingChange(t *testing.T) { cases := []struct { name string err error want int }{ {"required field, Hungarian as shipped", util.KindErrorf(stacks.ErrRequiredField, "a(z) %q (%s) mező kitöltése kötelező", "Jelszó", "PASSWORD"), http.StatusBadRequest}, {"required field, TRANSLATED", util.KindErrorf(stacks.ErrRequiredField, "the %q (%s) field is required", "Password", "PASSWORD"), http.StatusBadRequest}, {"password field, TRANSLATED", util.KindErrorf(stacks.ErrRequiredField, "fill in the %q field — use the Generate button", "Password"), http.StatusBadRequest}, {"path missing, TRANSLATED", util.KindErrorf(stacks.ErrPathMissing, "a mappa %q nem létezik (%q mező)", "/mnt/nope", "Adatok"), http.StatusBadRequest}, {"not enough memory, Hungarian as shipped", util.KindError(stacks.ErrNotEnoughMemory, "Nincs elég memória az alkalmazás telepítéséhez. Szükséges: 512 MB, Elérhető: 100 MB"), http.StatusBadRequest}, {"not enough memory, TRANSLATED", util.KindError(stacks.ErrNotEnoughMemory, "Not enough memory to install this app. Needed: 512 MB, available: 100 MB"), http.StatusBadRequest}, {"already deployed, TRANSLATED", util.KindErrorf(stacks.ErrAlreadyDeployed, "a(z) %q alkalmazás már telepítve van", "privatebin"), http.StatusConflict}, {"anything else is a server error", errors.New("compose exploded"), http.StatusInternalServerError}, {"a message that merely CONTAINS the old words is not a refusal kind", errors.New("docker: kötelező memória does not exist"), http.StatusInternalServerError}, } for _, c := range cases { if got := deployStatusFor(c.err); got != c.want { t.Errorf("%s: deployStatusFor(%q) = %d, want %d", c.name, c.err.Error(), got, c.want) } } } // The seam has to be WIRED, not merely present: the handler must call deployStatusFor, and no // decision in the deploy handler may read the error's text again (seam-built-but-never-wired, four // instances in this repo). func TestR553_DeployHandlerUsesTheKind(t *testing.T) { src, err := os.ReadFile("router.go") if err != nil { t.Fatal(err) } body := string(src) i := strings.Index(body, "func (r *Router) deployStack(") if i < 0 { t.Fatal("deployStack handler not found — this test no longer reads what it thinks it reads") } handler := body[i:] if j := strings.Index(handler[10:], "\nfunc "); j > 0 { handler = handler[:j+10] } if !strings.Contains(handler, "deployStatusFor(err)") { t.Error("the deploy handler no longer asks deployStatusFor for the status code") } // Comments may still NAME those words (this one does); a DECISION may not read them. for _, line := range strings.Split(handler, "\n") { if strings.HasPrefix(strings.TrimSpace(line), "//") { continue } if strings.Contains(line, "strings.Contains(err.Error()") { t.Errorf("the deploy handler decides by reading the error text again (R-553): %s", strings.TrimSpace(line)) } } }