153 lines
5.7 KiB
TypeScript
153 lines
5.7 KiB
TypeScript
// 앱 안에서만 배우는 앱(Agent Switchboard) — 그 앱 표본이 그 앱 제안의 재료가 된다(2026-10-25).
|
|
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
|
|
const config = vi.hoisted(() => ({
|
|
suggestionEnabled: true as boolean,
|
|
suggestionModelId: 'gemma4:e4b' as string | null,
|
|
llmModelId: 'gemma4:e4b' as string | null,
|
|
inputExcludedApps: [] as string[],
|
|
suggestionTriggerDelayMs: 600,
|
|
suggestionMinPrefixChars: 4,
|
|
suggestionMaxRequestsPerMinute: 20,
|
|
suggestionDailyBudget: 500,
|
|
suggestionRequestTimeoutMs: 8000,
|
|
inputLearnTypedText: false,
|
|
inputTelemetryEnabled: false,
|
|
suggestionOverlayInteractive: true,
|
|
insertMethod: 'clipboard'
|
|
}))
|
|
const harness = vi.hoisted(() => ({
|
|
streamGenerate: vi.fn(),
|
|
findContinuationHints: vi.fn((_tail: string, _limit?: number, _options?: { appName?: string | null }) => [] as string[]),
|
|
insertText: vi.fn(async (text: string) => ({ success: true, method: 'clipboard', textLength: text.length })),
|
|
replaceBeforeCaret: vi.fn(async (_count: number, text: string) => ({
|
|
success: true,
|
|
method: 'clipboard',
|
|
textLength: text.length
|
|
}))
|
|
}))
|
|
|
|
vi.mock('../../../src/main/services/ConfigService', () => ({
|
|
configGet: vi.fn((key: keyof typeof config) => config[key]),
|
|
configSet: vi.fn()
|
|
}))
|
|
vi.mock('../../../src/main/services/LoggerService', () => ({
|
|
getLogger: () => ({ info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() })
|
|
}))
|
|
vi.mock('../../../src/main/services/LocalLLMService', () => ({
|
|
getLocalLLMService: () => ({ isAvailable: () => true, streamGenerate: harness.streamGenerate })
|
|
}))
|
|
vi.mock('../../../src/main/services/InputTelemetryService', () => ({
|
|
getInputTelemetryService: () => ({
|
|
listPhrases: () => [],
|
|
listRecentWrites: () => [],
|
|
findContinuationHints: harness.findContinuationHints
|
|
})
|
|
}))
|
|
vi.mock('../../../src/main/services/PersonalGraphService', () => ({
|
|
getPersonalGraphService: () => ({ retrieveContext: () => ({ continuations: [], related: [] }) })
|
|
}))
|
|
vi.mock('../../../src/main/services/TextInsertService', () => ({
|
|
getTextInsertService: () => ({ insertText: harness.insertText, replaceBeforeCaret: harness.replaceBeforeCaret })
|
|
}))
|
|
vi.mock('../../../src/main/services/modifier-state', () => ({ prepareModifiersForPaste: async () => 'released' }))
|
|
vi.mock('../../../src/main/utils/win32-foreground', () => ({ getForegroundWindowInfo: () => null }))
|
|
vi.mock('../../../src/main/db', () => ({ getDatabase: vi.fn() }))
|
|
vi.mock('../../../src/main/db/schema', () => ({ suggestions: {} }))
|
|
|
|
import {
|
|
SuggestionService,
|
|
type SuggestionServiceDeps
|
|
} from '../../../src/main/services/SuggestionService'
|
|
import { SuggestionBudget } from '../../../src/main/services/suggestion/SuggestionBudget'
|
|
import { ModelWarmTracker } from '../../../src/main/services/suggestion/ModelWarmTracker'
|
|
import type { TypingContext } from '../../../src/main/services/InputTelemetryService'
|
|
|
|
const PREFIX = '지금 테스트 해보는데 잠시 쉬면'
|
|
|
|
function typingContext(overrides: Partial<TypingContext> = {}): TypingContext {
|
|
const prefix = overrides.prefix ?? PREFIX
|
|
return {
|
|
prefix,
|
|
fullText: prefix,
|
|
caretOffset: prefix.length,
|
|
documentBefore: prefix,
|
|
anchor: { x: 10, y: 10, width: 2, height: 16 },
|
|
anchorKind: 'caret',
|
|
isPassword: false,
|
|
isEditable: true,
|
|
isComposing: false,
|
|
hasSelection: false,
|
|
available: true,
|
|
appName: 'notepad.exe',
|
|
windowTitle: 'notes',
|
|
windowHandle: null,
|
|
idleMs: 2000,
|
|
capturedAt: Date.now(),
|
|
editedSinceFocus: true,
|
|
typedRecently: true,
|
|
...overrides
|
|
}
|
|
}
|
|
|
|
function deps(): SuggestionServiceDeps {
|
|
const warmth = new ModelWarmTracker()
|
|
warmth.noteWarm('gemma4:e4b', Date.now() + 600_000)
|
|
return {
|
|
repository: { record: vi.fn(), markAccepted: vi.fn(), list: vi.fn(() => []) },
|
|
budget: new SuggestionBudget(),
|
|
warmth,
|
|
foreground: { currentWindowHandle: () => null },
|
|
learning: { expectProgrammaticInsert: vi.fn(() => vi.fn()), recordAccepted: vi.fn() }
|
|
}
|
|
}
|
|
|
|
interface Internal {
|
|
_generate(prefix: string, context: TypingContext, maxCandidates: number, maxChars: number): Promise<void>
|
|
_lastContext: TypingContext | null
|
|
_lastRequestAt: number
|
|
}
|
|
|
|
let services: SuggestionService[] = []
|
|
function create(): SuggestionService {
|
|
const service = new SuggestionService(deps())
|
|
services.push(service)
|
|
return service
|
|
}
|
|
|
|
beforeEach(() => {
|
|
harness.streamGenerate.mockReset()
|
|
harness.findContinuationHints.mockClear()
|
|
harness.streamGenerate.mockImplementation(async function* () {
|
|
yield ' 확인해 줄래.'
|
|
})
|
|
})
|
|
|
|
afterEach(() => {
|
|
for (const service of services) service.dispose()
|
|
services = []
|
|
})
|
|
|
|
async function promptFor(appName: string): Promise<string> {
|
|
const prefix = '그래서 말인데 개발자 도구로 열어서'
|
|
const service = create()
|
|
const context = typingContext({ prefix, documentBefore: prefix, fullText: prefix, caretOffset: prefix.length, appName })
|
|
;(service as unknown as Internal)._lastContext = context
|
|
await (service as unknown as Internal)._generate(prefix, context, 3, 160)
|
|
return String(harness.streamGenerate.mock.calls[0]?.[0] ?? '')
|
|
}
|
|
|
|
describe('앱 안에서만 배우는 앱의 제안 재료 (Agent Switchboard)', () => {
|
|
it('같은 앱 표본에서 이어 쓴 글을 찾아 프롬프트 힌트로 넣는다', async () => {
|
|
harness.findContinuationHints.mockReturnValue(['확인해 줄래.'])
|
|
const prompt = await promptFor('Agent Switchboard.exe')
|
|
expect(harness.findContinuationHints).toHaveBeenCalledWith(expect.any(String), 3, { appName: 'Agent Switchboard.exe' })
|
|
expect(prompt).toContain('확인해 줄래.')
|
|
})
|
|
|
|
it('다른 앱에서는 이 경로를 쓰지 않는다', async () => {
|
|
await promptFor('KakaoTalk.exe')
|
|
expect(harness.findContinuationHints).not.toHaveBeenCalled()
|
|
})
|
|
})
|