Download task_047/tests/validate_enhanced_cases.sh from Lego-X/UXInteract: direct link, hf CLI and curl.
- Browser
- Download file 3.76 kB
-
https://huggingface.co/datasets/Lego-X/UXInteract/resolve/main/task_047/tests/validate_enhanced_cases.sh
- Command line
-
hf download hf://datasets/Lego-X/UXInteract/task_047/tests/validate_enhanced_cases.sh
-
curl -L -o validate_enhanced_cases.sh https://huggingface.co/datasets/Lego-X/UXInteract/resolve/main/task_047/tests/validate_enhanced_cases.sh
3.76 kB
| set -euo pipefail | |
| tmp_test="zz_enhanced_cases_test.go" | |
| trap 'rm -f "$tmp_test"' EXIT | |
| cat > "$tmp_test" <<'GOEOF' | |
| package loganalyzer | |
| import("bytes";"encoding/json";"net/http";"net/http/httptest";"os";"path/filepath";"testing") | |
| // ENHANCED_CASE_1: POST persists a generated tag and GET returns the same complete user log. | |
| func TestEnhancedCase1PostGetRoundTrip(t *testing.T){h:=NewHandler(NewMemoryRepository());payload:=Log{ID:"round-trip-7",Message:"payment failed",Level:"ERROR",Timestamp:"2025-09-05T12:00:00Z"};b,_:=json.Marshal(payload);p:=httptest.NewRecorder();h.ServeHTTP(p,httptest.NewRequest(http.MethodPost,"/logs",bytes.NewReader(b)));if p.Code<200||p.Code>=300{t.Fatalf("POST=%d %s",p.Code,p.Body.String())};g:=httptest.NewRecorder();h.ServeHTTP(g,httptest.NewRequest(http.MethodGet,"/log/round-trip-7",nil));var got Log;if g.Code!=200||json.Unmarshal(g.Body.Bytes(),&got)!=nil{t.Fatalf("GET=%d %s",g.Code,g.Body.String())};if got.ID!=payload.ID||got.Message!=payload.Message||got.Level!=payload.Level||got.Timestamp!=payload.Timestamp||got.Tag==""{t.Fatalf("round trip changed log: %+v",got)}} | |
| // ENHANCED_CASE_2: malformed input is isolated and does not prevent the next valid log request. | |
| func TestEnhancedCase2BadLogDoesNotBreakServer(t *testing.T){h:=NewHandler(NewMemoryRepository());bad:=httptest.NewRecorder();h.ServeHTTP(bad,httptest.NewRequest(http.MethodPost,"/logs",bytes.NewBufferString("{not-json")));if bad.Code<400{t.Fatalf("malformed JSON unexpectedly accepted: %d",bad.Code)};good:=httptest.NewRecorder();h.ServeHTTP(good,httptest.NewRequest(http.MethodPost,"/logs",bytes.NewBufferString(`{"id":"after-bad","message":"ok","level":"INFO","timestamp":"2025-09-05T12:00:00Z"}`)));if good.Code<200||good.Code>=300{t.Fatalf("valid request after bad one failed: %d %s",good.Code,good.Body.String())}} | |
| // ENHANCED_CASE_3: configured file storage writes decodable JSON and remains usable through its repository interface. | |
| func TestEnhancedCase3ConfiguredFileRepository(t *testing.T){p:=filepath.Join(t.TempDir(),"logs.jsonl");r:=NewRepositoryFromConfig("file",p);if r==nil{t.Fatal("file config returned nil")};want:=Log{ID:"file-config-1",Message:"persist me",Level:"WARN",Timestamp:"2025-09-05T12:00:00Z",Tag:"warn"};if err:=r.Save(want);err!=nil{t.Fatal(err)};reopened:=NewRepositoryFromConfig("file",p);got,ok:=reopened.Get(want.ID);if !ok||got!=want{t.Fatalf("file repository did not survive restart: ok=%v got=%+v",ok,got)};raw,err:=os.ReadFile(p);if err!=nil||len(bytes.TrimSpace(raw))==0{t.Fatalf("storage file missing/empty: %v",err)}} | |
| // ENHANCED_CASE_4: invalid fields yield 400 and do not become queryable; file failures do not create ghost records. | |
| func TestEnhancedCase4ValidationAndFileFailure(t *testing.T){ | |
| repo:=NewMemoryRepository();h:=NewHandler(repo) | |
| for _,body:=range []string{`{"id":"","message":"m","level":"INFO","timestamp":"2025-09-05T12:00:00Z"}`,`{"id":"bad-time","message":"m","level":"INFO","timestamp":"yesterday"}`} { | |
| rec:=httptest.NewRecorder();h.ServeHTTP(rec,httptest.NewRequest(http.MethodPost,"/logs",bytes.NewBufferString(body))) | |
| if rec.Code!=http.StatusBadRequest {t.Fatalf("invalid log status=%d",rec.Code)} | |
| } | |
| if _,ok:=repo.Get("bad-time");ok {t.Fatal("invalid log was stored")} | |
| badPath:=t.TempDir() // Opening a directory as a writable file must fail; creating missing parent dirs is valid. | |
| fileRepo:=NewFileRepository(badPath) | |
| if err:=fileRepo.Save(Log{ID:"ghost",Message:"m",Level:"ERROR",Timestamp:"2025-09-05T12:00:00Z"});err==nil{t.Fatal("file write unexpectedly succeeded")} | |
| if _,ok:=fileRepo.Get("ghost");ok{t.Fatal("failed file write left a ghost in memory")} | |
| } | |
| GOEOF | |
| GOTOOLCHAIN=local GOPROXY=off go test -v -count=1 -timeout=20s -run '^TestEnhancedCase' . | |