All files / src/repositories VscodeChatRepository.ts

54.54% Statements 18/33
100% Branches 1/1
20% Functions 1/5
54.54% Lines 18/33

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 341x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     1x 1x     1x 1x     1x 1x                   1x  
import * as vscode from 'vscode';
 
import { IChatRepository } from '../contracts/IChatRepository';
import { Chat } from '../services/chatService';
 
export class VscodeChatRepository implements IChatRepository {
    private static readonly STORAGE_KEY = 'ollama-view.chats';
 
    constructor(private context: vscode.ExtensionContext) {}
 
    getAll(): Chat[] {
        return this.context.globalState.get<Chat[]>(VscodeChatRepository.STORAGE_KEY, []);
    }
 
    getById(id: string): Chat | undefined {
        return this.getAll().find((c) => c.id === id);
    }
 
    async save(chats: Chat[]): Promise<void> {
        await this.context.globalState.update(VscodeChatRepository.STORAGE_KEY, chats);
    }
 
    async saveOne(chat: Chat): Promise<void> {
        const chats = this.getAll();
        const index = chats.findIndex((c) => c.id === chat.id);
        if (index !== -1) {
            chats[index] = chat;
        } else {
            chats.push(chat);
        }
        await this.save(chats);
    }
}