mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 12:24:30 +01:00
Merge pull request #499 from chriswritescode-dev/fix/sse-stream-url-length
fix(frontend): subscribe event stream directories after connecting instead of in the URL
This commit is contained in:
2 files changed
+67
-22
No files matched your search
@@ -242,29 +242,44 @@ describe('OpenCodeEventStream', () => {
|
||||
})
|
||||
})
|
||||
|
||||
it('opens the initial EventSource URL with first subscriber directories', () => {
|
||||
it('opens the initial EventSource without directories in the URL', async () => {
|
||||
const transport = new TestEventStreamTransport()
|
||||
const stream = new OpenCodeEventStream({ transport })
|
||||
|
||||
stream.subscribeGlobalMonitor({ directories: ['/repo'], onEvent: vi.fn() })
|
||||
|
||||
expect(transport.openedUrls).toHaveLength(1)
|
||||
const url = transport.openedUrls[0]
|
||||
expect(url).toContain('/api/sse/stream')
|
||||
expect(url).toContain(encodeURIComponent('/repo'))
|
||||
const url = new URL(transport.openedUrls[0])
|
||||
expect(url.pathname).toBe('/api/sse/stream')
|
||||
expect(url.searchParams.has('directories')).toBe(false)
|
||||
|
||||
transport.openConnection()
|
||||
transport.connected('client-1')
|
||||
await vi.advanceTimersByTimeAsync(0)
|
||||
|
||||
expect(transport.posts).toContainEqual({
|
||||
path: '/api/sse/subscribe',
|
||||
body: { clientId: 'client-1', directories: ['/repo'] },
|
||||
})
|
||||
})
|
||||
|
||||
it('opens the initial EventSource URL with all first subscriber directories', () => {
|
||||
it('subscribes every first subscriber directory after the connection opens', async () => {
|
||||
const transport = new TestEventStreamTransport()
|
||||
const stream = new OpenCodeEventStream({ transport })
|
||||
|
||||
stream.subscribeGlobalMonitor({ directories: ['/repo-a', '/repo-b'], onEvent: vi.fn() })
|
||||
|
||||
expect(transport.openedUrls).toHaveLength(1)
|
||||
const url = transport.openedUrls[0]
|
||||
expect(url).toContain('/api/sse/stream')
|
||||
expect(url).toContain(encodeURIComponent('/repo-a'))
|
||||
expect(url).toContain(encodeURIComponent('/repo-b'))
|
||||
expect(new URL(transport.openedUrls[0]).searchParams.has('directories')).toBe(false)
|
||||
|
||||
transport.openConnection()
|
||||
transport.connected('client-1')
|
||||
await vi.advanceTimersByTimeAsync(0)
|
||||
|
||||
expect(transport.posts).toContainEqual({
|
||||
path: '/api/sse/subscribe',
|
||||
body: { clientId: 'client-1', directories: ['/repo-a', '/repo-b'] },
|
||||
})
|
||||
})
|
||||
|
||||
it('marks health unhealthy when backend connected event reports no upstream connection', () => {
|
||||
@@ -464,13 +479,14 @@ describe('OpenCodeEventStream', () => {
|
||||
expect(transport.openedUrls).toHaveLength(1)
|
||||
})
|
||||
|
||||
it('reconnects with the subscribed directories when restored from bfcache', () => {
|
||||
it('reconnects from bfcache without directories in the URL and resubscribes them', async () => {
|
||||
const transport = new TestEventStreamTransport()
|
||||
const stream = new OpenCodeEventStream({ transport })
|
||||
|
||||
stream.subscribeGlobalMonitor({ directories: ['/repo-a', '/repo-b'], onEvent: vi.fn() })
|
||||
transport.openConnection()
|
||||
transport.connected()
|
||||
transport.connected('client-1')
|
||||
await vi.advanceTimersByTimeAsync(0)
|
||||
|
||||
window.dispatchEvent(new Event('pagehide'))
|
||||
|
||||
@@ -479,10 +495,43 @@ describe('OpenCodeEventStream', () => {
|
||||
window.dispatchEvent(pageshow)
|
||||
|
||||
expect(transport.openedUrls).toHaveLength(2)
|
||||
const url = transport.openedUrls[1]
|
||||
expect(url).toContain('/api/sse/stream')
|
||||
expect(url).toContain(encodeURIComponent('/repo-a'))
|
||||
expect(url).toContain(encodeURIComponent('/repo-b'))
|
||||
const url = new URL(transport.openedUrls[1])
|
||||
expect(url.pathname).toBe('/api/sse/stream')
|
||||
expect(url.searchParams.has('directories')).toBe(false)
|
||||
|
||||
transport.openConnection()
|
||||
transport.connected('client-2')
|
||||
await vi.advanceTimersByTimeAsync(0)
|
||||
|
||||
expect(transport.posts).toContainEqual({
|
||||
path: '/api/sse/subscribe',
|
||||
body: { clientId: 'client-2', directories: ['/repo-a', '/repo-b'] },
|
||||
})
|
||||
})
|
||||
|
||||
it('resubscribes all current directories after a reconnect following a connection error', async () => {
|
||||
const transport = new TestEventStreamTransport()
|
||||
const stream = new OpenCodeEventStream({ transport })
|
||||
|
||||
const subscription = stream.subscribeGlobalMonitor({ directories: ['/repo-a'], onEvent: vi.fn() })
|
||||
transport.openConnection()
|
||||
transport.connected('client-1')
|
||||
await vi.advanceTimersByTimeAsync(0)
|
||||
|
||||
subscription.updateDirectories(['/repo-a', '/repo-b'])
|
||||
await vi.advanceTimersByTimeAsync(0)
|
||||
transport.posts.length = 0
|
||||
|
||||
transport.fail()
|
||||
await vi.advanceTimersByTimeAsync(1_000)
|
||||
transport.openConnection()
|
||||
transport.connected('client-2')
|
||||
await vi.advanceTimersByTimeAsync(0)
|
||||
|
||||
expect(transport.posts).toContainEqual({
|
||||
path: '/api/sse/subscribe',
|
||||
body: { clientId: 'client-2', directories: ['/repo-a', '/repo-b'] },
|
||||
})
|
||||
})
|
||||
|
||||
it('does not reconnect on a pageshow that is not from bfcache', () => {
|
||||
|
||||
@@ -226,17 +226,14 @@ export class OpenCodeEventStream {
|
||||
}
|
||||
|
||||
private buildUrl(): string {
|
||||
const url = new URL('/api/sse/stream', window.location.origin)
|
||||
const directories = Array.from(this.directoryRefCounts.keys())
|
||||
if (directories.length > 0) {
|
||||
url.searchParams.set('directories', directories.join(','))
|
||||
}
|
||||
return url.toString()
|
||||
return new URL('/api/sse/stream', window.location.origin).toString()
|
||||
}
|
||||
|
||||
private connect(): void {
|
||||
if (this.connection) return
|
||||
|
||||
this.pendingDirectories = new Set(this.directoryRefCounts.keys())
|
||||
|
||||
this.connection = this.transport.open(this.buildUrl(), {
|
||||
onOpen: () => this.handleOpen(),
|
||||
onError: () => this.handleError(),
|
||||
@@ -360,7 +357,6 @@ export class OpenCodeEventStream {
|
||||
this.upstreamConnectedCount = null
|
||||
this.upstreamTotalCount = null
|
||||
this.lastEventAt = null
|
||||
this.pendingDirectories = new Set(this.directoryRefCounts.keys())
|
||||
this.notifyStatusChange(false)
|
||||
this.notifyHealth()
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user