|  | 
|  | 1 | +package app_test | 
|  | 2 | + | 
|  | 3 | +import ( | 
|  | 4 | +	"net/http/httptest" | 
|  | 5 | +	"path" | 
|  | 6 | +	"testing" | 
|  | 7 | + | 
|  | 8 | +	"github.com/claceio/clace/internal/app" | 
|  | 9 | +	"github.com/claceio/clace/internal/testutil" | 
|  | 10 | +) | 
|  | 11 | + | 
|  | 12 | +func actionTester(t *testing.T, rootPath bool, actionPath string) { | 
|  | 13 | +	t.Helper() | 
|  | 14 | +	logger := testutil.TestLogger() | 
|  | 15 | +	fileData := map[string]string{ | 
|  | 16 | +		"app.star": ` | 
|  | 17 | +def handler(dry_run, args): | 
|  | 18 | +	return ace.result(status="done", values=["a", "b"]) | 
|  | 19 | +
 | 
|  | 20 | +app = ace.app("testApp", | 
|  | 21 | +	actions=[ace.action("testAction", "` + actionPath + `", handler)]) | 
|  | 22 | +
 | 
|  | 23 | +		`, | 
|  | 24 | +		"params.star": `param("param1", description="param1 description", type=STRING, default="myvalue")`, | 
|  | 25 | +	} | 
|  | 26 | +	var a *app.App | 
|  | 27 | +	var err error | 
|  | 28 | +	if rootPath { | 
|  | 29 | +		a, _, err = CreateTestAppRoot(logger, fileData) | 
|  | 30 | +	} else { | 
|  | 31 | +		a, _, err = CreateTestApp(logger, fileData) | 
|  | 32 | +	} | 
|  | 33 | +	if err != nil { | 
|  | 34 | +		t.Fatalf("Error %s", err) | 
|  | 35 | +	} | 
|  | 36 | + | 
|  | 37 | +	var reqPath string | 
|  | 38 | +	if rootPath { | 
|  | 39 | +		reqPath = actionPath | 
|  | 40 | +	} else { | 
|  | 41 | +		reqPath = path.Join("/test", actionPath) | 
|  | 42 | +	} | 
|  | 43 | + | 
|  | 44 | +	request := httptest.NewRequest("GET", reqPath, nil) | 
|  | 45 | +	response := httptest.NewRecorder() | 
|  | 46 | +	a.ServeHTTP(response, request) | 
|  | 47 | + | 
|  | 48 | +	testutil.AssertEqualsInt(t, "code", 200, response.Code) | 
|  | 49 | +	testutil.AssertStringContains(t, response.Body.String(), "<title>testAction</title>") | 
|  | 50 | +	testutil.AssertStringContains(t, response.Body.String(), `id="param_param1"`) | 
|  | 51 | + | 
|  | 52 | +	request = httptest.NewRequest("POST", reqPath, nil) | 
|  | 53 | +	response = httptest.NewRecorder() | 
|  | 54 | +	a.ServeHTTP(response, request) | 
|  | 55 | +	testutil.AssertEqualsInt(t, "code", 200, response.Code) | 
|  | 56 | +	testutil.AssertStringMatch(t, "match response", response.Body.String(), ` | 
|  | 57 | +          <div class="text-lg text-bold"> | 
|  | 58 | +            done | 
|  | 59 | +          </div> | 
|  | 60 | +         | 
|  | 61 | +          <div id="action_result" hx-swap-oob="true" hx-swap="outerHTML"> | 
|  | 62 | +            <div class="divider text-lg text-secondary">Output</div> | 
|  | 63 | +            <textarea | 
|  | 64 | +              rows="20" | 
|  | 65 | +              class="textarea textarea-success w-full font-mono" | 
|  | 66 | +              readonly>a | 
|  | 67 | +        b | 
|  | 68 | +        </textarea | 
|  | 69 | +            > | 
|  | 70 | +          </div> | 
|  | 71 | +        `) | 
|  | 72 | + | 
|  | 73 | +} | 
|  | 74 | + | 
|  | 75 | +func TestRootAppRootAction(t *testing.T) { | 
|  | 76 | +	actionTester(t, true, "/") | 
|  | 77 | +} | 
|  | 78 | +func TestRootApp(t *testing.T) { | 
|  | 79 | +	actionTester(t, true, "/abc") | 
|  | 80 | +} | 
|  | 81 | + | 
|  | 82 | +func TestNonRootAppRootAction(t *testing.T) { | 
|  | 83 | +	actionTester(t, false, "/") | 
|  | 84 | +} | 
|  | 85 | +func TestNonRootApp(t *testing.T) { | 
|  | 86 | +	actionTester(t, false, "/abc") | 
|  | 87 | +} | 
0 commit comments