clone

a self-hosted multitenant nostr relay with sites, file storage, git hosting, and inbox and outbox delivery

feat/tinyagent
tinyrelay / protocol / contracts_test.go
contracts_test.go 1.8K
1package protocol_test23import (4 "os"5 "os/exec"6 "path/filepath"7 "runtime"8 "strconv"9 "strings"10 "testing"11)1213const modulePath = "github.com/FelineStateMachine/tinyrelay"1415// Run a real consumer outside tinyrelay's module so Go's internal import16// restriction, public type identity and standalone dependencies are checked.17func TestExternalNostrConsumer(t *testing.T) {18 runConsumer(t, "nostr")19}2021func TestExternalAuthConsumer(t *testing.T) {22 runConsumer(t, "auth")23}2425func runConsumer(t *testing.T, fixture string) {26 t.Helper()27 root, err := filepath.Abs("..")28 if err != nil {29 t.Fatal(err)30 }31 mod, err := os.ReadFile(filepath.Join(root, "go.mod"))32 if err != nil {33 t.Fatal(err)34 }35 var goVersion string36 for _, line := range strings.Split(string(mod), "\n") {37 if strings.HasPrefix(line, "go ") {38 goVersion = strings.TrimPrefix(line, "go ")39 }40 }41 if goVersion == "" {42 t.Fatal("module has no Go version")43 }44 dir := t.TempDir()45 write := func(name string, data []byte) {46 t.Helper()47 if err := os.WriteFile(filepath.Join(dir, name), data, 0600); err != nil {48 t.Fatal(err)49 }50 }51 write("go.mod", []byte("module example.com/protocol-consumer\n\ngo "+goVersion+"\n\nrequire "+modulePath+" v0.0.0\nreplace "+modulePath+" => "+strconv.Quote(filepath.ToSlash(root))+"\n"))52 for _, file := range []struct{ src, dst string }{53 {filepath.Join(root, "go.sum"), "go.sum"},54 {filepath.Join("testdata", fixture, "consumer_test.go"), "consumer_test.go"},55 } {56 data, err := os.ReadFile(file.src)57 if err != nil {58 t.Fatal(err)59 }60 write(file.dst, data)61 }62 cmd := exec.Command(filepath.Join(runtime.GOROOT(), "bin", "go"), "test", "-mod=mod", "-count=1", "./...")63 cmd.Dir = dir64 cmd.Env = append(os.Environ(), "GOWORK=off")65 if out, err := cmd.CombinedOutput(); err != nil {66 t.Fatalf("external %s consumer: %v\n%s", fixture, err, out)67 }68}