-
Notifications
You must be signed in to change notification settings - Fork 16
test(kit): improve conversation and message coverage #407
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
SonyLeo
merged 3 commits into
opentiny:develop
from
gene9831:codex/kit-conversation-message-tests
Sep 9, 2026
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| import 'fake-indexeddb/auto' | ||
| import { reactive } from 'vue' | ||
| import { afterEach, describe, expect, it, vi } from 'vitest' | ||
| import type { ChatMessage } from '../types' | ||
| import type { ConversationInfo } from '../vue/conversation/types' | ||
| import { indexedDBStorageStrategyFactory, localStorageStrategyFactory } from './factories' | ||
|
|
||
| const createLocalStorage = () => { | ||
| const values = new Map<string, string>() | ||
| const storage: Storage = { | ||
| get length() { | ||
| return values.size | ||
| }, | ||
| clear: () => values.clear(), | ||
| getItem: (key) => values.get(key) ?? null, | ||
| key: (index) => [...values.keys()][index] ?? null, | ||
| removeItem: (key) => values.delete(key), | ||
| setItem: (key, value) => values.set(key, value), | ||
| } | ||
| return { storage, values } | ||
| } | ||
|
|
||
| const conversation = (id: string, updatedAt: number): ConversationInfo => ({ | ||
| id, | ||
| title: `Conversation ${id}`, | ||
| createdAt: updatedAt - 1, | ||
| updatedAt, | ||
| metadata: { source: 'test' }, | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| vi.unstubAllGlobals() | ||
| vi.restoreAllMocks() | ||
| }) | ||
|
|
||
| describe('localStorageStrategyFactory', () => { | ||
| it('persists conversation metadata and restores legacy messages through the configured key', async () => { | ||
| const { storage, values } = createLocalStorage() | ||
| vi.stubGlobal('localStorage', storage) | ||
| const strategy = localStorageStrategyFactory({ key: 'conversation-test' }) | ||
| const legacyMessage = { | ||
| role: 'assistant', | ||
| content: 'stale', | ||
| renderContent: [ | ||
| { type: 'collapsible-text', content: 'thinking ' }, | ||
| { type: 'collapsible-text', content: 'done' }, | ||
| { type: 'markdown', content: 'Hello ' }, | ||
| { type: 'text', content: 'world' }, | ||
| ], | ||
| } as ChatMessage | ||
|
|
||
| await strategy.saveConversation(conversation('one', 10)) | ||
| await strategy.saveConversation({ ...conversation('one', 20), title: 'Updated title' }) | ||
| await strategy.saveMessages('one', [legacyMessage]) | ||
|
|
||
| expect(await strategy.loadConversations()).toEqual([ | ||
| { | ||
| id: 'one', | ||
| title: 'Updated title', | ||
| createdAt: 19, | ||
| updatedAt: 20, | ||
| metadata: { source: 'test' }, | ||
| }, | ||
| ]) | ||
| expect(await strategy.loadMessages('one')).toEqual([ | ||
| { | ||
| role: 'assistant', | ||
| content: 'Hello world', | ||
| reasoning_content: 'thinking done', | ||
| }, | ||
| ]) | ||
| expect(JSON.parse(values.get('conversation-test') ?? '[]')).toHaveLength(1) | ||
|
|
||
| await strategy.deleteConversation?.('one') | ||
|
|
||
| expect(await strategy.loadConversations()).toEqual([]) | ||
| expect(await strategy.loadMessages('one')).toEqual([]) | ||
| }) | ||
|
|
||
| it('returns empty data and keeps delete safe when persisted JSON is corrupted', async () => { | ||
| const { storage, values } = createLocalStorage() | ||
| values.set('corrupted', '{not-json') | ||
| vi.stubGlobal('localStorage', storage) | ||
| vi.spyOn(console, 'error').mockImplementation(() => undefined) | ||
| const strategy = localStorageStrategyFactory({ key: 'corrupted' }) | ||
|
|
||
| expect(await strategy.loadConversations()).toEqual([]) | ||
| expect(await strategy.loadMessages('missing')).toEqual([]) | ||
| expect(() => strategy.deleteConversation?.('missing')).not.toThrow() | ||
| }) | ||
| }) | ||
|
|
||
| describe('indexedDBStorageStrategyFactory', () => { | ||
| it('creates, updates, sorts, restores, and deletes conversations', async () => { | ||
| const strategy = indexedDBStorageStrategyFactory({ | ||
| dbName: `tiny-robot-conversations-${crypto.randomUUID()}`, | ||
| dbVersion: 1, | ||
| }) | ||
| const first = reactive(conversation('first', 10)) as ConversationInfo | ||
| const second = reactive(conversation('second', 20)) as ConversationInfo | ||
| const messages = reactive([ | ||
| { | ||
| role: 'assistant', | ||
| content: '', | ||
| renderContent: [ | ||
| { type: 'collapsible-text', content: 'reason' }, | ||
| { type: 'markdown', content: 'answer' }, | ||
| ], | ||
| }, | ||
| ]) as ChatMessage[] | ||
|
|
||
| await strategy.saveConversation(first) | ||
| await strategy.saveConversation(second) | ||
| await strategy.saveConversation({ ...first, title: 'First updated', updatedAt: 30 }) | ||
| await strategy.saveMessages('first', messages) | ||
|
|
||
| expect(await strategy.loadConversations()).toEqual([ | ||
| { ...conversation('first', 10), title: 'First updated', updatedAt: 30 }, | ||
| conversation('second', 20), | ||
| ]) | ||
| expect(await strategy.loadMessages('first')).toEqual([ | ||
| { role: 'assistant', content: 'answer', reasoning_content: 'reason' }, | ||
| ]) | ||
| expect(await strategy.loadMessages('missing')).toEqual([]) | ||
|
|
||
| await strategy.deleteConversation?.('first') | ||
|
|
||
| expect(await strategy.loadConversations()).toEqual([conversation('second', 20)]) | ||
| expect(await strategy.loadMessages('first')).toEqual([]) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| import { reactive } from 'vue' | ||
| import { describe, expect, it, vi } from 'vitest' | ||
| import type { ChatMessage } from '../types' | ||
| import { transformMessages, unwrapProxy } from './utils' | ||
|
|
||
| describe('unwrapProxy', () => { | ||
| it('creates a serializable graph while preserving supported values and shared references', () => { | ||
| const shared = { value: 1 } | ||
| const source: Record<string, unknown> = { | ||
| nested: shared, | ||
| list: [shared], | ||
| date: new Date('2026-09-08T00:00:00.000Z'), | ||
| regexp: /robot/gi, | ||
| buffer: new Uint8Array([1, 2, 3]).buffer, | ||
| blob: new Blob(['hello'], { type: 'text/plain' }), | ||
| callback: () => 'ignored', | ||
| token: Symbol('ignored'), | ||
| } | ||
| Object.defineProperty(source, 'computed', { enumerable: true, get: () => 'ignored' }) | ||
| source.self = source | ||
|
|
||
| const result = unwrapProxy(reactive(source)) | ||
|
|
||
| expect(result).not.toBe(source) | ||
| expect(result.nested).toEqual({ value: 1 }) | ||
| expect(result.nested).toBe((result.list as unknown[])[0]) | ||
| expect(result.date).toBe(source.date) | ||
| expect(result.regexp).toBe(source.regexp) | ||
| expect(result.buffer).toBe(source.buffer) | ||
| expect(result.blob).toBe(source.blob) | ||
| expect(result.self).toBe(result) | ||
| expect(result).not.toHaveProperty('callback') | ||
| expect(result).not.toHaveProperty('token') | ||
| expect(result).not.toHaveProperty('computed') | ||
| }) | ||
|
|
||
| it('passes through null, undefined, and primitive values', () => { | ||
| expect(unwrapProxy(null)).toBeNull() | ||
| expect(unwrapProxy(undefined)).toBeUndefined() | ||
| expect(unwrapProxy('text')).toBe('text') | ||
| expect(unwrapProxy(42)).toBe(42) | ||
| }) | ||
|
|
||
| it('falls back to an empty container when an object cannot be inspected', () => { | ||
| vi.spyOn(console, 'warn').mockImplementation(() => undefined) | ||
| const inaccessible = new Proxy( | ||
| {}, | ||
| { | ||
| ownKeys() { | ||
| throw new Error('cannot inspect') | ||
| }, | ||
| }, | ||
| ) | ||
|
|
||
| expect(unwrapProxy(inaccessible)).toEqual({}) | ||
| }) | ||
| }) | ||
|
|
||
| describe('transformMessages', () => { | ||
| it('preserves modern message content', () => { | ||
| const message: ChatMessage = { role: 'user', content: 'already modern' } | ||
|
|
||
| expect(transformMessages([message])).toEqual([{ role: 'user', content: 'already modern' }]) | ||
| }) | ||
|
|
||
| it('combines legacy reasoning and text segments and removes renderContent', () => { | ||
| const message = { | ||
| role: 'assistant', | ||
| content: 'old', | ||
| metadata: { id: 'message-1' }, | ||
| renderContent: [ | ||
| { type: 'collapsible-text', content: 'reason ' }, | ||
| { type: 'collapsible-text', content: 'complete' }, | ||
| { type: 'markdown', content: 'new ' }, | ||
| { type: 'text', content: 'answer' }, | ||
| { type: 'image', content: 'ignored' }, | ||
| ], | ||
| } as ChatMessage | ||
|
|
||
| expect(transformMessages([message])).toEqual([ | ||
| { | ||
| role: 'assistant', | ||
| content: 'new answer', | ||
| reasoning_content: 'reason complete', | ||
| metadata: { id: 'message-1' }, | ||
| }, | ||
| ]) | ||
| }) | ||
| }) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.