|
| 1 | +package intentproof_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/intentproof/intentproof-sdk-go/intentproof" |
| 9 | +) |
| 10 | + |
| 11 | +func TestDefaultDataDir(t *testing.T) { |
| 12 | + dir := intentproof.DefaultDataDir() |
| 13 | + if dir == "" { |
| 14 | + t.Fatal("expected non-empty default data dir") |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +func TestGettersBeforeConfigure(t *testing.T) { |
| 19 | + // Reset state by configuring then we need isolated package - instead test error paths |
| 20 | + // by using a fresh module is hard; test GetTenantID without configure returns default. |
| 21 | + if intentproof.GetTenantID() == "" { |
| 22 | + t.Fatal("expected default tenant") |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +func TestConfigureUsesDefaultDataDirWhenOmitted(t *testing.T) { |
| 27 | + dir := t.TempDir() |
| 28 | + os.Unsetenv("INTENTPROOF_OUTBOX_PATH") |
| 29 | + if err := intentproof.Configure(intentproof.ConfigureOptions{ |
| 30 | + DBPath: filepath.Join(dir, "outbox.db"), |
| 31 | + TenantID: "tnt_default_dir", |
| 32 | + }); err != nil { |
| 33 | + t.Fatal(err) |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +func TestConfigureWithEnvTenantAndOutboxPath(t *testing.T) { |
| 38 | + dir := t.TempDir() |
| 39 | + t.Setenv("INTENTPROOF_TENANT_ID", "tnt_from_env") |
| 40 | + t.Setenv("INTENTPROOF_OUTBOX_PATH", filepath.Join(dir, "custom.db")) |
| 41 | + if err := intentproof.Configure(intentproof.ConfigureOptions{ |
| 42 | + DataDir: filepath.Join(dir, "data"), |
| 43 | + }); err != nil { |
| 44 | + t.Fatal(err) |
| 45 | + } |
| 46 | + if intentproof.GetTenantID() != "tnt_from_env" { |
| 47 | + t.Fatalf("tenant: %s", intentproof.GetTenantID()) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +func TestConfigureInvalidKeypairFails(t *testing.T) { |
| 52 | + dir := t.TempDir() |
| 53 | + dataDir := filepath.Join(dir, "data") |
| 54 | + if err := os.MkdirAll(dataDir, 0o700); err != nil { |
| 55 | + t.Fatal(err) |
| 56 | + } |
| 57 | + if err := os.WriteFile(filepath.Join(dataDir, "keypair.json"), []byte("not-json"), 0o600); err != nil { |
| 58 | + t.Fatal(err) |
| 59 | + } |
| 60 | + dbPath, _ := testDirs(t) |
| 61 | + err := intentproof.Configure(intentproof.ConfigureOptions{ |
| 62 | + DBPath: dbPath, |
| 63 | + DataDir: dataDir, |
| 64 | + TenantID: "tnt_bad", |
| 65 | + }) |
| 66 | + if err == nil { |
| 67 | + t.Fatal("expected configure error") |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func TestFlushWithoutExporter(t *testing.T) { |
| 72 | + dbPath, dataDir := testDirs(t) |
| 73 | + configureTest(t, dbPath, dataDir, "tnt_a") |
| 74 | + intentproof.Flush() // no exporter configured |
| 75 | +} |
| 76 | + |
| 77 | +func TestPushSubjectMappingNoOp(t *testing.T) { |
| 78 | + intentproof.PushSubjectMapping("src", "type", "id") |
| 79 | +} |
| 80 | + |
| 81 | +func TestWrapGeneratesCorrelationIDWhenUnset(t *testing.T) { |
| 82 | + dbPath, dataDir := testDirs(t) |
| 83 | + configureTest(t, dbPath, dataDir, "tnt_auto_corr") |
| 84 | + fn := intentproof.Wrap("Test", "test.action", func(x int) int { return x }) |
| 85 | + fn(1) |
| 86 | + ob, _ := intentproof.GetOutbox() |
| 87 | + events, _ := ob.Events() |
| 88 | + if len(events) != 1 { |
| 89 | + t.Fatalf("events: %d", len(events)) |
| 90 | + } |
| 91 | + cid, _ := events[0]["correlation_id"].(string) |
| 92 | + if cid == "" || cid == "corr-outer" { |
| 93 | + // should be req_* ULID, not empty or from other tests' fixed ids |
| 94 | + if len(cid) < 4 || cid[:4] != "req_" { |
| 95 | + t.Fatalf("correlation_id: %q", cid) |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments