mirror of
https://github.com/charmbracelet/crush.git
synced 2026-05-30 18:47:33 +00:00
Adds in process tests that drive the server through realistic multi client scenarios over HTTP and SSE: two clients sharing a workspace by path see the same events, permission grants resolved by one client are observed by the other and idempotent on the wire, killing one client's event stream does not disturb the other, and the server's shutdown callback fires only after the last client leaves. Co-Authored-By: Charm Crush <crush@charm.land>
56 lines
1.8 KiB
Go
56 lines
1.8 KiB
Go
package backend
|
|
|
|
// InsertWorkspaceForTest registers ws with b under its current ID and
|
|
// path. It is intended for tests in other packages that need to drive
|
|
// HTTP handlers against a synthetic workspace without booting a real
|
|
// app.App. Production code should go through CreateWorkspace.
|
|
func InsertWorkspaceForTest(b *Backend, ws *Workspace) {
|
|
if ws.resolvedPath == "" {
|
|
ws.resolvedPath = ws.Path
|
|
}
|
|
if ws.clients == nil {
|
|
ws.clients = make(map[string]*clientState)
|
|
}
|
|
b.mu.Lock()
|
|
defer b.mu.Unlock()
|
|
b.workspaces.Set(ws.ID, ws)
|
|
if ws.resolvedPath != "" {
|
|
b.pathIndex[ws.resolvedPath] = ws.ID
|
|
}
|
|
}
|
|
|
|
// RegisterClientForTesting installs a creation hold for clientID on
|
|
// ws using the backend's normal registerClient path. Intended for
|
|
// tests in other packages that need to drive a hold-only client
|
|
// (streams == 0) without booting a real CreateWorkspace flow.
|
|
func RegisterClientForTesting(b *Backend, ws *Workspace, clientID string) error {
|
|
if _, err := validateClientID(clientID); err != nil {
|
|
return err
|
|
}
|
|
b.registerClient(ws, clientID)
|
|
return nil
|
|
}
|
|
|
|
// SetWorkspaceShutdownFnForTest overrides the workspace teardown
|
|
// callback. Useful for tests in other packages that drive synthetic
|
|
// workspaces (where the embedded [app.App] is incomplete) through
|
|
// detach paths that would otherwise crash inside App.Shutdown.
|
|
func SetWorkspaceShutdownFnForTest(ws *Workspace, fn func()) {
|
|
ws.shutdownFn = fn
|
|
}
|
|
|
|
// WorkspaceLiveStreamCountForTest returns the number of clients on ws
|
|
// that have at least one live SSE stream. Used by integration tests
|
|
// in other packages to wait for SSE attaches before publishing events.
|
|
func WorkspaceLiveStreamCountForTest(ws *Workspace) int {
|
|
ws.clientsMu.Lock()
|
|
defer ws.clientsMu.Unlock()
|
|
n := 0
|
|
for _, cs := range ws.clients {
|
|
if cs.streams > 0 {
|
|
n++
|
|
}
|
|
}
|
|
return n
|
|
}
|