#!/usr/bin/env python3 """ Task: go_htmx — a Go web app with HTMX partials + multi-table SQLite. Models a real-world Go web tool in miniature: single Go binary, server-rendered html/template + HTMX for partial updates, embedded SQLite (modernc.org/sqlite), a multi-table schema with a self-referential location tree (recursive CTE read), embedded assets via //go:embed. The "real product" = a binary that serves a page listing locations in a tree, lets you add a sub-location via an HTMX partial swap (no full page reload), and persists to SQLite. """ import os, sys, subprocess, time, shutil, signal, socket, urllib.request, urllib.parse, json HERE = os.path.dirname(os.path.abspath(__file__)) from bench.core import tools TASK_NAME = "go_htmx" PLAN = """\ # Task — Build a Go web app with HTMX partials + multi-table SQLite ## Goal `go build ./...` and `go test ./...` run green, and the binary starts on port 8080, serves a locations tree page at `/`, lets you add a sub-location via an HTMX partial swap (POST returns just the updated tree HTML fragment, not a full page), and persists to embedded SQLite. This is a Go web app with HTMX partials + multi-table SQLite: single Go binary, server-rendered html/template + HTMX, embedded SQLite, multi-table schema with a self-referential location tree read via a recursive CTE. ## Prerequisites Starter scaffold in the work dir: - `go.mod` (module `locations`, go 1.26, no deps) - `cmd/server/main.go` (empty stub: `package main; func main(){}`) ## Spec — the app to build A single Go binary serving a one-page location-tree manager over HTTP. Uses HTMX for partial updates (adding a sub-location swaps just the tree fragment, no full page reload). Embedded SQLite via `modernc.org/sqlite`. Embedded templates + migrations via `//go:embed`. ### Schema (two tables) locations( id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, parent_id INTEGER NULL REFERENCES locations(id) ON DELETE CASCADE, created_at DATETIME DEFAULT CURRENT_TIMESTAMP ) -- UNIQUE(name, parent_id) — names unique within a parent (NULL parent = root) CREATE UNIQUE INDEX locations_name_parent ON locations(name, IFNULL(parent_id, 0)); ### Files to produce (layout) cmd/server/main.go boot: config -> db -> migrate -> handlers -> ListenAndServe + graceful shutdown internal/config/config.go Config{Port, DBPath}; LoadFromEnv() reads NOTES_PORT (port, int) and NOTES_DB (db path) — MUST use these exact env var names; the scoring harness sets them. internal/db/db.go Open(dsn): modernc sqlite, PRAGMA WAL+busy_timeout+FK on; Migrate() via embedded .sql internal/db/migrations/00001_init.sql the schema above (both tables + the unique index) internal/models/location.go Location struct + Create(db, name, parentID) + Tree(db) — Tree returns a flat list with depth via a recursive CTE: WITH RECURSIVE tree AS (SELECT id, name, parent_id, 0 AS depth FROM locations WHERE parent_id IS NULL UNION ALL SELECT l.id, l.name, l.parent_id, t.depth+1 FROM locations l JOIN tree t ON l.parent_id = t.id) SELECT id, name, parent_id, depth FROM tree ORDER BY depth, name internal/handlers/handlers.go Handlers struct; routes via stdlib net/http ServeMux: GET / (full page: form + tree), POST / (create location: form fields name + parent_id; returns JUST the updated tree fragment as an HTML partial for HTMX swap, Content-Type text/html, status 200 — NOT a redirect, NOT a full page) internal/handlers/handlers_test.go httptest: POST a root location, assert the response body contains the location name indented by depth (one leading space pair per depth level); POST a child, assert it appears nested under the parent web/templates/page.html the full page: pulls in HTMX (a is fine), a form (name input + hidden parent_id input + submit) with hx-post="/" hx-target="#tree" hx-swap="innerHTML", and a
containing the rendered tree web/templates/tree.html the tree fragment: renders the locations as a nested