File size: 5,830 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import { beforeEach, describe, expect, it, vi } from 'vitest';

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

import { MessageRole, MessageType } from '$lib/enums';
import { ConversationTransferService } from '$lib/services/conversation-transfer.service';
import { DatabaseService } from '$lib/services/database.service';
import { conversationsStore } from '$lib/stores/conversations/index.svelte';
import type { DatabaseConversation, DatabaseMessage } from '$lib/types/database';
import { filterByLeafNodeId } from '$lib/utils/branching';

/**
 * Reproduces the exported-conversation bug:
 *
 * A conversation created in the current page session keeps `currNode: ''` in the
 * sidebar list, because that list is only loaded at init while IndexedDB is stamped
 * on every message insert.
 *
 * Exporting from the cached record resulted in no branch pointer, and importing
 * the file showed every branch at once.
 */

const fetchMock = vi.mocked(DatabaseService.getConversationsWithMessages);

beforeEach(() => {
	fetchMock.mockReset();
});

const CONV_ID = 'c1';

function message(
	id: string,
	parent: string | null,
	timestamp: number,
	role: MessageRole,
	type: MessageType = MessageType.TEXT
): DatabaseMessage {
	return {
		children: [],
		content: id,
		convId: CONV_ID,
		id,
		parent,
		role,
		timestamp,
		toolCalls: '',
		type
	} as DatabaseMessage;
}

/** root -> u1 -> a1 -> { u2a -> a2a (older) | u2b -> a2b (newer) } */
function branchedMessages(): DatabaseMessage[] {
	const messages = [
		message('root', null, 10, MessageRole.USER, MessageType.ROOT),
		message('u1', 'root', 20, MessageRole.USER),
		message('a1', 'u1', 30, MessageRole.ASSISTANT),
		message('u2a', 'a1', 40, MessageRole.USER),
		message('a2a', 'u2a', 50, MessageRole.ASSISTANT),
		message('u2b', 'a1', 60, MessageRole.USER),
		message('a2b', 'u2b', 70, MessageRole.ASSISTANT)
	];

	for (const m of messages) {
		m.children = messages.filter((c) => c.parent === m.id).map((c) => c.id);
	}

	return messages;
}

/** A second conversation with a single linear path: root -> u1 -> a1. */
function linearMessages(convId: string): DatabaseMessage[] {
	return [
		{ ...message('root', null, 10, MessageRole.USER, MessageType.ROOT), children: ['u1'], convId },
		{ ...message('u1', 'root', 20, MessageRole.USER), children: ['a1'], convId },
		{ ...message('a1', 'u1', 30, MessageRole.ASSISTANT), convId }
	];
}

function conversation(currNode: string, id: string = CONV_ID): DatabaseConversation {
	return { currNode, id, lastModified: 100, name: `Chat ${id}` };
}

/** Mirrors `conversationsStore.loadConversation` */
function displayedIds(imported: { conv: DatabaseConversation; messages: DatabaseMessage[] }) {
	if (imported.conv.currNode) {
		return filterByLeafNodeId(imported.messages, imported.conv.currNode, false).map((m) => m.id);
	}

	return imported.messages.map((m) => m.id);
}

/** Export then re-import */
function roundTrip(conv: DatabaseConversation) {
	const jsonl = ConversationTransferService.serializeSessionToJsonl({
		conv,
		messages: branchedMessages()
	});
	const [imported] = ConversationTransferService.parseSessionsJsonl(jsonl);

	return { imported, sessionLine: JSON.parse(jsonl.split('\n')[0]) };
}

describe('conversation export source', () => {
	it('reads the database record rather than the stale sidebar list', async () => {
		conversationsStore.conversations = [conversation('')];

		fetchMock.mockResolvedValue(
			new Map([[CONV_ID, { conv: conversation('a2a'), messages: branchedMessages() }]])
		);

		const [exported] = await conversationsStore.getConversationsForExport([CONV_ID]);

		expect(exported.conv.currNode).toBe('a2a');
		expect(conversationsStore.conversations[0].currNode).toBe('');
	});

	it('reads every selected conversation from the database on bulk export', async () => {
		conversationsStore.conversations = [conversation(''), conversation('', 'c2')];
		conversationsStore.activeConversation = conversation('');

		fetchMock.mockResolvedValue(
			new Map([
				['c2', { conv: conversation('a1', 'c2'), messages: linearMessages('c2') }],
				[CONV_ID, { conv: conversation('a2a'), messages: branchedMessages() }]
			])
		);

		const archive = vi
			.spyOn(ConversationTransferService, 'downloadConversationsArchive')
			.mockImplementation(() => {});

		await conversationsStore.bulkExportConversations([CONV_ID, 'c2']);

		expect(fetchMock).toHaveBeenCalledWith([CONV_ID, 'c2']);
		expect(archive).toHaveBeenCalledTimes(1);

		const payload = archive.mock.calls[0][0];

		expect(payload.map((entry) => entry.conv.id)).toEqual([CONV_ID, 'c2']);
		// Each entry carries its own database currNode.
		expect(payload.map((entry) => entry.conv.currNode)).toEqual(['a2a', 'a1']);
		expect(payload[1].messages.map((m: DatabaseMessage) => m.id)).toEqual(['root', 'u1', 'a1']);

		archive.mockRestore();
	});
});

describe('exported conversation branch pointer', () => {
	it('carries the database currNode, so the import restores the current branch', () => {
		// The user regenerated to create a2b, then switched back to the a2a branch,
		// so the stored leaf is NOT the newest message.
		const { imported, sessionLine } = roundTrip(conversation('a2a'));

		expect(sessionLine.currNode).toBe('a2a');
		expect(displayedIds(imported)).toEqual(['u1', 'a1', 'u2a', 'a2a']);
		expect(imported.messages.map((m: DatabaseMessage) => m.id).sort()).toEqual([
			'a1',
			'a2a',
			'a2b',
			'root',
			'u1',
			'u2a',
			'u2b'
		]);
	});

	it('shows every branch on import when the cache entry exported an empty currNode', () => {
		const { imported, sessionLine } = roundTrip(conversation(''));

		expect(sessionLine.currNode).toBe('');
		expect(displayedIds(imported)).toEqual(['root', 'u1', 'a1', 'u2a', 'a2a', 'u2b', 'a2b']);
	});
});