Files
llama.cpp/ui/apps/chat/tests/client/groups-db.svelte.test.ts
T
Aleksander Grygier 67cf8967eb ui: move Llama Chat to SvelteKit 3 and its built-in service worker (#32463)
* ui: replace deprecated packages and fix npm audit findings

Swap the mdast stub for @types/mdast, move the katex override to 0.19.0,
and override glob, source-map and html-encoding-sniffer under
workbox-build and http-server so installs no longer pull deprecated
packages. Apply the non-breaking npm audit fixes to the lockfile.

Assisted-by: Claude (Opus 5.5 Medium)

* ui: migrate Llama Chat to SvelteKit 3 and its built-in service worker

Move to SvelteKit 3, Vite 8, TypeScript 6 and Storybook 10.6. The
SvelteKit options move from svelte.config.js into sveltekit() in
vite.config.ts, $lib imports become #lib subpath imports from
package.json, and $app/environment becomes $app/env. Modules named
server.* or living under a server/ folder are server-only in SvelteKit 3,
so the llama-server components, store and enums move to llama-server
names.

@vite-pwa/sveltekit and Workbox give way to SvelteKit's own service
worker in src/service-worker, registered by usePwa(), which precaches the
app files and leaves API requests to the network. A small plugin emits
manifest.webmanifest. static/sw.js removes the worker of older builds, and
llama-server serves a self-removing service-worker.js as well as sw.js
when the built-in UI is off or replaced.

Assisted-by: Claude (Opus 5.5 Medium)

* ui: use SvelteKit 3's update detection and Svelte 5's current patterns

A page the service worker doesn't control now learns of a new build
from SvelteKit itself: `updated` from $app/state checks
_app/version.json on navigation, on focus and every hour. It replaces
the version Llama Chat kept in localStorage and compared by hand.
Reload on the update prompt pulls in the new worker and activates it
before reloading, so one click lands on the new build, whether or not
a worker controlled the page.

Following Svelte's current guidance:
- usePwa keeps its state in $state rather than a store, and no effect
  checks `browser`
- nearViewport, autoResizeTextarea and the custom CSS injection are
  attachments ({@attach}) rather than actions (use:)
- class lists use arrays rather than class: directives
- the chat contexts come from createContext, typed, in place of
  setContext/getContext with string keys
- ActionIconCopyToClipboard takes $props() rather than `export let`

Assisted-by: Claude (Opus 5.5 Medium)

* ui: move to ESLint 10

ESLint 9 is out of support. Every plugin the app uses supports 10:
typescript-eslint, eslint-plugin-svelte, perfectionist,
simple-import-sort, eslint-plugin-storybook and eslint-config-prettier.
@eslint/js and @eslint/compat follow.

ESLint 10's recommended rules ask for two things the code now does:
errors rethrown from a catch keep the caught error as their `cause`,
and no variable starts with a value that every path overwrites.

Assisted-by: Claude (Opus 5.5 Medium)

* ui: override minimatch to clear npm audit's last finding

ESLint 10.14, @eslint/config-array 0.23.5 and typescript-eslint 8.72
depend on minimatch 10, which GHSA-g3hv-75wg-m249 (denial of service
via globparts amplification) affects below 11.0.1. Their fixed releases
move to minimatch 11 but are all younger than the repo's 7-day
min-release-age, so an override pins minimatch 11.0.5, the newest the
rule allows. The three only call new Minimatch(), match() and
minimatch(), which minimatch 11 keeps. npm audit now finds nothing.

Assisted-by: Claude (Opus 5.5 Medium)
2026-11-17 11:43:36 +01:00

99 lines
3.8 KiB
TypeScript

import { DatabaseService } from '#lib/services/database.service.ts';
import type { Group } from '#lib/types/index.ts';
import { afterEach, describe, expect, it } from 'vitest';
function makeGroup(id: string): Group {
return { createdAt: 0, id, name: `Group ${id}`, updatedAt: 0 };
}
afterEach(async () => {
const groups = await DatabaseService.getAllGroups();
await Promise.all(groups.map((group) => DatabaseService.deleteGroup(group.id)));
const conversations = await DatabaseService.getAllConversations();
await Promise.all(
conversations.map((conversation) => DatabaseService.deleteConversation(conversation.id))
);
});
/**
* Groups live in their own table; deleting one must leave its chats in place,
* out of any group.
*/
describe('DatabaseService groups', () => {
it('round-trips a group', async () => {
await DatabaseService.createGroup(makeGroup('a'));
expect(await DatabaseService.getAllGroups()).toEqual([makeGroup('a')]);
});
it('applies partial updates without dropping other fields', async () => {
await DatabaseService.createGroup(makeGroup('a'));
await DatabaseService.updateGroup('a', { name: 'Renamed', updatedAt: 5 });
expect(await DatabaseService.getAllGroups()).toEqual([
{ ...makeGroup('a'), name: 'Renamed', updatedAt: 5 }
]);
});
it('returns groups newest first by creation, whatever their updates', async () => {
await DatabaseService.createGroup({ ...makeGroup('a'), createdAt: 1, updatedAt: 9 });
await DatabaseService.createGroup({ ...makeGroup('b'), createdAt: 2, updatedAt: 3 });
expect((await DatabaseService.getAllGroups()).map((group) => group.id)).toEqual(['b', 'a']);
});
it('takes the chats out of a deleted group and keeps them', async () => {
await DatabaseService.createGroup(makeGroup('a'));
const inGroup = await DatabaseService.createConversation('in', { groupId: 'a' });
const elsewhere = await DatabaseService.createConversation('out', { groupId: 'b' });
await DatabaseService.deleteGroup('a');
expect(await DatabaseService.getAllGroups()).toEqual([]);
expect((await DatabaseService.getConversation(inGroup.id))?.groupId).toBeUndefined();
expect((await DatabaseService.getConversation(elsewhere.id))?.groupId).toBe('b');
});
it("sorts imported chats into the file's group, by id, then by name, else a new one", async () => {
await DatabaseService.createGroup(makeGroup('a'));
await DatabaseService.createGroup({ ...makeGroup('b'), name: 'Research' });
const chat = (id: string, groupId?: string) => ({
conv: { currNode: '', groupId, id, lastModified: 0, name: id },
messages: []
});
await DatabaseService.importConversations([
{ ...chat('same-id', 'a'), group: { id: 'a', name: 'Other name' } },
{ ...chat('same-name', 'x'), group: { id: 'x', name: 'Research' } },
{ ...chat('new-group', 'y'), group: { id: 'y', name: 'Travel' } },
chat('dangling', 'gone')
]);
const groupOf = async (id: string) => (await DatabaseService.getConversation(id))?.groupId;
expect(await groupOf('same-id')).toBe('a');
expect(await groupOf('same-name')).toBe('b');
expect(await groupOf('new-group')).toBe('y');
expect(await groupOf('dangling')).toBeUndefined();
expect((await DatabaseService.getAllGroups()).map((group) => group.name)).toContain('Travel');
});
it('keeps a forked chat in the group of the chat it was forked from', async () => {
await DatabaseService.createGroup(makeGroup('a'));
const source = await DatabaseService.createConversation('source', { groupId: 'a' });
const rootId = await DatabaseService.createRootMessage(source.id);
const fork = await DatabaseService.forkConversation(source.id, rootId, {
includeAttachments: false,
name: 'fork'
});
expect(fork.groupId).toBe('a');
expect((await DatabaseService.getConversation(fork.id))?.groupId).toBe('a');
});
});