File size: 3,444 Bytes
82be208
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Field updates to the active conversation must keep the object identity
// stable: effects that track the identity ( the chat screen's sibling-info
// refresh ) refire on every identity change, which used to trigger a full
// message refetch on every send and tool result.

import { beforeEach, describe, expect, it, vi } from 'vitest';

vi.mock('$lib/services/database.service', () => ({
	DatabaseService: {
		getConversation: vi.fn(),
		getConversationMessages: vi.fn(),
		updateConversation: vi.fn(),
		updateCurrentNode: vi.fn()
	}
}));

import { DatabaseService } from '$lib/services/database.service';
import { conversationsStore } from '$lib/stores/conversations/index.svelte';
import type { DatabaseConversation, DatabaseMessage } from '$lib/types/database';

const getConversationMock = vi.mocked(DatabaseService.getConversation);
const getMessagesMock = vi.mocked(DatabaseService.getConversationMessages);
const updateCurrentNodeMock = vi.mocked(DatabaseService.updateCurrentNode);

function makeConversation(overrides: Partial<DatabaseConversation> = {}): DatabaseConversation {
	return {
		currNode: 'node-1',
		id: 'conv-1',
		lastModified: 1000,
		name: 'conversation',
		...overrides
	};
}

async function loadActive(conversation: DatabaseConversation, messages: DatabaseMessage[]) {
	getConversationMock.mockResolvedValue(conversation);
	getMessagesMock.mockResolvedValue(messages);

	expect(await conversationsStore.loadConversation(conversation.id)).toBe(true);
}

beforeEach(() => {
	getConversationMock.mockReset();
	getMessagesMock.mockReset();
	updateCurrentNodeMock.mockReset();
	updateCurrentNodeMock.mockResolvedValue(undefined);
	vi.mocked(DatabaseService.updateConversation).mockReset();
	vi.mocked(DatabaseService.updateConversation).mockResolvedValue(undefined);
});

describe('active conversation identity', () => {
	it('hands the load read off exactly once', async () => {
		await loadActive(makeConversation(), []);

		expect(conversationsStore.consumeLastLoadedMessages('conv-1')).toEqual([]);
		// a second consume is a miss: branch actions must fall back to a refetch
		expect(conversationsStore.consumeLastLoadedMessages('conv-1')).toBeNull();
	});

	it('writes currNode in place on updateCurrentNode', async () => {
		await loadActive(makeConversation(), []);
		const before = conversationsStore.activeConversation;

		await conversationsStore.updateCurrentNode('node-2');

		expect(conversationsStore.activeConversation).toBe(before);
		expect(conversationsStore.activeConversation?.currNode).toBe('node-2');
	});

	it('writes renamed and pinned fields in place on applyConversationUpdate', async () => {
		await loadActive(makeConversation(), []);
		const before = conversationsStore.activeConversation;

		conversationsStore.applyConversationUpdate('conv-1', { name: 'renamed', pinned: true });

		expect(conversationsStore.activeConversation).toBe(before);
		expect(conversationsStore.activeConversation?.name).toBe('renamed');
		expect(conversationsStore.activeConversation?.pinned).toBe(true);
	});

	it('writes lastModified in place on updateConversationTimestamp', async () => {
		await loadActive(makeConversation(), []);
		const before = conversationsStore.activeConversation;

		conversationsStore.updateConversationTimestamp('conv-1');

		expect(conversationsStore.activeConversation).toBe(before);
		expect(conversationsStore.activeConversation?.lastModified).toBeGreaterThan(1000);
	});
});