go/api_example_test.go
Snider 8b905f3a4a feat: per-file example tests — action, registry, fs, api, string, path, service, error, array
33 new examples across 8 dedicated files. Removed phantom CleanPath
(in RFC spec but never implemented — spec drift caught by examples).

545 tests total, 84.8% coverage. Every major primitive has compilable
examples that serve as test, documentation seed, and godoc content.

Co-Authored-By: Virgil <virgil@lethean.io>
2026-03-25 18:29:24 +00:00

50 lines
1.1 KiB
Go

package core_test
import (
"context"
"fmt"
. "dappco.re/go/core"
)
func ExampleAPI_RegisterProtocol() {
c := New()
c.API().RegisterProtocol("http", func(h *DriveHandle) (Stream, error) {
return &mockStream{response: []byte("pong")}, nil
})
fmt.Println(c.API().Protocols())
// Output: [http]
}
func ExampleAPI_Stream() {
c := New()
c.API().RegisterProtocol("http", func(h *DriveHandle) (Stream, error) {
return &mockStream{response: []byte("connected to " + h.Name)}, nil
})
c.Drive().New(NewOptions(
Option{Key: "name", Value: "charon"},
Option{Key: "transport", Value: "http://10.69.69.165:9101"},
))
r := c.API().Stream("charon")
if r.OK {
stream := r.Value.(Stream)
resp, _ := stream.Receive()
fmt.Println(string(resp))
stream.Close()
}
// Output: connected to charon
}
func ExampleCore_RemoteAction() {
c := New()
// Local action
c.Action("status", func(_ context.Context, _ Options) Result {
return Result{Value: "running", OK: true}
})
// No colon — resolves locally
r := c.RemoteAction("status", context.Background(), NewOptions())
fmt.Println(r.Value)
// Output: running
}