fix: red-team round 3 hardening across desktop, mobile, core and server
Batch of red-team r3 fixes that were in the working tree before the 2026-09-28 design overhaul, committed as one unit with their tests. - desktop main: STT timeouts and sidecar, voice recording store, sync (credentials, audio, knowledge reindex, push gates), runtime provisioner, update policy, AltGr keybindings, voice-command policy, dictionary file codec/limits, meeting transcript condensing and a local recording ledger so interrupted-session recovery only closes meetings this device recorded (a phone's live meeting is left alone). - mobile: login CSRF via implicit token callbacks rejected, account deletion/retention, durable queue retention, knowledge realtime without unfiltered DELETE, meeting re-record failure paths, cloud STT client, preferences store/resync. - core: text chunking splits long unbroken transcripts to fit, template field policy, dictionary limits, meeting markdown inline handling. - server: payple webhook policy and cancellation order scope, meeting document generation quota, team RPC null-role guard, unified LLM quota in-flight accounting, knowledge chunk vector index, meeting re-record failure paths (migrations 20260929*). - ci: portable/runtime feed gates, update-policy schema, Forgejo file delete and alias planning. Four older tests are updated to the new contracts rather than the old behavior: token-pair auth callbacks are rejected, knowledge realtime no longer subscribes to DELETE, long transcript lines are split, and meeting recovery requires the local recording ledger for empty rows.
This commit is contained in:
parent
2428ede03d
commit
ba9ef9741e
161 changed files with 17056 additions and 2379 deletions
81
apps/desktop/tests/main/sync/audio-sync-redteam-r3-1.test.ts
Normal file
81
apps/desktop/tests/main/sync/audio-sync-redteam-r3-1.test.ts
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
// tests/main/sync/audio-sync-redteam-r3-1.test.ts
|
||||
// 동기화 회귀 (r3-1):
|
||||
// - 파일 전사 기록(audioLocalPath = 사용자가 고른 원본 미디어)은 '녹음 동기화'로 올리지 않는다
|
||||
// - 대기 중인 원격 삭제를 뒤늦은 upsert(예: 자동 제목)가 덮어쓰지 않는다
|
||||
|
||||
import fs from 'fs'
|
||||
import os from 'os'
|
||||
import path from 'path'
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { createTestDb } from '../../helpers/createTestDb'
|
||||
import { FakeSyncRemote } from '../../helpers/fakeSyncRemote'
|
||||
import { bindTestDatabase, unbindTestDatabase } from '../../../src/main/db'
|
||||
import { history } from '../../../src/main/db/schema'
|
||||
import { initInMemoryConfig, resetInMemoryConfig } from '../../../src/main/services/ConfigService'
|
||||
import { listLocalAudioOwners, localAudioPath } from '../../../src/main/services/sync/audio-sync'
|
||||
import { SyncEngine } from '../../../src/main/services/sync/SyncEngine'
|
||||
import { enqueueChange, pendingOps } from '../../../src/main/services/sync/sync-outbox'
|
||||
|
||||
const USER = '11111111-1111-4111-8111-111111111111'
|
||||
|
||||
let testDb: ReturnType<typeof createTestDb>
|
||||
let remote: FakeSyncRemote
|
||||
let engine: SyncEngine
|
||||
let tmpDir: string
|
||||
|
||||
beforeEach(() => {
|
||||
testDb = createTestDb()
|
||||
bindTestDatabase(testDb.db, USER)
|
||||
initInMemoryConfig()
|
||||
vi.spyOn(globalThis, 'fetch').mockRejectedValue(new TypeError('fetch failed'))
|
||||
remote = new FakeSyncRemote(USER)
|
||||
engine = new SyncEngine({ remote, userId: USER })
|
||||
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'd3ro-audio-r3-1-'))
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks()
|
||||
engine.dispose()
|
||||
unbindTestDatabase()
|
||||
resetInMemoryConfig()
|
||||
testDb.close()
|
||||
fs.rmSync(tmpDir, { recursive: true, force: true })
|
||||
})
|
||||
|
||||
function insertHistory(mode: 'dictation' | 'file-transcription', fileName: string): string {
|
||||
const id = crypto.randomUUID()
|
||||
const file = path.join(tmpDir, fileName)
|
||||
fs.writeFileSync(file, Buffer.from('RIFF....WAVEfmt fake media'))
|
||||
const now = Date.now()
|
||||
testDb.db
|
||||
.insert(history)
|
||||
.values({ id, originalText: 'spoken', duration: 2, mode, audioLocalPath: file, createdAt: now, updatedAt: now })
|
||||
.run()
|
||||
return id
|
||||
}
|
||||
|
||||
describe('파일 전사 원본은 녹음으로 올리지 않는다 (item 19)', () => {
|
||||
it('파일 전사 기록의 원본 미디어는 업로드 대상이 아니다', async () => {
|
||||
const fileId = insertHistory('file-transcription', 'meeting-video.mp4')
|
||||
const dictationId = insertHistory('dictation', 'dictation.wav')
|
||||
|
||||
expect(localAudioPath('history', fileId)).toBeNull()
|
||||
expect(localAudioPath('history', dictationId)).not.toBeNull()
|
||||
expect(listLocalAudioOwners('history')).toEqual([dictationId])
|
||||
|
||||
const result = await engine.runFullSync()
|
||||
expect(result.errors).toEqual([])
|
||||
expect(remote.objects.has(`audio/${USER}/desktop/history/${fileId}.mp4`)).toBe(false)
|
||||
expect(remote.objects.has(`audio/${USER}/desktop/history/${dictationId}.wav`)).toBe(true)
|
||||
})
|
||||
})
|
||||
|
||||
describe('대기 중인 삭제는 뒤늦은 upsert 에 덮이지 않는다 (item 37)', () => {
|
||||
it('delete 뒤에 같은 행의 upsert 가 와도 op 는 delete 로 남는다', () => {
|
||||
const id = crypto.randomUUID()
|
||||
enqueueChange('history', id, 'upsert')
|
||||
enqueueChange('history', id, 'delete')
|
||||
enqueueChange('history', id, 'upsert')
|
||||
expect(pendingOps('history').get(id)).toBe('delete')
|
||||
})
|
||||
})
|
||||
|
|
@ -0,0 +1,264 @@
|
|||
// tests/main/sync/cloud-credentials-redteam-r3-1.test.ts
|
||||
// 로그아웃이 오프라인·5xx 로 서버 폐기에 실패해도(auth-js 가 { error } 를 돌려주고 _removeSession 을 건너뜀)
|
||||
// 자격 증명 포트(Cloud STT·Premium·realtime-token)가 옛 계정 토큰을 더는 내주지 않는지 검증한다.
|
||||
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import fs from 'fs'
|
||||
import os from 'os'
|
||||
import path from 'path'
|
||||
|
||||
const USER_DATA = path.join(os.tmpdir(), `d3ro-cloudcred-r3-1-${process.pid}`)
|
||||
const TOKEN_FILE = path.join(USER_DATA, 'cloud-sync.token')
|
||||
|
||||
const h = vi.hoisted(() => {
|
||||
type AuthCallback = (event: string, session: unknown) => void
|
||||
const state = {
|
||||
currentUserId: '_local' as string | null,
|
||||
authCallback: null as AuthCallback | null,
|
||||
tierGate: null as Promise<void> | null,
|
||||
}
|
||||
return {
|
||||
state,
|
||||
db: {
|
||||
openForUser: (userId: string) => {
|
||||
state.currentUserId = userId
|
||||
return { created: false, dbPath: `/db/${userId}` }
|
||||
},
|
||||
openLocal: () => {
|
||||
state.currentUserId = '_local'
|
||||
return { created: false, dbPath: '/db/_local' }
|
||||
},
|
||||
},
|
||||
enqueueChange: (..._args: unknown[]) => undefined,
|
||||
}
|
||||
})
|
||||
|
||||
const spies = vi.hoisted(() => ({
|
||||
refreshSession: null as null | ((...args: unknown[]) => Promise<unknown>),
|
||||
signOut: null as null | ((...args: unknown[]) => Promise<unknown>),
|
||||
clients: [] as Array<{ session: null | { access_token: string; user: { id: string } } }>,
|
||||
}))
|
||||
|
||||
vi.mock('electron', () => ({
|
||||
app: {
|
||||
getPath: () => path.join(os.tmpdir(), `d3ro-cloudcred-r3-1-${process.pid}`),
|
||||
getVersion: () => '1.0.0',
|
||||
},
|
||||
safeStorage: {
|
||||
isEncryptionAvailable: () => true,
|
||||
encryptString: (plain: string) => Buffer.from(plain, 'utf-8'),
|
||||
decryptString: (data: Buffer) => data.toString('utf-8'),
|
||||
},
|
||||
}))
|
||||
|
||||
vi.mock('../../../src/main/services/LoggerService', () => ({
|
||||
getLogger: () => ({ info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() }),
|
||||
}))
|
||||
|
||||
vi.mock('../../../src/main/services/ConfigService', () => ({
|
||||
configGet: () => undefined,
|
||||
configSet: vi.fn(),
|
||||
onConfigChanged: () => () => undefined,
|
||||
}))
|
||||
|
||||
vi.mock('../../../src/main/db', () => ({
|
||||
LOCAL_USER_ID: '_local',
|
||||
openForUser: vi.fn((userId: string) => h.db.openForUser(userId)),
|
||||
openLocal: vi.fn(() => h.db.openLocal()),
|
||||
closeCurrent: vi.fn(() => {
|
||||
h.state.currentUserId = null
|
||||
}),
|
||||
getCurrentUserId: () => h.state.currentUserId,
|
||||
importLocalModeData: vi.fn(() => null),
|
||||
}))
|
||||
|
||||
vi.mock('../../../src/main/services/sync/sync-outbox', () => ({
|
||||
enqueueChange: vi.fn((...args: unknown[]) => h.enqueueChange(...args)),
|
||||
getSyncState: () => null,
|
||||
setSyncState: vi.fn(),
|
||||
}))
|
||||
|
||||
vi.mock('../../../src/main/services/sync/SyncEngine', () => ({
|
||||
SyncEngine: class {
|
||||
on(): void {}
|
||||
dispose(): void {}
|
||||
async whenIdle(): Promise<boolean> {
|
||||
return true
|
||||
}
|
||||
async runFullSync() {
|
||||
return { pushed: 0, pulled: 0, deleted: 0, errors: [], changed: [] }
|
||||
}
|
||||
async flush() {
|
||||
return { pushed: 0, pulled: 0, deleted: 0, errors: [], changed: [] }
|
||||
}
|
||||
async pull() {
|
||||
return { pushed: 0, pulled: 0, deleted: 0, errors: [], changed: [] }
|
||||
}
|
||||
getStatus() {
|
||||
return { pending: 0, parked: 0 }
|
||||
}
|
||||
},
|
||||
}))
|
||||
|
||||
vi.mock('../../../src/main/services/sync/sync-adapters', () => ({
|
||||
SyncAbortedError: class SyncAbortedError extends Error {},
|
||||
}))
|
||||
vi.mock('../../../src/main/services/sync/supabase-sync-remote', () => ({ SupabaseSyncRemote: class {} }))
|
||||
vi.mock('../../../src/main/services/sync/device-registration', () => ({
|
||||
checkInDesktopDevice: vi.fn(async () => ({ status: 'active', deviceId: 'dev-1' })),
|
||||
currentDeviceInfo: () => ({}),
|
||||
unregisterDesktopDevice: vi.fn(async () => undefined),
|
||||
}))
|
||||
vi.mock('../../../src/main/services/sync/sync-types', () => ({ realtimeTables: () => [] }))
|
||||
vi.mock('../../../src/main/services/sync/realtime-transport', () => ({ nodeRealtimeTransport: {} }))
|
||||
vi.mock('../../../src/main/services/sync/settings-sync', () => ({
|
||||
SETTINGS_ROW_ID: 'settings',
|
||||
SYNCED_CONFIG_KEYS: [],
|
||||
isApplyingRemoteSettings: () => false,
|
||||
}))
|
||||
vi.mock('../../../src/main/services/sync/audio-sync', () => ({ AUDIO_BUCKET: 'audio', listLocalAudioOwners: () => [] }))
|
||||
vi.mock('../../../src/main/windows/web-contents-hardening', () => ({ openExternalSafe: vi.fn(async () => true) }))
|
||||
vi.mock('../../../src/main/services/LicenseService', () => ({
|
||||
getLicenseService: () => ({ syncFromCloud: vi.fn(), resetToFree: vi.fn() }),
|
||||
}))
|
||||
vi.mock('../../../src/main/services/VoiceModeService', () => ({
|
||||
getVoiceModeService: () => ({ isActive: false, cancelSession: vi.fn() }),
|
||||
}))
|
||||
vi.mock('../../../src/main/services/MeetingModeService', () => ({
|
||||
getMeetingModeService: () => ({ isMeetingModeActive: () => false, stopRecording: vi.fn() }),
|
||||
}))
|
||||
vi.mock('../../../src/main/services/CaptionService', () => ({
|
||||
getCaptionService: () => ({ stop: vi.fn(async () => null) }),
|
||||
}))
|
||||
|
||||
vi.mock('@supabase/supabase-js', () => {
|
||||
const query = (): Record<string, unknown> => {
|
||||
const builder: Record<string, unknown> = {}
|
||||
for (const method of ['select', 'eq', 'order', 'limit']) builder[method] = () => builder
|
||||
builder.maybeSingle = async () => {
|
||||
if (h.state.tierGate) await h.state.tierGate
|
||||
return { data: null, error: null }
|
||||
}
|
||||
return builder
|
||||
}
|
||||
const channel = {
|
||||
on() {
|
||||
return channel
|
||||
},
|
||||
subscribe() {
|
||||
return channel
|
||||
},
|
||||
unsubscribe: async () => undefined,
|
||||
state: 'joined',
|
||||
}
|
||||
return {
|
||||
createClient: () => {
|
||||
const clientState = { session: null as null | { access_token: string; user: { id: string } } }
|
||||
spies.clients.push(clientState)
|
||||
return {
|
||||
auth: {
|
||||
refreshSession: async (...args: unknown[]) => {
|
||||
const result = (await spies.refreshSession!(...args)) as { data: { session: unknown } }
|
||||
if (result.data.session) clientState.session = result.data.session as typeof clientState.session
|
||||
return result
|
||||
},
|
||||
signOut: (...args: unknown[]) => spies.signOut!(...args),
|
||||
onAuthStateChange: (cb: (event: string, session: unknown) => void) => {
|
||||
h.state.authCallback = cb
|
||||
return { data: { subscription: { unsubscribe: () => undefined } } }
|
||||
},
|
||||
getSession: async () => ({ data: { session: clientState.session } }),
|
||||
stopAutoRefresh: vi.fn(async () => undefined),
|
||||
},
|
||||
from: () => query(),
|
||||
channel: () => channel,
|
||||
realtime: { setAuth: async () => undefined },
|
||||
functions: {
|
||||
invoke: vi.fn(async () => ({ data: { ok: true }, error: null })),
|
||||
},
|
||||
}
|
||||
},
|
||||
}
|
||||
})
|
||||
|
||||
import { getCloudSyncService, resetCloudSyncServiceForTests } from '../../../src/main/services/CloudSyncService'
|
||||
import { cloudSyncCredentials, createCloudSttGateway } from '../../../src/main/services/cloud/cloud-credentials'
|
||||
import { D3ROCloudDriver } from '../../../src/main/services/stt/drivers/D3ROCloudDriver'
|
||||
|
||||
function session() {
|
||||
return { access_token: 'at-user-1', refresh_token: 'rt-new', user: { id: 'user-1', email: 'u@example.test' } }
|
||||
}
|
||||
|
||||
describe('로그아웃 실패(retryable) 뒤 자격 증명 폐기', () => {
|
||||
beforeEach(() => {
|
||||
resetCloudSyncServiceForTests()
|
||||
vi.clearAllMocks()
|
||||
spies.clients.length = 0
|
||||
fs.rmSync(USER_DATA, { recursive: true, force: true })
|
||||
fs.mkdirSync(USER_DATA, { recursive: true })
|
||||
fs.writeFileSync(TOKEN_FILE, JSON.stringify({ v: 1, rt: 'rt-1', uid: 'user-1' }))
|
||||
h.state.currentUserId = 'user-1'
|
||||
h.state.tierGate = null
|
||||
spies.refreshSession = vi.fn(async () => ({ data: { session: session() }, error: null }))
|
||||
// auth-js: 네트워크 실패면 throw 하지 않고 { error } 를 돌려주며 클라이언트 세션을 지우지 않는다
|
||||
spies.signOut = vi.fn(async () => ({
|
||||
error: { name: 'AuthRetryableFetchError', status: 0, message: 'fetch failed' },
|
||||
}))
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
resetCloudSyncServiceForTests()
|
||||
fs.rmSync(USER_DATA, { recursive: true, force: true })
|
||||
})
|
||||
|
||||
it('로그인 중에는 토큰과 Edge Function 호출이 가능하다', async () => {
|
||||
const sync = getCloudSyncService()
|
||||
await sync.init()
|
||||
expect(cloudSyncCredentials.hasCredentials()).toBe(true)
|
||||
await expect(cloudSyncCredentials.accessToken()).resolves.toBe('at-user-1')
|
||||
const result = await cloudSyncCredentials.invoke('realtime-token', {})
|
||||
expect(result.error).toBeNull()
|
||||
})
|
||||
|
||||
it('signOut 이 { error } 를 돌려줘도 접근자는 모두 null / no session 이다', async () => {
|
||||
const sync = getCloudSyncService()
|
||||
await sync.init()
|
||||
// 옛 클라이언트는 여전히 세션을 들고 있다(auth-js 동작 재현)
|
||||
expect(spies.clients[0].session).not.toBeNull()
|
||||
|
||||
await sync.signOut()
|
||||
|
||||
expect(sync.isAuthenticated()).toBe(false)
|
||||
expect(cloudSyncCredentials.hasCredentials()).toBe(false)
|
||||
await expect(sync.getAccessToken()).resolves.toBeNull()
|
||||
await expect(cloudSyncCredentials.accessToken()).resolves.toBeNull()
|
||||
const invoked = await cloudSyncCredentials.invoke('realtime-token', {})
|
||||
expect(invoked.error?.message).toMatch(/session/i)
|
||||
const streamed = await cloudSyncCredentials.invokeStream('llm-proxy', {})
|
||||
expect(streamed.stream).toBeNull()
|
||||
expect(streamed.error).not.toBeNull()
|
||||
})
|
||||
|
||||
it('실패한 signOut 뒤 옛 클라이언트는 자동 갱신을 멈추고 새 클라이언트로 바뀐다', async () => {
|
||||
const sync = getCloudSyncService()
|
||||
await sync.init()
|
||||
await sync.signOut()
|
||||
expect(spies.clients).toHaveLength(2)
|
||||
expect(spies.clients[1].session).toBeNull()
|
||||
})
|
||||
|
||||
it('Cloud STT 드라이버는 로그아웃 뒤 업로드하지 않는다', async () => {
|
||||
const sync = getCloudSyncService()
|
||||
await sync.init()
|
||||
await sync.signOut()
|
||||
const fetchSpy = vi.spyOn(globalThis, 'fetch')
|
||||
const driver = new D3ROCloudDriver({
|
||||
...createCloudSttGateway(),
|
||||
getSupabaseUrl: () => 'https://example.supabase.co',
|
||||
getAnonKey: () => 'anon',
|
||||
})
|
||||
await expect(driver.transcribe(Buffer.alloc(3200))).rejects.toThrow()
|
||||
expect(fetchSpy).not.toHaveBeenCalled()
|
||||
fetchSpy.mockRestore()
|
||||
})
|
||||
})
|
||||
|
|
@ -0,0 +1,159 @@
|
|||
// 레드팀 r3-2: 다른 기기에서 재색인한 지식 문서가 이미 문서를 가진 데스크톱에도 반영된다.
|
||||
// - 원문이 바뀌었으면 청크를 교체하고(옛 임베딩 제거) 다시 색인한다
|
||||
// - 원문이 같으면(자기 push의 에코 등) 로컬 청크·임베딩을 건드리지 않는다
|
||||
// - 재업로드가 중간이면 잘린 청크로 덮지 않고 미뤘다가 완성본을 반영한다
|
||||
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { eq } from 'drizzle-orm'
|
||||
import { createTestDb } from '../../helpers/createTestDb'
|
||||
import { FakeSyncRemote } from '../../helpers/fakeSyncRemote'
|
||||
import { bindTestDatabase, unbindTestDatabase } from '../../../src/main/db'
|
||||
import { ragChunks, ragDocuments } from '../../../src/main/db/schema'
|
||||
import { initInMemoryConfig, resetInMemoryConfig } from '../../../src/main/services/ConfigService'
|
||||
import {
|
||||
getCustomInstructionService,
|
||||
resetCustomInstructionServiceForTests,
|
||||
} from '../../../src/main/services/CustomInstructionService'
|
||||
import { resetDictationTemplateServiceForTests } from '../../../src/main/services/DictationTemplateService'
|
||||
import { resetMeetingDocTemplateServiceForTests } from '../../../src/main/services/MeetingDocTemplateService'
|
||||
import { resetRAGServiceForTests } from '../../../src/main/services/RAGService'
|
||||
import { SyncEngine } from '../../../src/main/services/sync/SyncEngine'
|
||||
import { planRemoteDocument, sameChunks, usableChunks } from '../../../src/main/services/rag/remote-document'
|
||||
|
||||
const USER = '11111111-1111-4111-8111-111111111111'
|
||||
|
||||
let testDb: ReturnType<typeof createTestDb>
|
||||
let remote: FakeSyncRemote
|
||||
let engine: SyncEngine
|
||||
|
||||
beforeEach(() => {
|
||||
testDb = createTestDb()
|
||||
bindTestDatabase(testDb.db, USER)
|
||||
initInMemoryConfig()
|
||||
resetCustomInstructionServiceForTests()
|
||||
resetDictationTemplateServiceForTests()
|
||||
resetMeetingDocTemplateServiceForTests()
|
||||
resetRAGServiceForTests()
|
||||
getCustomInstructionService().initialize()
|
||||
// 임베딩 서버(Ollama)는 없다 — 원문만 저장되는 경로를 탄다.
|
||||
vi.spyOn(globalThis, 'fetch').mockRejectedValue(new TypeError('fetch failed'))
|
||||
remote = new FakeSyncRemote(USER)
|
||||
engine = new SyncEngine({ remote, userId: USER })
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks()
|
||||
engine.dispose()
|
||||
unbindTestDatabase()
|
||||
resetInMemoryConfig()
|
||||
resetRAGServiceForTests()
|
||||
testDb.close()
|
||||
})
|
||||
|
||||
function localChunks(id: string): Array<{ content: string; embedding: string }> {
|
||||
return testDb.db
|
||||
.select()
|
||||
.from(ragChunks)
|
||||
.where(eq(ragChunks.documentId, id))
|
||||
.all()
|
||||
.sort((a, b) => a.chunkIndex - b.chunkIndex)
|
||||
.map((c) => ({ content: c.content, embedding: c.embedding }))
|
||||
}
|
||||
|
||||
function markEmbedded(id: string): void {
|
||||
testDb.db.update(ragChunks).set({ embedding: '[1,2]' }).where(eq(ragChunks.documentId, id)).run()
|
||||
testDb.db.update(ragDocuments).set({ indexed: true, indexedAt: 1 }).where(eq(ragDocuments.id, id)).run()
|
||||
}
|
||||
|
||||
/** 다른 데스크톱(A)의 재색인 push를 흉내: 행 upsert(updated_at 이동) → 청크 전부 교체 */
|
||||
function remoteReindex(id: string, chunks: string[], uploaded = chunks.length): void {
|
||||
remote.mobileUpdate('knowledge_documents', id, { chunk_count: chunks.length })
|
||||
const rows = remote.rows('knowledge_chunks')
|
||||
for (let i = rows.length - 1; i >= 0; i--) if (rows[i].document_id === id) rows.splice(i, 1)
|
||||
chunks.slice(0, uploaded).forEach((content, chunk_index) =>
|
||||
rows.push({ id: crypto.randomUUID(), document_id: id, chunk_index, content })
|
||||
)
|
||||
}
|
||||
|
||||
function uploadRest(id: string, chunks: string[], from: number): void {
|
||||
chunks.slice(from).forEach((content, offset) =>
|
||||
remote.rows('knowledge_chunks').push({ id: crypto.randomUUID(), document_id: id, chunk_index: from + offset, content })
|
||||
)
|
||||
}
|
||||
|
||||
async function seedSyncedDocument(chunks: string[]): Promise<string> {
|
||||
const id = crypto.randomUUID()
|
||||
remote.mobileInsert('knowledge_documents', { id, title: 'Doc', file_name: 'doc.md', file_type: 'md', chunk_count: chunks.length })
|
||||
chunks.forEach((content, chunk_index) =>
|
||||
remote.rows('knowledge_chunks').push({ id: crypto.randomUUID(), document_id: id, chunk_index, content })
|
||||
)
|
||||
await engine.runFullSync()
|
||||
expect(localChunks(id).map((c) => c.content)).toEqual(chunks)
|
||||
// 받은 직후 시작된 색인 시도(임베딩 서버 없음)가 끝난 뒤에 "이미 색인됨" 상태를 만든다
|
||||
for (let i = 0; i < 20; i++) await new Promise((r) => setTimeout(r, 0))
|
||||
markEmbedded(id)
|
||||
return id
|
||||
}
|
||||
|
||||
describe('재색인된 지식 문서 동기화', () => {
|
||||
it('다른 기기에서 원문이 바뀌면 이미 있는 문서의 청크를 교체하고 옛 임베딩을 버린다', async () => {
|
||||
const id = await seedSyncedDocument(['old intro', 'old body'])
|
||||
|
||||
remoteReindex(id, ['new intro', 'new body', 'new appendix'])
|
||||
await engine.pull()
|
||||
|
||||
const chunks = localChunks(id)
|
||||
expect(chunks.map((c) => c.content)).toEqual(['new intro', 'new body', 'new appendix'])
|
||||
// 옛 벡터가 새 원문에 붙어 검색되지 않는다(임베딩 서버가 없어 아직 비어 있다)
|
||||
expect(chunks.every((c) => c.embedding === '')).toBe(true)
|
||||
const doc = testDb.db.select().from(ragDocuments).where(eq(ragDocuments.id, id)).get()
|
||||
expect(doc?.chunkCount).toBe(3)
|
||||
expect(doc?.indexed).toBe(false)
|
||||
})
|
||||
|
||||
it('원문이 같으면(에코·메타데이터만 변경) 로컬 청크와 임베딩을 그대로 둔다', async () => {
|
||||
const id = await seedSyncedDocument(['same a', 'same b'])
|
||||
|
||||
remote.mobileUpdate('knowledge_documents', id, { indexed: true })
|
||||
await engine.pull()
|
||||
|
||||
expect(localChunks(id)).toEqual([
|
||||
{ content: 'same a', embedding: '[1,2]' },
|
||||
{ content: 'same b', embedding: '[1,2]' },
|
||||
])
|
||||
expect(testDb.db.select().from(ragDocuments).where(eq(ragDocuments.id, id)).get()?.indexed).toBe(true)
|
||||
})
|
||||
|
||||
it('재업로드가 중간이면 잘린 청크로 덮지 않고, 완성되면 반영한다', async () => {
|
||||
const id = await seedSyncedDocument(['v1 a', 'v1 b'])
|
||||
const next = ['v2 a', 'v2 b', 'v2 c']
|
||||
|
||||
remoteReindex(id, next, 1)
|
||||
await engine.pull()
|
||||
expect(localChunks(id).map((c) => c.content)).toEqual(['v1 a', 'v1 b'])
|
||||
|
||||
uploadRest(id, next, 1)
|
||||
await engine.pull()
|
||||
expect(localChunks(id).map((c) => c.content)).toEqual(next)
|
||||
})
|
||||
})
|
||||
|
||||
describe('planRemoteDocument', () => {
|
||||
it('로컬에 없으면 insert, 같으면 skip, 다르면 replace', () => {
|
||||
expect(planRemoteDocument(null, ['a', ' ', 'b'])).toEqual({ kind: 'insert', chunks: ['a', 'b'] })
|
||||
expect(planRemoteDocument(['a', 'b'], ['a', 'b'])).toEqual({ kind: 'skip' })
|
||||
expect(planRemoteDocument(['a', 'b'], ['a', 'c'])).toEqual({ kind: 'replace', chunks: ['a', 'c'] })
|
||||
expect(planRemoteDocument(['a'], ['a', 'b'])).toEqual({ kind: 'replace', chunks: ['a', 'b'] })
|
||||
})
|
||||
|
||||
it('받은 청크가 전부 비어 있으면 로컬을 비우지 않는다', () => {
|
||||
expect(planRemoteDocument(['a'], ['', ' '])).toEqual({ kind: 'skip' })
|
||||
expect(planRemoteDocument(null, [])).toEqual({ kind: 'skip' })
|
||||
})
|
||||
|
||||
it('빈 청크는 비교에서 빠진다', () => {
|
||||
expect(usableChunks(['a', '', ' b '])).toEqual(['a', ' b '])
|
||||
expect(planRemoteDocument(['a', ''], ['a'])).toEqual({ kind: 'skip' })
|
||||
expect(sameChunks(['a'], ['a', 'b'])).toBe(false)
|
||||
})
|
||||
})
|
||||
269
apps/desktop/tests/main/sync/sync-redteam-r3-0.test.ts
Normal file
269
apps/desktop/tests/main/sync/sync-redteam-r3-0.test.ts
Normal file
|
|
@ -0,0 +1,269 @@
|
|||
// 레드팀 r3-0: push 전 tombstone 읽기 실패 시 upsert 보류(fail closed),
|
||||
// 서버에 없는 활성 명령이 설정 항목을 끝없는 재시도에 묶어 설정 pull을 막던 문제.
|
||||
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { eq } from 'drizzle-orm'
|
||||
import { createTestDb } from '../../helpers/createTestDb'
|
||||
import { FakeSyncRemote } from '../../helpers/fakeSyncRemote'
|
||||
import { bindTestDatabase, unbindTestDatabase } from '../../../src/main/db'
|
||||
import { history } from '../../../src/main/db/schema'
|
||||
import { configGet, configSet, initInMemoryConfig, resetInMemoryConfig } from '../../../src/main/services/ConfigService'
|
||||
import {
|
||||
getCustomInstructionService,
|
||||
resetCustomInstructionServiceForTests,
|
||||
} from '../../../src/main/services/CustomInstructionService'
|
||||
import { resetDictationTemplateServiceForTests } from '../../../src/main/services/DictationTemplateService'
|
||||
import { resetMeetingDocTemplateServiceForTests } from '../../../src/main/services/MeetingDocTemplateService'
|
||||
import { resetRAGServiceForTests } from '../../../src/main/services/RAGService'
|
||||
import { SyncEngine } from '../../../src/main/services/sync/SyncEngine'
|
||||
import { enqueueChange, pendingOps } from '../../../src/main/services/sync/sync-outbox'
|
||||
import {
|
||||
SyncRemoteError,
|
||||
type RemotePageRequest,
|
||||
type RemoteRow,
|
||||
} from '../../../src/main/services/sync/sync-types'
|
||||
import { pushableEntries } from '../../../src/main/services/sync/push-gate-policy'
|
||||
import {
|
||||
isActiveInstructionMissing,
|
||||
planActiveInstructionStep,
|
||||
shouldResyncSettingsAfterInstructionPush,
|
||||
} from '../../../src/main/services/sync/active-instruction-push-policy'
|
||||
import type { EmbeddingPort } from '../../../src/main/services/rag/embedding-port'
|
||||
|
||||
const USER = '11111111-1111-4111-8111-111111111111'
|
||||
const U = '22222222-2222-4222-8222-222222222222'
|
||||
|
||||
/**
|
||||
* 운영 서버처럼 INSERT 때 updated_at을 서버 시각으로 새로 찍고(0037 stamp_sync_timestamp_v1),
|
||||
* sync_tombstones 읽기를 지정한 횟수만큼 일시 오류로 실패시키는 원격.
|
||||
*/
|
||||
class ServerStampingRemote extends FakeSyncRemote {
|
||||
failTombstoneReads = 0
|
||||
|
||||
override async fetchPage(request: RemotePageRequest): Promise<RemoteRow[]> {
|
||||
if (request.table === 'sync_tombstones' && this.failTombstoneReads > 0) {
|
||||
this.failTombstoneReads--
|
||||
throw new SyncRemoteError('canceling statement due to statement timeout', '57014', true)
|
||||
}
|
||||
return super.fetchPage(request)
|
||||
}
|
||||
|
||||
override async upsert(table: string, rows: RemoteRow[], onConflict = 'id'): Promise<void> {
|
||||
const existing = new Set(this.rows(table).map((r) => String(r.id)))
|
||||
await super.upsert(table, rows, onConflict)
|
||||
for (const row of this.rows(table)) {
|
||||
if (!existing.has(String(row.id))) row.updated_at = this.now()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const offlineEmbedder: EmbeddingPort = {
|
||||
model: 'test-embed',
|
||||
ensureModel: () => Promise.reject(new Error('no embedding server in tests')),
|
||||
embed: () => Promise.reject(new Error('no embedding server in tests')),
|
||||
}
|
||||
|
||||
let testDb: ReturnType<typeof createTestDb>
|
||||
let remote: ServerStampingRemote
|
||||
let engine: SyncEngine
|
||||
let clock: number
|
||||
|
||||
beforeEach(() => {
|
||||
testDb = createTestDb()
|
||||
bindTestDatabase(testDb.db, USER)
|
||||
initInMemoryConfig()
|
||||
resetCustomInstructionServiceForTests()
|
||||
resetDictationTemplateServiceForTests()
|
||||
resetMeetingDocTemplateServiceForTests()
|
||||
resetRAGServiceForTests({ embedder: offlineEmbedder, notify: () => undefined, yieldMs: 0 })
|
||||
getCustomInstructionService().initialize()
|
||||
remote = new ServerStampingRemote(USER)
|
||||
clock = Date.parse('2026-09-28T00:00:00.000Z')
|
||||
engine = new SyncEngine({ remote, userId: USER, now: () => clock })
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks()
|
||||
engine.dispose()
|
||||
resetRAGServiceForTests()
|
||||
unbindTestDatabase()
|
||||
resetInMemoryConfig()
|
||||
testDb.close()
|
||||
})
|
||||
|
||||
function historyRow(id: string, text: string): Record<string, unknown> {
|
||||
return { id, original_text: text, duration: 1, mode: 'dictation', status: 'completed' }
|
||||
}
|
||||
|
||||
function localHistoryIds(): string[] {
|
||||
return testDb.db.select({ id: history.id }).from(history).all().map((r) => r.id).sort()
|
||||
}
|
||||
|
||||
describe('push 전 tombstone 읽기 실패 → upsert 보류(fail closed)', () => {
|
||||
it('폰에서 지운 행의 대기 upsert가 서버에 행을 되살리지 않고, 다음 pull에서 삭제가 반영된다', async () => {
|
||||
const x = crypto.randomUUID()
|
||||
remote.mobileInsert('history', historyRow(x, 'will be deleted on phone'))
|
||||
await engine.runFullSync()
|
||||
expect(localHistoryIds()).toEqual([x])
|
||||
|
||||
// 데스크톱의 대기 편집(즐겨찾기 등) + 폰의 삭제(T1)
|
||||
enqueueChange('history', x, 'upsert', clock)
|
||||
remote.mobileDelete('history', x)
|
||||
|
||||
remote.failTombstoneReads = 1
|
||||
const flushed = await engine.flush()
|
||||
expect(flushed.errors.some((e) => e.startsWith('tombstones:'))).toBe(true)
|
||||
expect(flushed.pushed).toBe(0)
|
||||
expect(remote.find('history', x)).toBeUndefined()
|
||||
expect(pendingOps('history').get(x)).toBe('upsert')
|
||||
|
||||
await engine.pull()
|
||||
expect(localHistoryIds()).toEqual([])
|
||||
expect(pendingOps('history').has(x)).toBe(false)
|
||||
expect(remote.find('history', x)).toBeUndefined()
|
||||
})
|
||||
|
||||
it('tombstone 읽기가 실패해도 삭제는 보낸다', async () => {
|
||||
const x = crypto.randomUUID()
|
||||
const y = crypto.randomUUID()
|
||||
remote.mobileInsert('history', historyRow(x, 'edit me'))
|
||||
remote.mobileInsert('history', historyRow(y, 'delete me'))
|
||||
await engine.runFullSync()
|
||||
|
||||
enqueueChange('history', x, 'upsert', clock)
|
||||
testDb.db.delete(history).where(eq(history.id, y)).run()
|
||||
enqueueChange('history', y, 'delete', clock)
|
||||
remote.failTombstoneReads = 1
|
||||
const flushed = await engine.flush()
|
||||
|
||||
expect(remote.find('history', y)).toBeUndefined()
|
||||
expect(pendingOps('history').has(y)).toBe(false)
|
||||
expect(pendingOps('history').get(x)).toBe('upsert')
|
||||
expect(flushed.pushed).toBe(1)
|
||||
})
|
||||
|
||||
it('runFullSync에서 tombstone 읽기가 실패하면 최초 대조·upsert를 미루고 다음 flush에서 이어 간다', async () => {
|
||||
const local = crypto.randomUUID()
|
||||
const at = clock
|
||||
testDb.db.insert(history).values({ id: local, originalText: 'offline note', duration: 1, createdAt: at, updatedAt: at }).run()
|
||||
|
||||
remote.failTombstoneReads = 1
|
||||
await engine.runFullSync()
|
||||
expect(remote.find('history', local)).toBeUndefined()
|
||||
|
||||
await engine.flush()
|
||||
expect(remote.find('history', local)?.original_text).toBe('offline note')
|
||||
})
|
||||
})
|
||||
|
||||
describe('서버에 없는 활성 명령과 설정 동기화', () => {
|
||||
function rejectInstructionUploads(error: SyncRemoteError | null): void {
|
||||
remote.rejectRow = (table) => (table === 'custom_instructions' ? error : null)
|
||||
}
|
||||
|
||||
function seedRemoteSettings(): void {
|
||||
remote.mobileInsert('user_settings', {
|
||||
locale: 'ko',
|
||||
theme_mode: 'system',
|
||||
auto_polish_enabled: true,
|
||||
revision: 1,
|
||||
active_instruction_id: null,
|
||||
})
|
||||
}
|
||||
|
||||
it('올라가지 못한(보관된) 명령이 활성이어도 설정 항목은 완료되고, 폰의 설정 변경을 계속 받는다', async () => {
|
||||
seedRemoteSettings()
|
||||
const command = getCustomInstructionService().create({ name: 'Local only', description: '', prompt: 'Keep it local' })
|
||||
rejectInstructionUploads(new SyncRemoteError('value too long for type character varying(4000)', '22001', false))
|
||||
await engine.runFullSync()
|
||||
expect(pendingOps('custom_instructions').get(command.id)).toBe('upsert')
|
||||
|
||||
// 서버에 없는 명령을 활성으로 고르고 테마를 바꾼다(CloudSyncService가 설정 항목을 넣는다)
|
||||
configSet('activeInstructionId', command.id)
|
||||
configSet('theme', 'dark')
|
||||
enqueueChange('user_settings', 'self', 'upsert', clock)
|
||||
const flushed = await engine.flush()
|
||||
expect(flushed.errors.filter((e) => e.startsWith('user_settings'))).toEqual([])
|
||||
expect(pendingOps('user_settings').has('self')).toBe(false)
|
||||
expect(remote.rows('user_settings')[0].theme_mode).toBe('dark')
|
||||
expect(configGet('activeInstructionId')).toBe(command.id)
|
||||
expect(remote.calls).not.toContain('rpc:set_active_custom_instruction')
|
||||
|
||||
const settings = remote.rows('user_settings')[0]
|
||||
Object.assign(settings, { theme_mode: 'light', revision: Number(settings.revision) + 1, updated_at: remote.now() })
|
||||
await engine.pull()
|
||||
expect(configGet('theme')).toBe('light')
|
||||
})
|
||||
|
||||
it('outbox에 없는데 서버가 모르는 명령(P0002)은 설정 항목을 재시도에 묶지 않는다', async () => {
|
||||
seedRemoteSettings()
|
||||
const command = getCustomInstructionService().create({ name: 'Gone', description: '', prompt: 'Vanished on server' })
|
||||
await engine.runFullSync()
|
||||
// 서버에서 tombstone 없이 사라진 경우(보존 기간 정리 등)
|
||||
const rows = remote.rows('custom_instructions')
|
||||
rows.splice(rows.findIndex((r) => r.id === command.id), 1)
|
||||
|
||||
configSet('activeInstructionId', command.id)
|
||||
enqueueChange('user_settings', 'self', 'upsert', clock)
|
||||
const flushed = await engine.flush()
|
||||
expect(remote.calls).toContain('rpc:set_active_custom_instruction')
|
||||
expect(flushed.errors.filter((e) => e.startsWith('user_settings'))).toEqual([])
|
||||
expect(pendingOps('user_settings').has('self')).toBe(false)
|
||||
})
|
||||
|
||||
it('미룬 활성 명령이 나중에 서버에 올라가면 설정을 다시 올려 서버 활성 명령을 맞춘다', async () => {
|
||||
seedRemoteSettings()
|
||||
const command = getCustomInstructionService().create({ name: 'Late', description: '', prompt: 'Arrives later' })
|
||||
rejectInstructionUploads(new SyncRemoteError('canceling statement due to statement timeout', '57014', true))
|
||||
await engine.runFullSync()
|
||||
configSet('activeInstructionId', command.id)
|
||||
enqueueChange('user_settings', 'self', 'upsert', clock)
|
||||
await engine.flush()
|
||||
expect(pendingOps('user_settings').has('self')).toBe(false)
|
||||
expect(remote.rows('user_settings')[0].active_instruction_id).toBeNull()
|
||||
expect(remote.calls).not.toContain('rpc:set_active_custom_instruction')
|
||||
|
||||
rejectInstructionUploads(null)
|
||||
clock += 60 * 60_000
|
||||
await engine.flush()
|
||||
expect(remote.find('custom_instructions', command.id)).toBeDefined()
|
||||
expect(pendingOps('user_settings').has('self')).toBe(true)
|
||||
|
||||
await engine.flush()
|
||||
expect(remote.rows('user_settings')[0].active_instruction_id).toBe(command.id)
|
||||
expect(pendingOps('user_settings').has('self')).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe('push-gate-policy', () => {
|
||||
const entries = [
|
||||
{ op: 'upsert' as const, rowId: 'a' },
|
||||
{ op: 'delete' as const, rowId: 'b' },
|
||||
]
|
||||
|
||||
it('tombstone을 반영했으면 전부, 실패했으면 삭제만 push한다', () => {
|
||||
expect(pushableEntries(entries, { tombstonesSynced: true })).toEqual(entries)
|
||||
expect(pushableEntries(entries, { tombstonesSynced: false })).toEqual([{ op: 'delete', rowId: 'b' }])
|
||||
})
|
||||
})
|
||||
|
||||
describe('active-instruction-push-policy', () => {
|
||||
it('서버에 아직 없는 명령은 미루고, 나머지는 RPC로 보낸다', () => {
|
||||
expect(planActiveInstructionStep(null, new Set([U]))).toEqual({ kind: 'none' })
|
||||
expect(planActiveInstructionStep({ instructionId: U }, new Set([U]))).toEqual({ kind: 'defer', instructionId: U })
|
||||
expect(planActiveInstructionStep({ instructionId: U }, new Set())).toEqual({ kind: 'rpc', instructionId: U })
|
||||
expect(planActiveInstructionStep({ instructionId: null }, new Set([U]))).toEqual({ kind: 'rpc', instructionId: null })
|
||||
})
|
||||
|
||||
it('P0002만 "서버에 명령 없음"으로 본다', () => {
|
||||
expect(isActiveInstructionMissing(new SyncRemoteError('instruction_not_found', 'P0002', true))).toBe(true)
|
||||
expect(isActiveInstructionMissing(new SyncRemoteError('fetch failed', 'network', true))).toBe(false)
|
||||
})
|
||||
|
||||
it('미룬 명령이 올라갔을 때만 설정을 다시 올린다', () => {
|
||||
expect(shouldResyncSettingsAfterInstructionPush(U, [U])).toBe(true)
|
||||
expect(shouldResyncSettingsAfterInstructionPush(U, ['other'])).toBe(false)
|
||||
expect(shouldResyncSettingsAfterInstructionPush('', [U])).toBe(false)
|
||||
expect(shouldResyncSettingsAfterInstructionPush(null, [U])).toBe(false)
|
||||
})
|
||||
})
|
||||
Loading…
Add table
Add a link
Reference in a new issue