Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,12 @@ jobs:
- name: Verify bounded watcher states and rejected source mutations
run: python3 -B scripts/verify_alden_realtime_state.py

- name: Test declared and body notation normalization
run: python3 -B -m unittest tests.test_alden_time_normalization tests.test_alden_quantity_normalization tests.test_alden_normalization_evidence tests.test_alden_body_normalization tests.test_alden_body_details -v

- name: Verify body notation source mutation sensitivity
run: python3 -B scripts/verify_alden_body_normalization.py

# The reader enforces a process peak RSS budget. Earlier test modules
# must not consume the memory allowance of this independent CLI process.
- name: Test bounded affinity in a fresh process
Expand Down
1 change: 1 addition & 0 deletions desktop/src-tauri/src/resource_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ pub const DATA_FILES: &[&str] = &[
"scripts/alden_link_fetch.py",
"scripts/alden_graph_quality.py",
"scripts/alden_normalization_evidence.py",
"scripts/alden_body_normalization.py",
"scripts/alden_time_normalization.py",
"scripts/alden_quantity_normalization.py",
"scripts/alden_content_evidence.py",
Expand Down
1 change: 1 addition & 0 deletions desktop/src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@
"bundle-resources/scripts/alden_link_fetch.py": "scripts/alden_link_fetch.py",
"bundle-resources/scripts/alden_graph_quality.py": "scripts/alden_graph_quality.py",
"bundle-resources/scripts/alden_normalization_evidence.py": "scripts/alden_normalization_evidence.py",
"bundle-resources/scripts/alden_body_normalization.py": "scripts/alden_body_normalization.py",
"bundle-resources/scripts/alden_time_normalization.py": "scripts/alden_time_normalization.py",
"bundle-resources/scripts/alden_quantity_normalization.py": "scripts/alden_quantity_normalization.py",
"bundle-resources/scripts/alden_content_evidence.py": "scripts/alden_content_evidence.py",
Expand Down
87 changes: 87 additions & 0 deletions desktop/src/__tests__/body-notation.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// @vitest-environment happy-dom
import { webcrypto } from 'node:crypto';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import fixture from './fixtures/body-notation.json';
import { verifiedBodyNotation, renderBodyNotation } from '../knowledge/body-notation';
import { parseKnowledgeGraph } from '../knowledge/graph-model';
import { renderNodeDetails, setNodeDetailsVisible } from '../knowledge/node-details';

const data = () => structuredClone(fixture);
const graph = () => parseKnowledgeGraph({ nodes: [{ id: 'record-a', label: '자료', category: 'document', source_version: 'v1', source_target: 'target-a' }], edges: [] });
beforeEach(() => {
vi.stubGlobal('crypto', webcrypto);
document.body.innerHTML = '<dialog id="knowledge-note-pane" open><div id="knowledge-node-body"></div></dialog>';
const pane = document.querySelector('dialog')!;
pane.close = () => { pane.removeAttribute('open'); };
});
afterEach(() => vi.unstubAllGlobals());

describe('exact source body notation boundary', () => {
it('accepts the actual Python parser fixture, including emoji, exact integers and percent points', async () => {
const result = await verifiedBodyNotation(graph().nodes[0], data());
expect(result?.items).toHaveLength(8);
expect(result?.items.map(item => item.value)).toEqual(expect.arrayContaining(['2026-02-23', '50000000 원', '-5 명', '5 %p', '8000 g ~ 3000 g · 원문 순서', '의미 미확정', '9007199254740993 명']));
});
it.each([
['body bytes', (v: any) => { v.body = v.body.replace('기한', '수정'); }],
['document', (v: any) => { v.body_notation.binding.document_id = 'other'; }],
['version', (v: any) => { v.body_notation.binding.version = 'v2'; }],
['target', (v: any) => { v.body_notation.binding.target_id = 'other'; }],
['archive', (v: any) => { v.body_notation.binding.raw_sha256 = 'b'.repeat(64); }],
['body digest', (v: any) => { v.body_notation.binding.body_sha256 = v.body_notation.analysis.body_sha256 = 'b'.repeat(64); }],
['excerpt scope', (v: any) => { v.body_notation.binding.body_scope = 'detail_body_excerpt'; }],
['raw quote', (v: any) => { v.body_notation.analysis.candidates[0].raw = '<img src=x>'; }],
['UTF16 offset', (v: any) => { v.body_notation.analysis.candidates[0].utf16_start -= 1; }],
['codepoint offset', (v: any) => { v.body_notation.analysis.candidates[0].start -= 1; }],
['invalid offset', (v: any) => { v.body_notation.analysis.candidates[0].start = -1; }],
['overlapping spans', (v: any) => { v.body_notation.analysis.candidates.push(v.body_notation.analysis.candidates[0]); }],
['unknown unit', (v: any) => { v.body_notation.analysis.candidates[1].canonical_unit = 'KRW'; }],
['invented dimension', (v: any) => { v.body_notation.analysis.candidates[1].dimension = '__proto__'; }],
['fact claim', (v: any) => { v.body_notation.analysis.semantic_validity = 'verified'; }],
['incomplete state', (v: any) => { v.body_notation.analysis.state = 'supported_notation_scan_complete'; v.body_notation.analysis.truncated = true; }],
['excess candidates', (v: any) => { v.body_notation.analysis.candidates = Array(65).fill(v.body_notation.analysis.candidates[0]); }],
])('rejects a mismatched %s without altering the body', async (_name, mutate) => {
const value = data(); mutate(value);
expect(await verifiedBodyNotation(graph().nodes[0], value)).toBeNull();
});
it('supports canonical IDs after unified graph ID rewriting', async () => {
const node = { ...graph().nodes[0], id: 'scoped:record-a', canonicalId: 'record-a' };
expect(await verifiedBodyNotation(node, { ...data(), node_id: node.id })).not.toBeNull();
});
it('leaves the source readable if WebCrypto is absent or rejects', async () => {
vi.stubGlobal('crypto', {});
renderNodeDetails(graph(), graph().nodes[0], { ok: true, details: data() });
await vi.waitFor(() => expect(document.querySelector('.knowledge-body-notation')).toBeNull());
expect(document.getElementById('knowledge-node-body')?.textContent).toBe(fixture.body);
vi.stubGlobal('crypto', { subtle: { digest: () => Promise.reject(new Error('unavailable')) } });
renderBodyNotation(document.getElementById('knowledge-node-body')!, graph().nodes[0], data());
await vi.waitFor(() => expect(document.querySelector('.knowledge-body-notation')).toBeNull());
});
it('uses a collapsed disclosure and literal source quotes without HTML execution', async () => {
renderNodeDetails(graph(), graph().nodes[0], { ok: true, details: data() });
await vi.waitFor(() => expect(document.querySelector<HTMLDetailsElement>('.knowledge-body-notation')?.hidden).toBe(false));
const section = document.querySelector<HTMLDetailsElement>('.knowledge-body-notation')!;
expect(section.open).toBe(false);
expect(section.querySelectorAll('mark')).toHaveLength(8);
expect(document.querySelector('img')).toBeNull();
expect(section.textContent).toContain('현재 유효성');
expect(document.querySelector('#knowledge-node-facts')).toBeNull();
});
it('discloses excerpt and candidate budgets', async () => {
const value = data(); value.truncated = true; value.body_notation.binding.body_scope = 'detail_body_excerpt';
value.body_notation.analysis.truncated = true; value.body_notation.analysis.state = 'bounded_candidates';
renderBodyNotation(document.getElementById('knowledge-node-body')!, graph().nodes[0], value);
await vi.waitFor(() => expect(document.querySelector('.knowledge-body-notation')?.textContent).toContain('처리 한도'));
expect(document.querySelector('.knowledge-body-notation')?.textContent).toContain('발췌 범위');
});
it.each(['new render', 'close'])('discards a delayed digest after %s', async action => {
let finish!: (buffer: ArrayBuffer) => void;
vi.stubGlobal('crypto', { subtle: { digest: () => new Promise<ArrayBuffer>(resolve => { finish = resolve; }) } });
renderNodeDetails(graph(), graph().nodes[0], { ok: true, details: data() });
if (action === 'new render') renderNodeDetails(graph(), graph().nodes[0], { ok: false });
else setNodeDetailsVisible(false);
finish(await webcrypto.subtle.digest('SHA-256', new TextEncoder().encode(fixture.body)));
await new Promise(resolve => setTimeout(resolve, 0));
expect(document.querySelector('.knowledge-body-notation')).toBeNull();
});
});
161 changes: 161 additions & 0 deletions desktop/src/__tests__/fixtures/body-notation.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
{
"node_id": "record-a",
"basis": "source_record",
"target_id": "target-a",
"version": "v1",
"raw_sha256": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
"body": "😀 기한 2026년 2월 23일, 연봉 5천만원, 수량 −5명. 비율 5%p, 범위 8~3kg. 연도 26년 2월 23일. 단위 없는 5천. 큰 수 9007199254740993명. <img src=x onerror=alert(1)>",
"truncated": false,
"body_notation": {
"binding": {
"basis": "verified_source_record_detail",
"document_id": "record-a",
"target_id": "target-a",
"version": "v1",
"raw_sha256": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
"body_sha256": "3bd423a018cc59c3bc022869685c893d5a120b898774622ee622a7e5e33a3f83",
"body_scope": "detail_body"
},
"analysis": {
"policy": "body-notation-candidates-v1",
"state": "supported_notation_scan_complete",
"body_sha256": "3bd423a018cc59c3bc022869685c893d5a120b898774622ee622a7e5e33a3f83",
"body_characters": 130,
"offset_basis": "exact_supplied_unicode_codepoints",
"semantic_validity": "not_assessed",
"client_offset_basis": "exact_supplied_utf16_code_units",
"source_authorization": "caller_must_bind_permitted_current_source_version",
"candidates": [
{
"kind": "time",
"start": 5,
"end": 17,
"raw": "2026년 2월 23일",
"status": "normalized",
"precision": "day",
"canonical": "2026-02-23",
"basis": "date_only",
"reason": null,
"utf16_start": 6,
"utf16_end": 18
},
{
"start": 22,
"end": 26,
"raw": "5천만원",
"source_unit": "원",
"source_sign": "unsigned",
"kind": "quantity",
"status": "normalized",
"canonical_value": "50000000",
"canonical_unit": "원",
"dimension": "money_won_unspecified",
"reason": null,
"utf16_start": 23,
"utf16_end": 27
},
{
"start": 31,
"end": 34,
"raw": "−5명",
"source_unit": "명",
"source_sign": "negative",
"kind": "quantity",
"status": "normalized",
"canonical_value": "-5",
"canonical_unit": "명",
"dimension": "person_count",
"reason": null,
"utf16_start": 32,
"utf16_end": 35
},
{
"start": 39,
"end": 42,
"raw": "5%p",
"source_unit": "%p",
"source_sign": "unsigned",
"kind": "quantity",
"status": "normalized",
"canonical_value": "5",
"canonical_unit": "%p",
"dimension": "percentage_point",
"reason": null,
"utf16_start": 40,
"utf16_end": 43
},
{
"start": 47,
"end": 52,
"raw": "8~3kg",
"source_unit": "kg",
"source_sign": "unsigned",
"kind": "quantity_range",
"status": "normalized",
"reason": null,
"first": {
"status": "normalized",
"dimension": "mass",
"canonical_value": "8000",
"canonical_unit": "g",
"reason": null
},
"last": {
"status": "normalized",
"dimension": "mass",
"canonical_value": "3000",
"canonical_unit": "g",
"reason": null
},
"range_marker": "~",
"written_order": "descending",
"utf16_start": 48,
"utf16_end": 53
},
{
"kind": "time",
"start": 57,
"end": 67,
"raw": "26년 2월 23일",
"status": "ambiguous",
"canonical": null,
"reason": "century_not_declared",
"precision": "day",
"basis": "body_notation",
"utf16_start": 58,
"utf16_end": 68
},
{
"kind": "untyped_number",
"start": 75,
"end": 78,
"raw": "5천.",
"status": "ambiguous",
"reason": "unit_not_declared",
"source_unit": null,
"canonical_unit": null,
"dimension": null,
"canonical_value": null,
"utf16_start": 76,
"utf16_end": 79
},
{
"start": 83,
"end": 100,
"raw": "9007199254740993명",
"source_unit": "명",
"source_sign": "unsigned",
"kind": "quantity",
"status": "normalized",
"canonical_value": "9007199254740993",
"canonical_unit": "명",
"dimension": "person_count",
"reason": null,
"utf16_start": 84,
"utf16_end": 101
}
],
"truncated": false
}
}
}
Loading