This commit is contained in:
2026-01-27 16:20:46 -05:00
parent 7bf4f115b1
commit c3a16c9e92
21 changed files with 1192 additions and 577 deletions

122
internal/standard/fetch.go Normal file
View File

@@ -0,0 +1,122 @@
package standard
import (
"context"
"fmt"
"io"
"maps"
"net/http"
"strings"
"reichard.io/poiesis/internal/builtin"
)
type FetchArgs struct {
Input string `json:"input"`
Init *RequestInit `json:"init,omitempty"`
}
func (f FetchArgs) Validate() error {
return nil
}
type RequestInit struct {
Method string `json:"method,omitempty"`
Headers map[string]string `json:"headers,omitempty"`
Body *string `json:"body,omitempty"`
}
func (o *RequestInit) Validate() error {
if o.Method == "" {
o.Method = "GET"
}
return nil
}
type Response struct {
OK bool `json:"ok"`
Status int `json:"status"`
Body string `json:"body"`
Headers map[string]string `json:"headers"`
}
type AddArgs struct {
A int `json:"a"`
B int `json:"b"`
}
func (a AddArgs) Validate() error {
return nil
}
type GreetArgs struct {
Name string `json:"name"`
}
func (g GreetArgs) Validate() error {
return nil
}
func Fetch(_ context.Context, args FetchArgs) (Response, error) {
method := "GET"
headers := make(map[string]string)
if args.Init != nil {
method = args.Init.Method
if args.Init.Headers != nil {
maps.Copy(headers, args.Init.Headers)
}
}
req, err := http.NewRequest(method, args.Input, nil)
if err != nil {
return Response{}, fmt.Errorf("failed to create request: %w", err)
}
for k, v := range headers {
req.Header.Set(k, v)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return Response{}, fmt.Errorf("failed to fetch: %w", err)
}
defer func() {
_ = resp.Body.Close()
}()
body, err := io.ReadAll(resp.Body)
if err != nil {
return Response{}, fmt.Errorf("failed to read body: %w", err)
}
resultHeaders := make(map[string]string)
for key, values := range resp.Header {
if len(values) > 0 {
val := values[0]
resultHeaders[key] = val
resultHeaders[strings.ToLower(key)] = val
}
}
return Response{
OK: resp.StatusCode >= 200 && resp.StatusCode < 300,
Status: resp.StatusCode,
Body: string(body),
Headers: resultHeaders,
}, nil
}
func Add(_ context.Context, args AddArgs) (int, error) {
return args.A + args.B, nil
}
func Greet(_ context.Context, args GreetArgs) (string, error) {
return fmt.Sprintf("Hello, %s!", args.Name), nil
}
func init() {
builtin.RegisterAsyncBuiltin("fetch", Fetch)
builtin.RegisterBuiltin("add", Add)
builtin.RegisterBuiltin("greet", Greet)
}

View File

@@ -0,0 +1,51 @@
package standard
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/dop251/goja"
"reichard.io/poiesis/internal/builtin"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestFetchReturnsPromise(t *testing.T) {
vm := goja.New()
builtin.RegisterBuiltins(vm)
result, err := vm.RunString(`fetch({input: "https://example.com"})`)
require.NoError(t, err)
promise, ok := result.Export().(*goja.Promise)
require.True(t, ok, "fetch should return a Promise")
assert.NotNil(t, promise)
}
func TestFetchAsyncAwait(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(`{"status":"ok"}`))
}))
defer server.Close()
vm := goja.New()
builtin.RegisterBuiltins(vm)
result, err := vm.RunString(`
async function testFetch() {
const response = await fetch({input: "` + server.URL + `"});
return response.ok;
}
testFetch();
`)
require.NoError(t, err)
promise, ok := result.Export().(*goja.Promise)
require.True(t, ok, "async function should return a Promise")
assert.NotNil(t, promise)
}

View File

@@ -0,0 +1,120 @@
package standard
import (
"context"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestFetch(t *testing.T) {
ctx := context.Background()
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Header().Set("X-Custom-Header", "test-value")
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(`{"status":"ok","message":"Hello from httptest"}`))
}))
defer server.Close()
result, err := Fetch(ctx, FetchArgs{Input: server.URL})
require.NoError(t, err)
assert.True(t, result.OK)
assert.Equal(t, http.StatusOK, result.Status)
assert.Contains(t, result.Body, "Hello from httptest")
assert.Contains(t, result.Body, `"status":"ok"`)
assert.Equal(t, "application/json", result.Headers["Content-Type"])
assert.Equal(t, "test-value", result.Headers["X-Custom-Header"])
}
func TestFetchHTTPBin(t *testing.T) {
t.Skip("httpbin.org test is flaky")
ctx := context.Background()
result, err := Fetch(ctx, FetchArgs{Input: "https://httpbin.org/get"})
require.NoError(t, err)
assert.True(t, result.OK)
assert.Equal(t, http.StatusOK, result.Status)
assert.Contains(t, result.Body, `"args"`)
assert.Equal(t, "application/json", result.Headers["Content-Type"])
}
func TestFetchWith404(t *testing.T) {
ctx := context.Background()
result, err := Fetch(ctx, FetchArgs{Input: "https://httpbin.org/status/404"})
require.NoError(t, err)
assert.False(t, result.OK)
assert.Equal(t, http.StatusNotFound, result.Status)
}
func TestFetchWithInvalidURL(t *testing.T) {
ctx := context.Background()
_, err := Fetch(ctx, FetchArgs{Input: "http://this-domain-does-not-exist-12345.com"})
assert.Error(t, err)
assert.Contains(t, err.Error(), "failed to fetch")
}
func TestFetchWithHeaders(t *testing.T) {
ctx := context.Background()
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "Bearer test-token", r.Header.Get("Authorization"))
assert.Equal(t, "GET", r.Method)
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(`ok`))
}))
defer server.Close()
headers := map[string]string{
"Authorization": "Bearer test-token",
}
options := &RequestInit{
Method: "GET",
Headers: headers,
}
result, err := Fetch(ctx, FetchArgs{Input: server.URL, Init: options})
require.NoError(t, err)
assert.True(t, result.OK)
}
func TestFetchDefaults(t *testing.T) {
ctx := context.Background()
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "GET", r.Method, "default method should be GET")
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(`ok`))
}))
defer server.Close()
options := &RequestInit{}
result, err := Fetch(ctx, FetchArgs{Input: server.URL, Init: options})
require.NoError(t, err)
assert.True(t, result.OK)
}
func TestAdd(t *testing.T) {
ctx := context.Background()
result, err := Add(ctx, AddArgs{A: 5, B: 10})
require.NoError(t, err)
assert.Equal(t, 15, result)
result, err = Add(ctx, AddArgs{A: -3, B: 7})
require.NoError(t, err)
assert.Equal(t, 4, result)
}
func TestGreet(t *testing.T) {
ctx := context.Background()
result, err := Greet(ctx, GreetArgs{Name: "World"})
require.NoError(t, err)
assert.Equal(t, "Hello, World!", result)
result, err = Greet(ctx, GreetArgs{Name: "Alice"})
require.NoError(t, err)
assert.Equal(t, "Hello, Alice!", result)
}