@wherever-dev/client 0.5.4 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,113 @@
1
+ import {describe, it, expect, beforeEach, afterEach, vi} from 'vitest';
2
+ import {get} from 'sveltore';
3
+ import {WhereverClient} from '../src/client.js';
4
+ import {makeWSFactory} from './harness.js';
5
+
6
+ // `/skill:` composer autocomplete contract. The skill list is only resolvable
7
+ // once the session's live agent exists, so the client asks for it as soon as
8
+ // the agent is live. A brand-new session (session_new) is live IMMEDIATELY and
9
+ // never emits session_ready, so requesting only on session_ready left a fresh
10
+ // session with an empty skill menu (the regression this covers).
11
+
12
+ describe('skill autocomplete list', () => {
13
+ let ws: ReturnType<typeof makeWSFactory>;
14
+ let client: WhereverClient;
15
+
16
+ beforeEach(() => {
17
+ vi.useFakeTimers();
18
+ ws = makeWSFactory();
19
+ client = new WhereverClient({
20
+ host: 'localhost',
21
+ port: 1234,
22
+ secure: false,
23
+ WebSocketCtor: ws.ctor,
24
+ });
25
+ client.connect();
26
+ ws.last().open();
27
+ });
28
+
29
+ afterEach(() => {
30
+ client.disconnect(true);
31
+ vi.useRealTimers();
32
+ });
33
+
34
+ it('requests skills for a freshly created session (no session_ready)', () => {
35
+ client.createSession('/tmp/new');
36
+ ws.last().receive({
37
+ type: 'session_created',
38
+ sessionId: 'sid-new',
39
+ sessionFile: '/tmp/new/session.jsonl',
40
+ cwd: '/tmp/new',
41
+ model: 'fake:model',
42
+ isStreaming: false,
43
+ // no pending flag: the agent is already live
44
+ });
45
+
46
+ const req = ws.last().lastSentOfType('skills_request');
47
+ expect(req).toBeDefined();
48
+ expect(req.sessionId).toBe('sid-new');
49
+
50
+ ws.last().receive({
51
+ type: 'skills_list',
52
+ sessionId: 'sid-new',
53
+ skills: [{name: 'skill:setup', description: 'Set things up'}],
54
+ });
55
+ expect(get(client.stateStore).skills).toEqual([
56
+ {name: 'skill:setup', description: 'Set things up'},
57
+ ]);
58
+ });
59
+
60
+ it('defers the request to session_ready for a cold (pending) load', () => {
61
+ client.joinSession('/tmp/p/session.jsonl');
62
+ ws.last().receive({
63
+ type: 'session_created',
64
+ sessionId: 'sid-cold',
65
+ sessionFile: '/tmp/p/session.jsonl',
66
+ cwd: '/tmp/p',
67
+ model: '',
68
+ isStreaming: false,
69
+ pending: true,
70
+ });
71
+ // The agent is still building: nothing to ask yet.
72
+ expect(ws.last().lastSentOfType('skills_request')).toBeUndefined();
73
+
74
+ ws.last().receive({
75
+ type: 'session_ready',
76
+ sessionId: 'sid-cold',
77
+ sessionFile: '/tmp/p/session.jsonl',
78
+ model: 'fake:model',
79
+ isStreaming: false,
80
+ });
81
+ expect(ws.last().lastSentOfType('skills_request')?.sessionId).toBe('sid-cold');
82
+ });
83
+
84
+ it('clears the previous session skills when attaching to another session', () => {
85
+ client.createSession('/tmp/a');
86
+ ws.last().receive({
87
+ type: 'session_created',
88
+ sessionId: 'sid-a',
89
+ sessionFile: '/tmp/a/session.jsonl',
90
+ cwd: '/tmp/a',
91
+ model: 'fake:model',
92
+ isStreaming: false,
93
+ });
94
+ ws.last().receive({
95
+ type: 'skills_list',
96
+ sessionId: 'sid-a',
97
+ skills: [{name: 'skill:a', description: 'A'}],
98
+ });
99
+ expect(get(client.stateStore).skills.length).toBe(1);
100
+
101
+ client.joinSession('/tmp/b/session.jsonl');
102
+ ws.last().receive({
103
+ type: 'session_created',
104
+ sessionId: 'sid-b',
105
+ sessionFile: '/tmp/b/session.jsonl',
106
+ cwd: '/tmp/b',
107
+ model: 'fake:model',
108
+ isStreaming: false,
109
+ pending: true,
110
+ });
111
+ expect(get(client.stateStore).skills).toEqual([]);
112
+ });
113
+ });
@@ -0,0 +1,70 @@
1
+ import {describe, it, expect, beforeEach, afterEach, vi} from 'vitest';
2
+ import {WhereverClient} from '../src/client.js';
3
+ import {makeWSFactory} from './harness.js';
4
+
5
+ // The clientKey lets the server retire this viewer's own superseded (silently
6
+ // dropped) connection instead of counting it as a second viewer. But the web
7
+ // client scopes the key per tab via sessionStorage, and "Duplicate tab" CLONES
8
+ // sessionStorage: two LIVE tabs can end up sharing one key and evicting each
9
+ // other forever, one reconnect apart.
10
+ //
11
+ // The server therefore tells a connection it is being retired. Receiving that
12
+ // proves this client is alive, so it takes a fresh key before the reconnect --
13
+ // one eviction, then both tabs are distinct viewers again.
14
+
15
+ describe('superseded connection', () => {
16
+ let ws: ReturnType<typeof makeWSFactory>;
17
+ let client: WhereverClient;
18
+ let persisted: string[];
19
+
20
+ function keyOf(url: string): string | null {
21
+ return new URL(url).searchParams.get('clientKey');
22
+ }
23
+
24
+ beforeEach(() => {
25
+ vi.useFakeTimers();
26
+ ws = makeWSFactory();
27
+ persisted = [];
28
+ client = new WhereverClient({
29
+ host: 'localhost',
30
+ port: 1234,
31
+ secure: false,
32
+ WebSocketCtor: ws.ctor,
33
+ clientKey: 'shared-key',
34
+ onClientKeyChange: (k) => persisted.push(k),
35
+ });
36
+ client.connect();
37
+ ws.last().open();
38
+ });
39
+
40
+ afterEach(() => {
41
+ client.disconnect(true);
42
+ vi.useRealTimers();
43
+ });
44
+
45
+ it('sends the configured key on connect', () => {
46
+ expect(keyOf(ws.last().url)).toBe('shared-key');
47
+ });
48
+
49
+ it('takes (and reports) a new key when the server says it was superseded', () => {
50
+ ws.last().receive({type: 'connection_superseded'});
51
+
52
+ expect(persisted).toHaveLength(1);
53
+ expect(persisted[0]).not.toBe('shared-key');
54
+
55
+ // The server closes right after; the reconnect must carry the NEW key, so
56
+ // the two tabs stop evicting each other.
57
+ ws.last().close();
58
+ vi.advanceTimersByTime(5000);
59
+ const reconnected = ws.last();
60
+ expect(keyOf(reconnected.url)).toBe(persisted[0]);
61
+ });
62
+
63
+ it('keeps its key across an ordinary reconnect', () => {
64
+ ws.last().close();
65
+ vi.advanceTimersByTime(5000);
66
+
67
+ expect(keyOf(ws.last().url)).toBe('shared-key');
68
+ expect(persisted).toHaveLength(0);
69
+ });
70
+ });