| 1 | package docs |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "net/http/httptest" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "testing" |
| 10 | "time" |
| 11 | |
| 12 | access "amadan.net/oficina/agent-access.git" |
| 13 | "amadan.net/oficina/home/connect" |
| 14 | "gorm.io/gorm" |
| 15 | ) |
| 16 | |
| 17 | // Opt-in subprocess endpoint for the suite's real-store composition test. |
| 18 | // There is no production configuration or runtime switch for this fixture. |
| 19 | func TestSuiteInteropFixture(t *testing.T) { |
| 20 | directory := os.Getenv("OFICINA_INTEROP_DIR") |
| 21 | if directory == "" { |
| 22 | t.Skip("suite interoperability subprocess only") |
| 23 | } |
| 24 | f := newDocAgentFixture(t) |
| 25 | id, e := f.a.resolver.resolveHome(t.Context(), f.p.Subject) |
| 26 | if e != nil { |
| 27 | t.Fatal(e) |
| 28 | } |
| 29 | bridge, ok := f.a.resolver.links.(*connect.Bridge) |
| 30 | if !ok { |
| 31 | t.Fatal("real Home bridge required") |
| 32 | } |
| 33 | tx, e := f.app.db.Writer().BeginTx(t.Context(), nil) |
| 34 | if e != nil { |
| 35 | t.Fatal(e) |
| 36 | } |
| 37 | defer tx.Rollback() |
| 38 | if _, e = tx.Exec("DELETE FROM home_identity_links"); e != nil { |
| 39 | t.Fatal(e) |
| 40 | } |
| 41 | if e = bridge.Adopt(t.Context(), tx, bridgeIssuer, "opaque-home-alice", id.Actor.PersonID); e != nil { |
| 42 | t.Fatal(e) |
| 43 | } |
| 44 | // No resources/receipts exist yet; establish the fixture's chosen team. |
| 45 | if _, e = tx.Exec("DELETE FROM docs_agent_state"); e != nil { |
| 46 | t.Fatal(e) |
| 47 | } |
| 48 | if e = tx.Commit(); e != nil { |
| 49 | t.Fatal(e) |
| 50 | } |
| 51 | f.a.team = "acme" |
| 52 | f.p = access.Principal{Subject: "opaque-home-alice", Client: "oficina-interop", Grant: "interop-grant", Teams: []string{"acme"}, Scopes: []string{"docs:read", "docs:write"}} |
| 53 | if e = f.a.store.db.Transaction(func(tx *gorm.DB) error { return f.a.bind(tx) }); e != nil { |
| 54 | t.Fatal(e) |
| 55 | } |
| 56 | handler, e := f.a.httpHandler(access.VerifyFunc(func(context.Context, string) (access.Principal, error) { return f.p, nil })) |
| 57 | if e != nil { |
| 58 | t.Fatal(e) |
| 59 | } |
| 60 | server := httptest.NewTLSServer(handler) |
| 61 | defer server.Close() |
| 62 | metadata, e := json.Marshal(map[string]string{"url": server.URL, "origin": f.a.origin}) |
| 63 | if e != nil { |
| 64 | t.Fatal(e) |
| 65 | } |
| 66 | temporary := filepath.Join(directory, "docs.json.tmp") |
| 67 | if e = os.WriteFile(temporary, metadata, 0600); e != nil { |
| 68 | t.Fatal(e) |
| 69 | } |
| 70 | if e = os.Rename(temporary, filepath.Join(directory, "docs.json")); e != nil { |
| 71 | t.Fatal(e) |
| 72 | } |
| 73 | tick := time.NewTicker(100 * time.Millisecond) |
| 74 | defer tick.Stop() |
| 75 | deadline := time.NewTimer(2 * time.Minute) |
| 76 | defer deadline.Stop() |
| 77 | for { |
| 78 | select { |
| 79 | case <-tick.C: |
| 80 | if _, e := os.Stat(filepath.Join(directory, "stop")); e == nil { |
| 81 | return |
| 82 | } |
| 83 | case <-deadline.C: |
| 84 | t.Fatal("suite fixture timed out") |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |