/ src / lib / observability / local-observability.test.ts
local-observability.test.ts
 1  import assert from 'node:assert/strict'
 2  import { afterEach, describe, it } from 'node:test'
 3  
 4  import type { Session } from '@/types'
 5  
 6  import {
 7    findLatestObservablePlatformSession,
 8    isLocalhostBrowser,
 9    isVisibleSessionForViewer,
10  } from '@/lib/observability/local-observability'
11  
12  const originalWindow = globalThis.window
13  
14  function makeSession(overrides: Partial<Session>): Session {
15    return {
16      id: overrides.id || 'session-test',
17      name: overrides.name || 'Test Session',
18      user: overrides.user || 'default',
19      messages: overrides.messages || [],
20      createdAt: overrides.createdAt || 1,
21      updatedAt: overrides.updatedAt || overrides.createdAt || 1,
22      lastActiveAt: overrides.lastActiveAt || overrides.updatedAt || overrides.createdAt || 1,
23      provider: overrides.provider || 'openai',
24      model: overrides.model || 'gpt-test',
25      ...overrides,
26    } as Session
27  }
28  
29  afterEach(() => {
30    if (originalWindow === undefined) {
31      delete (globalThis as { window?: Window }).window
32      return
33    }
34    globalThis.window = originalWindow
35  })
36  
37  describe('local observability', () => {
38    it('shows observable platform sessions only on localhost', () => {
39      const workbench = makeSession({ id: 'wb-1', user: 'workbench' })
40      const swarm = makeSession({ id: 'sw-1', user: 'swarm' })
41      const connector = makeSession({ id: 'conn-1', user: 'connector' })
42      const mine = makeSession({ id: 'me-1', user: 'wayde' })
43  
44      assert.equal(isVisibleSessionForViewer(workbench, 'wayde', { localhost: false }), false)
45      assert.equal(isVisibleSessionForViewer(workbench, 'wayde', { localhost: true }), true)
46      assert.equal(isVisibleSessionForViewer(swarm, 'wayde', { localhost: false }), true)
47      assert.equal(isVisibleSessionForViewer(connector, 'wayde', { localhost: false }), false)
48      assert.equal(isVisibleSessionForViewer(mine, 'wayde', { localhost: false }), true)
49    })
50  
51    it('prefers the latest live observable platform session for an agent', () => {
52      const sessions: Record<string, Session> = {
53        old: makeSession({ id: 'old', agentId: 'agent-1', user: 'workbench', lastActiveAt: 100 }),
54        shortcut: makeSession({
55          id: 'shortcut',
56          agentId: 'agent-1',
57          user: 'workbench',
58          lastActiveAt: 500,
59          shortcutForAgentId: 'agent-1',
60        }),
61        latest: makeSession({ id: 'latest', agentId: 'agent-1', user: 'comparison-bench', lastActiveAt: 300 }),
62        otherAgent: makeSession({ id: 'other', agentId: 'agent-2', user: 'workbench', lastActiveAt: 999 }),
63      }
64  
65      assert.equal(findLatestObservablePlatformSession(sessions, 'agent-1')?.id, 'latest')
66    })
67  
68    it('detects localhost browser hosts', () => {
69      globalThis.window = { location: { hostname: "localhost" } } as any
70      assert.equal(isLocalhostBrowser(), true)
71  
72      globalThis.window = { location: { hostname: "swarmclaw.ai" } } as any
73      assert.equal(isLocalhostBrowser(), false)
74    })
75  })