69 lines
1.6 KiB
Go
69 lines
1.6 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/metazla/meta-core/internal/claim"
|
|
"github.com/metazla/meta-core/internal/identity"
|
|
"github.com/metazla/meta-core/internal/storage"
|
|
)
|
|
|
|
// newTestStorage opens (and starts) a fresh store in the test's temp dir.
|
|
func newTestStorage(t *testing.T) *storage.Client {
|
|
t.Helper()
|
|
dir := t.TempDir()
|
|
c := storage.NewClient()
|
|
if err := c.Connect(filepath.Join(dir, "meta-core.db"), filepath.Join(dir, "cache.db")); err != nil {
|
|
t.Fatalf("open store: %v", err)
|
|
}
|
|
if err := c.EnsureSchemaVersion(storage.SchemaVersion); err != nil {
|
|
t.Fatalf("schema: %v", err)
|
|
}
|
|
if err := c.Start(); err != nil {
|
|
t.Fatalf("start: %v", err)
|
|
}
|
|
t.Cleanup(func() { _ = c.Close() })
|
|
return c
|
|
}
|
|
|
|
func testSigner(t *testing.T, n byte) *identity.Identity {
|
|
t.Helper()
|
|
id, err := identity.Import(fmt.Sprintf("%02x%062x", n, 1))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return id
|
|
}
|
|
|
|
// signed builds a claim signed by id; v is string, []byte or nil.
|
|
func signed(t *testing.T, id *identity.Identity, cid, key string, v any, ts uint64) claim.Claim {
|
|
t.Helper()
|
|
cl := claim.Claim{CID: cid, UID: id.UID, Key: key, TS: ts}
|
|
switch x := v.(type) {
|
|
case string:
|
|
cl.Value = claim.Text(x)
|
|
case []byte:
|
|
cl.Value = claim.Bytes(x)
|
|
}
|
|
if err := cl.Sign(id); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return cl
|
|
}
|
|
|
|
func storeClaims(t *testing.T, c *storage.Client, claims ...claim.Claim) {
|
|
t.Helper()
|
|
res, err := c.WriteClaims(context.Background(), claims)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for i, r := range res {
|
|
if r.Status != storage.StatusAccepted {
|
|
t.Fatalf("claim %d: %s %s", i, r.Status, r.Reason)
|
|
}
|
|
}
|
|
}
|