d3ro-voice/packages/core/src/template-field-policy.ts
Yun Chan ba9ef9741e 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.
2026-09-28 20:45:52 +09:00

60 lines
2.8 KiB
TypeScript

// packages/core/src/template-field-policy.ts
// 받아쓰기 템플릿 필드 id 규칙 — 편집 UI(TemplateSection)와 도메인 서비스(DictationTemplateService)가 함께 쓰는 정본.
//
// 필드 id는 세션에서 받아쓴 값을 담는 키(fieldValues[id])이자 출력 포맷의 {{id}} 자리표시자 키다.
// 그래서 한 템플릿 안에서 비어 있지 않고 서로 달라야 한다. 겹치면 나중 필드 값이 앞 필드 값을 덮어써
// 출력에서 앞 값이 조용히 사라진다.
import type { TemplateField } from './types'
/** 필드 하나의 id 문제. 공백뿐인 id는 비어 있는 것으로 본다. */
export type TemplateFieldIdProblem = 'empty' | 'duplicate'
/** 새 필드 id 접두사 — `field1`, `field2`, ... */
export const TEMPLATE_FIELD_ID_PREFIX = 'field'
const GENERATED_ID = new RegExp(`^${TEMPLATE_FIELD_ID_PREFIX}(\\d+)$`)
/**
* 필드마다 id 문제를 돌려준다(입력과 같은 길이, 문제 없으면 null).
* 같은 id를 가진 필드는 모두 'duplicate'로 표시한다 — 편집 UI가 겹치는 행을 전부 강조할 수 있게.
*/
export function templateFieldIdProblems(
fields: ReadonlyArray<Pick<TemplateField, 'id'>>,
): Array<TemplateFieldIdProblem | null> {
const counts = new Map<string, number>()
for (const field of fields) counts.set(field.id, (counts.get(field.id) ?? 0) + 1)
return fields.map((field) => {
if (field.id.trim().length === 0) return 'empty'
return (counts.get(field.id) ?? 0) > 1 ? 'duplicate' : null
})
}
/** 첫 번째 id 문제와 그 위치. 모든 id가 유효하면 null. */
export function findTemplateFieldIdProblem(
fields: ReadonlyArray<Pick<TemplateField, 'id'>>,
): { index: number; id: string; problem: TemplateFieldIdProblem } | null {
const problems = templateFieldIdProblems(fields)
const index = problems.findIndex((problem) => problem !== null)
if (index === -1) return null
return { index, id: fields[index].id, problem: problems[index] as TemplateFieldIdProblem }
}
/**
* 기존 필드와 겹치지 않는 새 필드 번호 — `fieldN` 형식 id 중 가장 큰 N + 1.
* (사용자가 이름을 `field7`로 바꾼 필드도 세므로 `field7`은 다시 나오지 않는다.)
* 필드 개수에서 번호를 만들면 중간 필드를 지운 뒤 이미 있는 id가 다시 나온다.
*/
export function nextTemplateFieldNumber(fields: ReadonlyArray<Pick<TemplateField, 'id'>>): number {
let max = 0
for (const field of fields) {
const match = GENERATED_ID.exec(field.id)
if (match) max = Math.max(max, Number(match[1]))
}
return max + 1
}
/** 기존 필드와 겹치지 않는 새 필드 id. */
export function nextTemplateFieldId(fields: ReadonlyArray<Pick<TemplateField, 'id'>>): string {
return `${TEMPLATE_FIELD_ID_PREFIX}${nextTemplateFieldNumber(fields)}`
}