dsh-memu-memory 1.4.2 → 1.4.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +108 -110
- package/CHANGELOG.md +169 -868
- package/LICENSE +21 -21
- package/README.md +368 -374
- package/README.zh.md +354 -360
- package/assets/icon.svg +7 -7
- package/cordis.patch.yml +8 -8
- package/docs/active-memory-ab.md +279 -385
- package/docs/binding-lifecycle.md +99 -99
- package/docs/client-conventions.md +75 -75
- package/docs/contributing.md +217 -217
- package/docs/emotion-model-spec.md +247 -281
- package/docs/env-and-commands.md +149 -152
- package/docs/host-adaptation.md +339 -360
- package/docs/host-api.md +131 -137
- package/docs/host-versions.md +117 -120
- package/docs/mcp-porting.md +146 -146
- package/docs/memory-semantics.md +271 -259
- package/docs/pending-work.md +44 -51
- package/docs/troubleshooting.md +909 -854
- package/lib/active-memory.js +160 -165
- package/lib/activity-log.js +207 -207
- package/lib/auto-link/apply.js +248 -249
- package/lib/auto-link/candidates.js +163 -163
- package/lib/auto-link/decide.js +300 -300
- package/lib/auto-link/normalize.js +99 -99
- package/lib/auto-link/policy.js +42 -42
- package/lib/auto-link/suppression.js +101 -101
- package/lib/auto-link/terminology.js +311 -311
- package/lib/backup.js +1254 -1254
- package/lib/binding-diagnostics.js +634 -634
- package/lib/budget.js +118 -118
- package/lib/client.js +9820 -9751
- package/lib/commitment.js +178 -138
- package/lib/consolidation-ledger.js +434 -438
- package/lib/consolidation-receipt.js +386 -387
- package/lib/consolidation-snapshot.js +420 -405
- package/lib/consolidation-worker.js +240 -241
- package/lib/context.js +33 -33
- package/lib/curator-evidence.js +131 -131
- package/lib/curator-policy.js +198 -202
- package/lib/curator.js +1813 -1811
- package/lib/cycle.js +524 -513
- package/lib/diagnostics.js +524 -524
- package/lib/embedding-catalog.js +1051 -1055
- package/lib/embedding-download.js +637 -607
- package/lib/embedding-runtime.js +946 -946
- package/lib/engine.js +1103 -1103
- package/lib/entity-repair.js +285 -285
- package/lib/environment-check.js +394 -394
- package/lib/fork-model.js +312 -312
- package/lib/freshness.js +128 -121
- package/lib/global-graph.js +389 -389
- package/lib/graph.js +1193 -1193
- package/lib/host-surface.js +30 -30
- package/lib/inbound.js +284 -281
- package/lib/index.js +506 -511
- package/lib/inject.js +2639 -2619
- package/lib/lexicon-scope.js +143 -143
- package/lib/lexicon.js +1183 -1192
- package/lib/lifecycle-audit.js +37 -0
- package/lib/local-embedding-entry.js +145 -145
- package/lib/local-embedding-manager.js +306 -306
- package/lib/local-embedding-protocol.js +210 -210
- package/lib/local-embedding-worker.js +682 -682
- package/lib/local-vector-service.js +506 -453
- package/lib/maintenance-ledger.js +138 -0
- package/lib/maintenance-review.js +103 -0
- package/lib/maintenance-runner.js +267 -0
- package/lib/managed-runtime.js +118 -118
- package/lib/media.js +607 -513
- package/lib/memory.js +5035 -4941
- package/lib/memu-config.js +213 -213
- package/lib/memu.js +565 -565
- package/lib/migrate.js +200 -200
- package/lib/mood.js +300 -300
- package/lib/paths.js +453 -457
- package/lib/port-surface.js +82 -82
- package/lib/preset-mount.js +79 -0
- package/lib/preset-scope.js +126 -126
- package/lib/private-links.js +567 -567
- package/lib/private.js +2198 -2209
- package/lib/prompts/global-curator.md +135 -135
- package/lib/prompts/image-caption.md +1 -1
- package/lib/prompts/private-consolidation.coding.md +39 -39
- package/lib/prompts/private-consolidation.md +32 -32
- package/lib/prompts/private-consolidation.mixed.md +37 -37
- package/lib/prompts/private-consolidation.rp.md +47 -47
- package/lib/prompts/private-policy.coding.md +35 -35
- package/lib/prompts/private-policy.md +36 -36
- package/lib/prompts/private-policy.mixed.md +29 -29
- package/lib/prompts/private-policy.rp.md +43 -43
- package/lib/prompts/system-guide.coding.md +20 -20
- package/lib/prompts/system-guide.md +25 -25
- package/lib/prompts/system-guide.mixed.md +22 -22
- package/lib/prompts/system-guide.rp.md +23 -23
- package/lib/prompts.js +331 -331
- package/lib/recall-cache.js +212 -212
- package/lib/remote-summary.js +123 -123
- package/lib/rpc.js +2531 -2532
- package/lib/salient-wake.js +438 -438
- package/lib/snippet.js +125 -125
- package/lib/state.js +1948 -1928
- package/lib/stats.js +236 -236
- package/lib/temporal.js +345 -348
- package/lib/text-retrieval.js +992 -1001
- package/lib/text-similarity.js +305 -305
- package/lib/tokenizer-v2.js +271 -271
- package/lib/tools.js +1098 -1091
- package/lib/turn-log.js +545 -545
- package/lib/vector-migration.js +928 -930
- package/lib/vector-profile.js +1353 -1359
- package/lib/visibility.js +59 -59
- package/lib/webui/graph-LICENSE.txt +1 -1
- package/lib/webui/graph-view.bundle.js +2 -2
- package/lib/webui/graph-view.js +1171 -1175
- package/lib/webui/mark.svg +7 -7
- package/lib/webui/standalone.css +1178 -1178
- package/lib/webui/standalone.js +238 -238
- package/lib/webui.js +136 -136
- package/lib/worker-pool.js +1015 -979
- package/package.json +153 -153
- package/requirements.lock +34 -34
- package/scripts/acceptance-local.mjs +946 -946
- package/scripts/active-memory-ab-live.mjs +653 -655
- package/scripts/active-memory-ab.mjs +1302 -1302
- package/scripts/audit-memory-lifecycle.mjs +55 -0
- package/scripts/bench-recall.mjs +142 -142
- package/scripts/build-graph.mjs +44 -45
- package/scripts/chain-matrix.mjs +541 -541
- package/scripts/check-all.mjs +68 -74
- package/scripts/check-doc-drift.mjs +547 -547
- package/scripts/check-graph-browser.mjs +261 -261
- package/scripts/check-i18n.mjs +81 -83
- package/scripts/check-idle-compositing.mjs +61 -61
- package/scripts/check-seed-compat.mjs +115 -118
- package/scripts/check-text-format.mjs +55 -0
- package/scripts/curator-pair-calibrate.mjs +259 -259
- package/scripts/diagnose-bindings.mjs +176 -176
- package/scripts/drive-session.mjs +863 -864
- package/scripts/engine-smoke.mjs +69 -69
- package/scripts/entity-dedupe.mjs +110 -110
- package/scripts/lexicon-calibrate.mjs +230 -230
- package/scripts/lexicon-dedupe.mjs +318 -318
- package/scripts/local-runtime-install.mjs +142 -142
- package/scripts/local-runtime-verify.mjs +56 -56
- package/scripts/local-vector-verify.mjs +93 -93
- package/scripts/make-readme-shots.mjs +59 -59
- package/scripts/migrate-visibility.mjs +94 -94
- package/scripts/mock-llm.mjs +938 -903
- package/scripts/preview.mjs +58 -58
- package/scripts/prune-sessions.mjs +31 -34
- package/scripts/prune-turnlogs.mjs +29 -30
- package/scripts/prune-vectors.mjs +24 -24
- package/scripts/rebuild-auto-links.mjs +252 -252
- package/scripts/recall-bench.mjs +820 -820
- package/scripts/repetition-ab-scenarios.mjs +93 -93
- package/scripts/repetition-detector-snapshot.mjs +314 -314
- package/scripts/repetition-eval-live.mjs +360 -360
- package/scripts/repetition-eval.mjs +702 -702
- package/scripts/replay.mjs +705 -575
- package/scripts/setup.mjs +46 -46
- package/scripts/simulate-injection.mjs +299 -301
- package/tests/absorb-review.test.mjs +354 -354
- package/tests/activation.test.mjs +89 -89
- package/tests/active-memory-ab.test.mjs +745 -745
- package/tests/active-memory.test.mjs +129 -129
- package/tests/activity-log.test.mjs +126 -126
- package/tests/activity-wiring.test.mjs +155 -155
- package/tests/appendix-b.test.mjs +217 -217
- package/tests/appendix-b2.test.mjs +166 -166
- package/tests/audit-batch3.test.mjs +247 -247
- package/tests/audit-regression.test.mjs +275 -275
- package/tests/audit-safety.test.mjs +227 -227
- package/tests/auto-link-candidates.test.mjs +245 -245
- package/tests/auto-link-decide.test.mjs +512 -512
- package/tests/auto-link-migration.test.mjs +133 -133
- package/tests/backend-compensation.test.mjs +272 -272
- package/tests/backup.test.mjs +793 -793
- package/tests/binding-diagnostics.test.mjs +720 -720
- package/tests/browser.test.mjs +476 -476
- package/tests/client-export.test.mjs +521 -521
- package/tests/client-render.test.mjs +2008 -1728
- package/tests/coalesce.test.mjs +113 -113
- package/tests/commitment.test.mjs +243 -144
- package/tests/config.test.mjs +159 -159
- package/tests/consolidation-credential.test.mjs +330 -330
- package/tests/consolidation-feed.test.mjs +122 -122
- package/tests/consolidation-fork.test.mjs +1586 -1539
- package/tests/consolidation-ledger.test.mjs +389 -389
- package/tests/consolidation-snapshot.test.mjs +567 -505
- package/tests/consolidation-value.test.mjs +89 -89
- package/tests/context-scope.test.mjs +40 -40
- package/tests/cross-library-recall.test.mjs +269 -269
- package/tests/cue.test.mjs +58 -58
- package/tests/curator-absorb.test.mjs +444 -444
- package/tests/curator-calibrate.test.mjs +173 -173
- package/tests/curator-clusters.test.mjs +306 -306
- package/tests/curator-commit.test.mjs +325 -325
- package/tests/curator-contract.test.mjs +191 -191
- package/tests/curator-evidence.test.mjs +102 -102
- package/tests/curator-link.test.mjs +317 -317
- package/tests/curator-pairing.test.mjs +339 -339
- package/tests/curator-probe.test.mjs +490 -490
- package/tests/curator-review-mode.test.mjs +315 -315
- package/tests/curator.test.mjs +152 -152
- package/tests/cycle.test.mjs +567 -568
- package/tests/diagnostics.test.mjs +278 -278
- package/tests/disclosure.test.mjs +249 -249
- package/tests/doc-drift.test.mjs +342 -342
- package/tests/drive-session.test.mjs +152 -152
- package/tests/dynamic-rrf.test.mjs +182 -182
- package/tests/dynamic-threshold.test.mjs +52 -52
- package/tests/embedding-catalog.test.mjs +374 -374
- package/tests/embedding-download.test.mjs +529 -486
- package/tests/embedding-runtime.test.mjs +449 -449
- package/tests/emotion-chain.test.mjs +84 -84
- package/tests/emotion-contract.test.mjs +445 -445
- package/tests/engine.test.mjs +108 -108
- package/tests/entity-merge.test.mjs +278 -278
- package/tests/environment-check.test.mjs +187 -187
- package/tests/fact-version.test.mjs +146 -146
- package/tests/fixtures/active-memory-ab/README.md +56 -56
- package/tests/fixtures/active-memory-ab/cases/01-clean-dense-turn.json +351 -351
- package/tests/fixtures/active-memory-ab/cases/02-clean-thin-turn.json +122 -122
- package/tests/fixtures/active-memory-ab/cases/03-clean-starved-small.json +125 -125
- package/tests/fixtures/active-memory-ab/cases/04-clean-large-thin.json +137 -137
- package/tests/fixtures/active-memory-ab/cases/05-clean-half-step.json +166 -166
- package/tests/fixtures/active-memory-ab/cases/06-clean-duplicate-reread.json +197 -197
- package/tests/fixtures/active-memory-ab/cases/07-clean-recent-recall.json +125 -125
- package/tests/fixtures/active-memory-ab/cases/08-global-relevant.json +111 -111
- package/tests/fixtures/active-memory-ab/cases/09-global-irrelevant-conflict.json +158 -158
- package/tests/fixtures/active-memory-ab/cases/10-no-human-turn.json +97 -97
- package/tests/fixtures/active-memory-ab/cases/11-clean-scale-100.json +137 -137
- package/tests/fixtures/active-memory-ab/cases/12-clean-scale-1000.json +137 -137
- package/tests/fixtures/active-memory-ab/cases/13-clean-scale-5000.json +137 -137
- package/tests/fixtures/active-memory-ab/cases/14-clean-scale-20000.json +137 -137
- package/tests/fixtures/active-memory-ab/lead-ai-reviewed-reference.json +191 -191
- package/tests/fixtures/active-memory-ab/manifest.json +199 -199
- package/tests/fixtures/curator-pairs/labeled.v1.jsonl +90 -90
- package/tests/fixtures/lexicon-stub-hooks.mjs +15 -15
- package/tests/fixtures/lexicon-stub.mjs +284 -284
- package/tests/fixtures/mock-llm/reply.txt +1 -1
- package/tests/fixtures/recall-bench/01-paraphrase.json +183 -183
- package/tests/fixtures/recall-bench/02-exact.json +152 -152
- package/tests/fixtures/recall-bench/03-negation.json +172 -172
- package/tests/fixtures/recall-bench/04-no-answer.json +161 -161
- package/tests/fixtures/recall-bench/05-conflict.json +193 -193
- package/tests/fixtures/recall-bench/06-boundary.json +272 -272
- package/tests/fixtures/recall-bench/07-secret.json +190 -190
- package/tests/fixtures/recall-bench/08-coding.json +163 -163
- package/tests/fixtures/recall-bench-gold-candidates/01-paraphrase.json +193 -193
- package/tests/fixtures/recall-bench-gold-candidates/02-exact.json +197 -197
- package/tests/fixtures/recall-bench-gold-candidates/03-negation.json +244 -244
- package/tests/fixtures/recall-bench-gold-candidates/04-no-answer.json +189 -189
- package/tests/fixtures/recall-bench-gold-candidates/05-conflict.json +245 -245
- package/tests/fixtures/recall-bench-gold-candidates/06-boundary.json +279 -279
- package/tests/fixtures/recall-bench-gold-candidates/07-secret.json +224 -224
- package/tests/fixtures/recall-bench-gold-candidates/08-coding.json +218 -218
- package/tests/fixtures/repetition-eval/01-zh-rp.json +92 -92
- package/tests/fixtures/repetition-eval/02-en.json +96 -96
- package/tests/fixtures/repetition-eval/03-code.json +90 -90
- package/tests/fixtures/repetition-eval/04-names.json +115 -115
- package/tests/fixtures/repetition-eval/05-user-quote.json +95 -95
- package/tests/fixtures/repetition-eval/06-agreement.json +85 -85
- package/tests/fixtures/tokenizer-cases.mjs +27 -27
- package/tests/fork-model.test.mjs +345 -345
- package/tests/freshness.test.mjs +140 -140
- package/tests/global-graph.test.mjs +106 -106
- package/tests/global-lifecycle.test.mjs +149 -149
- package/tests/graph-cascade.test.mjs +363 -363
- package/tests/graph-view.test.mjs +414 -414
- package/tests/graph.test.mjs +371 -371
- package/tests/help-page.test.mjs +416 -416
- package/tests/hook-order.test.mjs +72 -72
- package/tests/humanlike.test.mjs +232 -232
- package/tests/hybrid-recall.test.mjs +610 -610
- package/tests/inbound.test.mjs +441 -441
- package/tests/inject-conflict.test.mjs +173 -173
- package/tests/inject-guide.test.mjs +513 -513
- package/tests/inject.test.mjs +384 -384
- package/tests/injection-budget-settings.test.mjs +35 -35
- package/tests/injection-budget.test.mjs +165 -165
- package/tests/injection-chain.test.mjs +442 -442
- package/tests/injection-intent-budget.test.mjs +92 -92
- package/tests/injection-rollback.test.mjs +428 -428
- package/tests/issue2-chain.test.mjs +169 -169
- package/tests/lexical-bm25.test.mjs +171 -171
- package/tests/lexicon-calibrate.test.mjs +48 -48
- package/tests/lexicon-dedupe.test.mjs +171 -171
- package/tests/lexicon-frequency.test.mjs +195 -195
- package/tests/lexicon-inject.test.mjs +459 -459
- package/tests/lexicon-perf.test.mjs +464 -464
- package/tests/lexicon-scope.test.mjs +256 -256
- package/tests/lexicon-ui.test.mjs +667 -667
- package/tests/lexicon.test.mjs +922 -922
- package/tests/lifecycle-audit.test.mjs +56 -0
- package/tests/loader-register.mjs +3 -3
- package/tests/loader.mjs +17 -17
- package/tests/local-embedding-entry.test.mjs +95 -95
- package/tests/local-embedding-env.test.mjs +51 -51
- package/tests/local-embedding-manager.test.mjs +494 -494
- package/tests/local-embedding-protocol.test.mjs +121 -121
- package/tests/local-embedding-worker.test.mjs +440 -440
- package/tests/local-vector-service.test.mjs +41 -0
- package/tests/maintenance-status.test.mjs +77 -0
- package/tests/maintenance.test.mjs +438 -0
- package/tests/managed-memory.test.mjs +1234 -1234
- package/tests/managed-runtime.test.mjs +72 -72
- package/tests/media-injection.test.mjs +225 -0
- package/tests/media.test.mjs +135 -135
- package/tests/memory-chain.test.mjs +169 -169
- package/tests/memory-fields.test.mjs +134 -134
- package/tests/memu-resident-route.test.mjs +130 -130
- package/tests/memu-resident.test.mjs +33 -33
- package/tests/migration-payload.test.mjs +40 -42
- package/tests/mock-llm.test.mjs +263 -225
- package/tests/mood.test.mjs +109 -109
- package/tests/multiprocess.test.mjs +29 -29
- package/tests/negation-polarity.test.mjs +135 -135
- package/tests/new-session-gate.test.mjs +617 -617
- package/tests/open-loop-date.test.mjs +416 -416
- package/tests/ops-hardening.test.mjs +209 -209
- package/tests/package-shipping.test.mjs +88 -88
- package/tests/pending-injection.test.mjs +107 -107
- package/tests/port-surface.test.mjs +138 -138
- package/tests/preset-mount.test.mjs +124 -0
- package/tests/preset-scope.test.mjs +212 -212
- package/tests/private-links.test.mjs +493 -493
- package/tests/prompt-semantics.test.mjs +51 -51
- package/tests/prompts.test.mjs +315 -315
- package/tests/query-decompose.test.mjs +62 -62
- package/tests/react-snapshot.test.mjs +24 -24
- package/tests/recall-bench.test.mjs +317 -317
- package/tests/recall-cache.test.mjs +292 -292
- package/tests/recall-corpus-eligibility.test.mjs +51 -51
- package/tests/recall-dedup.test.mjs +446 -446
- package/tests/recall-intent.test.mjs +172 -172
- package/tests/regression-sweep.test.mjs +311 -311
- package/tests/reminder-fairness.test.mjs +55 -0
- package/tests/remote.test.mjs +140 -140
- package/tests/rename.test.mjs +30 -30
- package/tests/repetition-detector-snapshot.test.mjs +231 -231
- package/tests/repetition-eval-live.test.mjs +177 -177
- package/tests/repetition-eval.test.mjs +373 -373
- package/tests/salience-domain.test.mjs +173 -173
- package/tests/salient-date.test.mjs +142 -142
- package/tests/salient-wake.test.mjs +389 -389
- package/tests/settings-beat.test.mjs +222 -222
- package/tests/settings-coverage.test.mjs +97 -97
- package/tests/settings-plumbing.test.mjs +79 -79
- package/tests/short-term.test.mjs +182 -166
- package/tests/sidebar-beat.test.mjs +213 -213
- package/tests/sidebar-refresh.test.mjs +123 -123
- package/tests/sidebar.test.mjs +102 -74
- package/tests/simulate-injection.test.mjs +76 -76
- package/tests/slot-similarity.test.mjs +153 -153
- package/tests/snippet.test.mjs +79 -79
- package/tests/source-kind.test.mjs +52 -52
- package/tests/speaker-provenance.test.mjs +120 -120
- package/tests/stale-maintenance.test.mjs +206 -131
- package/tests/state-gc.test.mjs +34 -34
- package/tests/state.test.mjs +155 -155
- package/tests/stats.test.mjs +112 -112
- package/tests/storage.test.mjs +125 -125
- package/tests/stubs/child-process-spy.mjs +14 -14
- package/tests/stubs/dsh-llm-pi-ai.js +13 -13
- package/tests/stubs/dsh-llm.js +5 -5
- package/tests/stubs/dsh-schedule.js +140 -140
- package/tests/stubs/dsh-session-query.js +26 -26
- package/tests/stubs/dsh-session.js +7 -7
- package/tests/stubs/dsh-tools.js +101 -101
- package/tests/temporal-repair.test.mjs +78 -0
- package/tests/temporal.test.mjs +124 -124
- package/tests/text-format.test.mjs +24 -0
- package/tests/tokenizer-v2.test.mjs +142 -142
- package/tests/tool-schema.test.mjs +126 -126
- package/tests/tools.test.mjs +810 -810
- package/tests/turn-log.test.mjs +487 -487
- package/tests/ui-defaults.test.mjs +57 -57
- package/tests/update-fields.test.mjs +138 -138
- package/tests/vector-gc.test.mjs +37 -37
- package/tests/vector-migration.test.mjs +810 -810
- package/tests/vector-profile.test.mjs +449 -449
- package/tests/visibility-migration.test.mjs +152 -152
- package/tests/webui-auth.test.mjs +963 -963
- package/tests/webui.test.mjs +421 -421
- package/tests/windows-env.test.mjs +89 -89
- package/tests/worker-lifecycle.test.mjs +1521 -1429
|
@@ -1,414 +1,414 @@
|
|
|
1
|
-
import test from 'node:test'
|
|
2
|
-
import assert from 'node:assert/strict'
|
|
3
|
-
import vm from 'node:vm'
|
|
4
|
-
import fs from 'node:fs'
|
|
5
|
-
import { createForceLayout, createGraphView, DEFAULT_OPTIONS, fitCamera, zoomCamera, placeLabels } from '../lib/webui/graph-view.js'
|
|
6
|
-
|
|
7
|
-
const graph = (count, connected = true) => ({
|
|
8
|
-
nodes: Array.from({ length: count }, (_, i) => ({ id: `n${String(i).padStart(3, '0')}`, title: `Memory ${i}`, kind: 'memory', strength: .5 })),
|
|
9
|
-
edges: connected ? Array.from({ length: count - 1 }, (_, i) => ({ from: `n${String(i).padStart(3, '0')}`, to: `n${String(i + 1).padStart(3, '0')}` })) : [],
|
|
10
|
-
})
|
|
11
|
-
|
|
12
|
-
test('layout preserves object identity, positions and cooling on equivalent refreshed snapshots', () => {
|
|
13
|
-
const data = graph(30), before = structuredClone(data), layout = createForceLayout()
|
|
14
|
-
layout.update(data); layout.settle()
|
|
15
|
-
const node = layout.nodes[0], position = { x: node.x, y: node.y }, alpha = layout.alpha
|
|
16
|
-
assert.equal(layout.update(structuredClone(data)), false)
|
|
17
|
-
assert.equal(layout.nodes[0], node)
|
|
18
|
-
assert.equal(node.x, position.x); assert.equal(node.y, position.y)
|
|
19
|
-
assert.equal(layout.alpha, alpha)
|
|
20
|
-
assert.equal(layout.active, false, 'a settled layout must stop integrating')
|
|
21
|
-
assert.deepEqual(data, before, 'the RPC snapshot must never be mutated')
|
|
22
|
-
layout.update({ nodes: data.nodes.slice(0, 4), edges: data.edges.slice(0, 3) })
|
|
23
|
-
layout.update(data)
|
|
24
|
-
assert.equal(layout.nodes[0], node, 'positions live in the cache, not in the snapshot objects')
|
|
25
|
-
layout.dispose()
|
|
26
|
-
})
|
|
27
|
-
|
|
28
|
-
test('disconnected 200-node layout stays finite, separated and framed without boundary clamping', () => {
|
|
29
|
-
const layout = createForceLayout()
|
|
30
|
-
layout.update(graph(200, false)); layout.settle(300)
|
|
31
|
-
const camera = fitCamera(layout.nodes, 340, 380)
|
|
32
|
-
for (const n of layout.nodes) {
|
|
33
|
-
assert.ok(Number.isFinite(n.x) && Number.isFinite(n.y))
|
|
34
|
-
const x = n.x * camera.k + camera.x, y = n.y * camera.k + camera.y
|
|
35
|
-
assert.ok(x > 30 && x < 310 && y > 30 && y < 350)
|
|
36
|
-
}
|
|
37
|
-
for (let i = 0; i < layout.nodes.length; i++) for (let j = i + 1; j < layout.nodes.length; j++) {
|
|
38
|
-
const a = layout.nodes[i], b = layout.nodes[j]
|
|
39
|
-
assert.ok(Math.hypot(a.x - b.x, a.y - b.y) > a.radius + b.radius, 'nodes must not overlap')
|
|
40
|
-
}
|
|
41
|
-
layout.dispose()
|
|
42
|
-
})
|
|
43
|
-
|
|
44
|
-
test('invalid/self edges are ignored, all unique nodes retained, node order deterministic', () => {
|
|
45
|
-
const data = graph(250, false)
|
|
46
|
-
data.edges.push({ from: 'missing', to: 'n000' }, { from: 'n000', to: 'n000' })
|
|
47
|
-
const a = createForceLayout(), b = createForceLayout()
|
|
48
|
-
a.update(data)
|
|
49
|
-
b.update({ nodes: [...data.nodes, data.nodes[0]].reverse(), edges: [] })
|
|
50
|
-
assert.equal(a.nodes.length, 250); assert.equal(b.nodes.length, 250); assert.equal(a.edges.length, 0)
|
|
51
|
-
a.settle(); b.settle()
|
|
52
|
-
assert.deepEqual(a.nodes.map(n => [n.id, n.x, n.y]), b.nodes.map(n => [n.id, n.x, n.y]),
|
|
53
|
-
'the jitter must be deterministic, never Math.random')
|
|
54
|
-
a.dispose(); b.dispose()
|
|
55
|
-
})
|
|
56
|
-
|
|
57
|
-
test('zoom keeps the cursor world point fixed and clamps extreme scales', () => {
|
|
58
|
-
const camera = { x: 100, y: 80, k: .8 }, point = { x: 350, y: 210 }
|
|
59
|
-
for (const factor of [.0001, .8, 1.25, 10000]) {
|
|
60
|
-
const next = zoomCamera(camera, factor, point)
|
|
61
|
-
assert.ok(Math.abs((point.x - camera.x) / camera.k - (point.x - next.x) / next.k) < 1e-8)
|
|
62
|
-
assert.ok(Math.abs((point.y - camera.y) / camera.k - (point.y - next.y) / next.k) < 1e-8)
|
|
63
|
-
assert.ok(next.k >= .06 && next.k <= 5)
|
|
64
|
-
}
|
|
65
|
-
})
|
|
66
|
-
|
|
67
|
-
test('labels neither overlap each other nor node obstacles and remain inside canvas', () => {
|
|
68
|
-
const candidates = Array.from({ length: 50 }, (_, i) => ({
|
|
69
|
-
x: 40 + i % 8 * 40, y: 40 + Math.floor(i / 8) * 40, radius: 5, textWidth: 90,
|
|
70
|
-
}))
|
|
71
|
-
const obstacles = candidates.map(c => ({ x: c.x - 7, y: c.y - 7, width: 14, height: 14 }))
|
|
72
|
-
const labels = placeLabels(candidates, obstacles, 360, 360)
|
|
73
|
-
assert.ok(labels.length > 0 && labels.length < candidates.length)
|
|
74
|
-
const intersect = (a, b) => a.x < b.x + b.width && a.x + a.width > b.x && a.y < b.y + b.height && a.y + a.height > b.y
|
|
75
|
-
for (let i = 0; i < labels.length; i++) {
|
|
76
|
-
const a = labels[i].box
|
|
77
|
-
assert.ok(a.x >= 5 && a.x + a.width <= 355 && a.y >= 5 && a.y + a.height <= 355)
|
|
78
|
-
for (const b of obstacles) assert.equal(intersect(a, b), false)
|
|
79
|
-
for (const b of labels.slice(i + 1)) assert.equal(intersect(a, b.box), false)
|
|
80
|
-
}
|
|
81
|
-
})
|
|
82
|
-
|
|
83
|
-
test('pin/unpin/relayout/unpinAll keep the physics state coherent', () => {
|
|
84
|
-
const layout = createForceLayout()
|
|
85
|
-
const data = graph(12)
|
|
86
|
-
layout.update(data); layout.settle(120)
|
|
87
|
-
layout.pin('n000', 321, 123)
|
|
88
|
-
assert.equal(layout.nodes[0].fx, 321); assert.equal(layout.nodes[0].x, 321)
|
|
89
|
-
assert.equal(layout.nodes[0].fy, 123); assert.equal(layout.nodes[0].y, 123)
|
|
90
|
-
layout.tick()
|
|
91
|
-
assert.equal(layout.nodes[0].x, 321, 'a pinned node is never moved by forces')
|
|
92
|
-
layout.update(structuredClone(data))
|
|
93
|
-
assert.equal(layout.nodes[0].x, 321, 'a heartbeat refresh must not drop the pin')
|
|
94
|
-
layout.unpin('n000')
|
|
95
|
-
assert.equal(layout.nodes[0].fx, null); assert.equal(layout.nodes[0].fy, null)
|
|
96
|
-
layout.pin('n001', 10, 10); layout.pin('n002', 20, 20)
|
|
97
|
-
layout.unpinAll()
|
|
98
|
-
assert.ok(layout.nodes.every(node => node.fx == null && node.fy == null))
|
|
99
|
-
layout.relayout(); layout.settle(120)
|
|
100
|
-
assert.ok(layout.nodes.every(node => Number.isFinite(node.x) && Number.isFinite(node.y) && node.fx == null))
|
|
101
|
-
assert.equal(layout.edges.length, 11)
|
|
102
|
-
layout.dispose()
|
|
103
|
-
})
|
|
104
|
-
|
|
105
|
-
test('relayout cools down and separates instead of shoving nodes forever', () => {
|
|
106
|
-
// 「缩到最小 → 重新布局 → 打架」的回归:硬分离阈值以前写成 24 / viewScale,
|
|
107
|
-
// k≈.06 时等于要求 400 世界像素间距,每帧互推、永不收敛。现在只看半径,
|
|
108
|
-
// 物理也拿不到相机(setScale 已删),缩放再小也不会改变力的量级。
|
|
109
|
-
assert.equal('setScale' in createForceLayout(), false, 'the physics must never be handed the camera scale')
|
|
110
|
-
const layout = createForceLayout()
|
|
111
|
-
layout.update(graph(60))
|
|
112
|
-
layout.relayout()
|
|
113
|
-
let previous = Infinity
|
|
114
|
-
for (let window = 0; window < 6; window++) {
|
|
115
|
-
const before = layout.nodes.map(node => [node.x, node.y])
|
|
116
|
-
for (let i = 0; i < 20; i++) layout.tick()
|
|
117
|
-
let maxMove = 0
|
|
118
|
-
layout.nodes.forEach((node, i) => { maxMove = Math.max(maxMove, Math.hypot(node.x - before[i][0], node.y - before[i][1])) })
|
|
119
|
-
assert.ok(maxMove < previous, `window ${window} must move less than the one before (${maxMove} >= ${previous})`)
|
|
120
|
-
previous = maxMove
|
|
121
|
-
}
|
|
122
|
-
assert.ok(previous < 4, `a cooled relayout must stop shoving, saw ${previous}px per window`)
|
|
123
|
-
for (let i = 0; i < layout.nodes.length; i++) for (let j = i + 1; j < layout.nodes.length; j++) {
|
|
124
|
-
const a = layout.nodes[i], b = layout.nodes[j]
|
|
125
|
-
assert.ok(Math.hypot(a.x - b.x, a.y - b.y) > a.radius + b.radius, 'relayout must not leave nodes overlapping')
|
|
126
|
-
}
|
|
127
|
-
layout.dispose()
|
|
128
|
-
})
|
|
129
|
-
|
|
130
|
-
test('configure maps all three force sliders and reheats without ever going non-finite', () => {
|
|
131
|
-
assert.equal(DEFAULT_OPTIONS.center, 0, 'center force is off by default')
|
|
132
|
-
assert.equal(DEFAULT_OPTIONS.clusterScope, true)
|
|
133
|
-
const layout = createForceLayout()
|
|
134
|
-
layout.update(graph(40)); layout.settle(80)
|
|
135
|
-
const cooled = layout.alpha
|
|
136
|
-
layout.configure({ repulsion: 300, distance: 180, center: 2 })
|
|
137
|
-
assert.ok(layout.alpha > cooled)
|
|
138
|
-
for (let i = 0; i < 200; i++) layout.tick()
|
|
139
|
-
assert.ok(layout.nodes.every(node => Number.isFinite(node.x) && Number.isFinite(node.y)))
|
|
140
|
-
layout.dispose()
|
|
141
|
-
})
|
|
142
|
-
|
|
143
|
-
test('drag scope freezes other clusters and repels the dragged one from them', () => {
|
|
144
|
-
// 两簇各 12 节点,链状相连;A 在左,B 在右,互不连通。
|
|
145
|
-
const nodes = [], edges = []
|
|
146
|
-
for (let group = 0; group < 2; group++) for (let i = 0; i < 12; i++) {
|
|
147
|
-
const id = `${group}-${String(i).padStart(2, '0')}`
|
|
148
|
-
nodes.push({ id, title: id, kind: 'memory' })
|
|
149
|
-
if (i) edges.push({ from: `${group}-${String(i - 1).padStart(2, '0')}`, to: id })
|
|
150
|
-
}
|
|
151
|
-
const layout = createForceLayout()
|
|
152
|
-
layout.update({ nodes, edges })
|
|
153
|
-
// 手动把两簇分开(布局原长由首次见到的距离决定,这里覆盖成稳定值)。
|
|
154
|
-
layout.nodes.forEach((node, i) => { node.x = i < 12 ? -260 + i * 8 : 260 + (i - 12) * 8; node.y = i < 12 ? 0 : 40; node.fx = null; node.fy = null })
|
|
155
|
-
layout.edges.forEach(edge => { edge.baseLen = 12 })
|
|
156
|
-
layout.settle(80)
|
|
157
|
-
const frozen = layout.nodes.slice(12).map(node => ({ id: node.id, x: node.x, y: node.y }))
|
|
158
|
-
const dragged = layout.nodes[0]
|
|
159
|
-
const scope = layout.startDragScope(dragged.id)
|
|
160
|
-
assert.ok(scope?.ids.size === 12, 'the drag scope is exactly one connected component')
|
|
161
|
-
assert.ok(!scope.ids.has(layout.nodes[12].id))
|
|
162
|
-
const centerX = frozen.reduce((sum, n) => sum + n.x, 0) / frozen.length
|
|
163
|
-
const centerY = frozen.reduce((sum, n) => sum + n.y, 0) / frozen.length
|
|
164
|
-
// 把整簇硬塞到冻结簇中心:靠单向斥力把活动簇推开,冻结簇自身只当障碍。
|
|
165
|
-
layout.reheat(.9)
|
|
166
|
-
for (let i = 0; i < scope.members.length; i++) {
|
|
167
|
-
const member = scope.members[i]
|
|
168
|
-
if (member === dragged) continue
|
|
169
|
-
member.x = centerX + (i % 4) * 4 - 6
|
|
170
|
-
member.y = centerY + Math.floor(i / 4) * 4 - 6
|
|
171
|
-
member.vx = 0; member.vy = 0
|
|
172
|
-
}
|
|
173
|
-
// 落点放在冻结簇包围盒之外:pin 节点优先跟手,不能被硬分离挪动。
|
|
174
|
-
layout.pin(dragged.id, Math.max(...frozen.map(n => n.x)) + 60, centerY)
|
|
175
|
-
for (let i = 0; i < 80; i++) layout.tick()
|
|
176
|
-
layout.setDragging(false)
|
|
177
|
-
for (let i = 0; i < 20; i++) layout.tick()
|
|
178
|
-
// 100 ticks 内 alpha 仍高于 .05,作用域未解除,其他簇必须一像素都不动。
|
|
179
|
-
for (const before of frozen) {
|
|
180
|
-
const after = layout.nodeById(before.id)
|
|
181
|
-
assert.equal(after.x, before.x, 'frozen clusters must not move at all')
|
|
182
|
-
assert.equal(after.y, before.y)
|
|
183
|
-
}
|
|
184
|
-
let min = Infinity
|
|
185
|
-
for (const a of scope.members) for (const b of frozen) min = Math.min(min, Math.hypot(a.x - b.x, a.y - b.y))
|
|
186
|
-
assert.ok(min > 12, `clusters must not overlap (min distance ${min.toFixed(1)} world px)`)
|
|
187
|
-
layout.dispose()
|
|
188
|
-
})
|
|
189
|
-
|
|
190
|
-
function harness(reducedMotion = true) {
|
|
191
|
-
const handlers = new Map(), windowHandlers = new Map(), frames = new Map(), arcs = [], focuses = [], texts = []
|
|
192
|
-
let id = 0, paints = 0
|
|
193
|
-
const ctx = new Proxy({
|
|
194
|
-
arc(x, y, radius) { arcs.push({ x, y, radius }) },
|
|
195
|
-
measureText(text) { return { width: text.length * 7 } },
|
|
196
|
-
clearRect() { paints++; arcs.length = 0; texts.length = 0 },
|
|
197
|
-
fillText(text) { texts.push(String(text)) },
|
|
198
|
-
}, { get(target, key) { return key in target ? target[key] : () => {} } })
|
|
199
|
-
const win = {
|
|
200
|
-
requestAnimationFrame(fn) { frames.set(++id, fn); return id },
|
|
201
|
-
cancelAnimationFrame(id) { frames.delete(id) },
|
|
202
|
-
addEventListener(type, fn) { windowHandlers.set(type, fn) },
|
|
203
|
-
removeEventListener(type) { windowHandlers.delete(type) },
|
|
204
|
-
getComputedStyle: () => ({ color: '#202020', getPropertyValue: () => '#ffffff' }),
|
|
205
|
-
devicePixelRatio: 2,
|
|
206
|
-
}
|
|
207
|
-
const captures = new Set()
|
|
208
|
-
const canvas = {
|
|
209
|
-
style: {}, ownerDocument: { defaultView: win, documentElement: {} },
|
|
210
|
-
getContext: () => ctx, getBoundingClientRect: () => ({ left: 0, top: 0, width: 600, height: 400 }),
|
|
211
|
-
focus() {},
|
|
212
|
-
setPointerCapture(id) { captures.add(id) },
|
|
213
|
-
hasPointerCapture: id => captures.has(id),
|
|
214
|
-
releasePointerCapture(id) { captures.delete(id) },
|
|
215
|
-
addEventListener(type, fn) { handlers.set(type, fn) },
|
|
216
|
-
removeEventListener(type) { handlers.delete(type) },
|
|
217
|
-
}
|
|
218
|
-
const view = createGraphView(canvas, { onFocus: id => focuses.push(id), reducedMotion })
|
|
219
|
-
const flush = () => {
|
|
220
|
-
const pending = [...frames.values()]; frames.clear()
|
|
221
|
-
for (const fn of pending) fn(1000)
|
|
222
|
-
}
|
|
223
|
-
const emit = (type, x, y, extra = {}) => handlers.get(type)?.({
|
|
224
|
-
type, clientX: x, clientY: y, button: 0, pointerId: 1, pointerType: 'mouse', preventDefault() {}, ...extra,
|
|
225
|
-
})
|
|
226
|
-
return { view, flush, emit, arcs, texts, frames, focuses, handlers, windowHandlers, getPaints: () => paints }
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
test('view snapshots retain multiple clusters and entities beyond the first 200 nodes', () => {
|
|
230
|
-
const data = graph(250, false)
|
|
231
|
-
for (let i = 0; i < 249; i++) {
|
|
232
|
-
if (i !== 119) data.edges.push({ from: data.nodes[i].id, to: data.nodes[i + 1].id })
|
|
233
|
-
}
|
|
234
|
-
for (const [id, from] of [['ent-first', 'n119'], ['ent-last', 'n249']]) {
|
|
235
|
-
data.nodes.push({ id, title: id, kind: 'person', strength: .5 })
|
|
236
|
-
data.edges.push({ from, to: id })
|
|
237
|
-
}
|
|
238
|
-
const h = harness()
|
|
239
|
-
h.view.update(data); h.flush()
|
|
240
|
-
const snapshot = h.view.snapshot()
|
|
241
|
-
assert.equal(snapshot.length, 252)
|
|
242
|
-
assert.deepEqual(new Set(snapshot.map(node => node.id)), new Set(data.nodes.map(node => node.id)))
|
|
243
|
-
for (const id of ['ent-first', 'ent-last']) {
|
|
244
|
-
const entity = snapshot.find(node => node.id === id)
|
|
245
|
-
assert.equal(entity.degree, 1, 'late entity links must survive canvas ingestion')
|
|
246
|
-
assert.ok(Number.isFinite(entity.x) && Number.isFinite(entity.y))
|
|
247
|
-
}
|
|
248
|
-
h.view.update({ nodes: [...data.nodes, data.nodes[0]].reverse(), edges: [...data.edges].reverse() }); h.flush()
|
|
249
|
-
assert.deepEqual(h.view.snapshot(), snapshot, 'equivalent refreshed input preserves every node and its position')
|
|
250
|
-
h.view.dispose()
|
|
251
|
-
})
|
|
252
|
-
|
|
253
|
-
test('the detailed layer actually paints labels and the label switch silences them', () => {
|
|
254
|
-
const h = harness()
|
|
255
|
-
h.view.update(graph(10)); h.flush()
|
|
256
|
-
assert.ok(h.texts.length > 0, 'labels must reach fillText (radius/textWidth must be finite)')
|
|
257
|
-
assert.ok(h.texts.length <= 10, 'labels must never be painted more often than there are nodes')
|
|
258
|
-
h.view.configure({ labels: false }); h.flush()
|
|
259
|
-
assert.equal(h.texts.length, 0)
|
|
260
|
-
h.view.dispose()
|
|
261
|
-
})
|
|
262
|
-
|
|
263
|
-
test('left drag never locks the node; right click toggles the pin; refresh preserves position', () => {
|
|
264
|
-
const h = harness()
|
|
265
|
-
h.view.update(graph(1)); h.flush()
|
|
266
|
-
const p = { ...h.arcs[0] }
|
|
267
|
-
h.emit('pointerdown', p.x, p.y)
|
|
268
|
-
h.emit('pointermove', p.x + 70, p.y + 25)
|
|
269
|
-
h.view.update(graph(1))
|
|
270
|
-
h.flush()
|
|
271
|
-
assert.equal(h.arcs[0].x, p.x + 70); assert.equal(h.arcs[0].y, p.y + 25)
|
|
272
|
-
h.emit('pointerup', p.x + 70, p.y + 25); h.flush()
|
|
273
|
-
assert.deepEqual(h.focuses, [], 'a drag must never focus/refetch')
|
|
274
|
-
assert.equal(h.view.isPinned('n000'), false, 'a left drag must not pin the node')
|
|
275
|
-
// 松手后位置留在落点,心跳刷新不把它拽回。
|
|
276
|
-
for (let i = 0; i < 3; i++) { h.view.update(graph(1)); h.flush() }
|
|
277
|
-
assert.equal(h.arcs[0].x, p.x + 70); assert.equal(h.arcs[0].y, p.y + 25)
|
|
278
|
-
h.emit('pointerdown', p.x + 70, p.y + 25)
|
|
279
|
-
h.emit('pointerup', p.x + 70, p.y + 25)
|
|
280
|
-
assert.deepEqual(h.focuses, ['n000'], 'a click without movement still focuses')
|
|
281
|
-
// 右键 = 锁定 / 解除,左键从不产生固定点。
|
|
282
|
-
h.emit('contextmenu', p.x + 70, p.y + 25); h.flush()
|
|
283
|
-
assert.equal(h.view.isPinned('n000'), true, 'right click pins the node')
|
|
284
|
-
assert.equal(h.view.pinnedCount(), 1)
|
|
285
|
-
assert.equal(h.arcs[0].x, p.x + 70, 'pinning must not teleport the node')
|
|
286
|
-
h.emit('contextmenu', p.x + 70, p.y + 25); h.flush()
|
|
287
|
-
assert.equal(h.view.isPinned('n000'), false, 'right click again unpins')
|
|
288
|
-
assert.equal(h.view.pinnedCount(), 0)
|
|
289
|
-
h.view.dispose()
|
|
290
|
-
assert.equal(h.handlers.size, 0); assert.equal(h.frames.size, 0); assert.equal(h.windowHandlers.size, 0)
|
|
291
|
-
})
|
|
292
|
-
|
|
293
|
-
test('dragging an already pinned node keeps the lock at the new position', () => {
|
|
294
|
-
const h = harness()
|
|
295
|
-
h.view.update(graph(1)); h.flush()
|
|
296
|
-
const p = { ...h.arcs[0] }
|
|
297
|
-
h.emit('contextmenu', p.x, p.y); h.flush()
|
|
298
|
-
assert.equal(h.view.isPinned('n000'), true)
|
|
299
|
-
h.emit('pointerdown', p.x, p.y)
|
|
300
|
-
h.emit('pointermove', p.x + 40, p.y + 10)
|
|
301
|
-
h.emit('pointerup', p.x + 40, p.y + 10); h.flush()
|
|
302
|
-
assert.equal(h.view.isPinned('n000'), true, 'an existing lock survives a left drag')
|
|
303
|
-
assert.equal(h.arcs[0].x, p.x + 40)
|
|
304
|
-
h.view.dispose()
|
|
305
|
-
})
|
|
306
|
-
|
|
307
|
-
test('pan/zoom and equivalent refresh never re-fit the camera; cancelled pointer never clicks', () => {
|
|
308
|
-
const h = harness()
|
|
309
|
-
h.view.update(graph(1)); h.flush()
|
|
310
|
-
const before = { ...h.arcs[0] }
|
|
311
|
-
h.emit('pointerdown', 30, 30); h.emit('pointermove', 70, 60); h.emit('pointerup', 70, 60); h.flush()
|
|
312
|
-
assert.equal(h.arcs[0].x, before.x + 40); assert.equal(h.arcs[0].y, before.y + 30)
|
|
313
|
-
h.view.zoom(.8); h.flush()
|
|
314
|
-
const zoomed = { ...h.arcs[0] }
|
|
315
|
-
h.view.update(graph(1)); h.flush()
|
|
316
|
-
assert.deepEqual(h.arcs[0], zoomed)
|
|
317
|
-
h.emit('pointerdown', zoomed.x, zoomed.y); h.emit('pointercancel', zoomed.x, zoomed.y)
|
|
318
|
-
assert.deepEqual(h.focuses, [])
|
|
319
|
-
h.view.dispose()
|
|
320
|
-
})
|
|
321
|
-
|
|
322
|
-
test('dense hit targets choose the closest node, not the last node in draw order', () => {
|
|
323
|
-
const h = harness()
|
|
324
|
-
h.view.update(graph(2)); h.view.zoom(.04); h.flush()
|
|
325
|
-
const p = h.arcs[0], other = h.arcs[1]
|
|
326
|
-
assert.ok(Math.hypot(other.x - p.x, other.y - p.y) < 10)
|
|
327
|
-
h.emit('pointerdown', p.x, p.y); h.emit('pointerup', p.x, p.y)
|
|
328
|
-
assert.deepEqual(h.focuses, ['n000'])
|
|
329
|
-
h.view.dispose()
|
|
330
|
-
})
|
|
331
|
-
|
|
332
|
-
test('pinch does not trigger focus and reduced-motion/paused views do not run an idle animation loop', () => {
|
|
333
|
-
const h = harness()
|
|
334
|
-
h.view.update(graph(10)); h.flush()
|
|
335
|
-
assert.equal(h.frames.size, 0, 'reduced motion must not keep a rAF loop alive')
|
|
336
|
-
const p = h.arcs[0]
|
|
337
|
-
h.emit('pointerdown', p.x, p.y, { pointerType: 'touch' })
|
|
338
|
-
h.emit('pointerdown', p.x + 40, p.y, { pointerType: 'touch', pointerId: 2 })
|
|
339
|
-
h.emit('pointermove', p.x + 80, p.y, { pointerType: 'touch', pointerId: 2 })
|
|
340
|
-
h.emit('pointerup', p.x, p.y)
|
|
341
|
-
h.emit('pointerup', p.x + 80, p.y, { pointerId: 2 })
|
|
342
|
-
assert.deepEqual(h.focuses, [])
|
|
343
|
-
h.view.dispose()
|
|
344
|
-
const paused = harness(false)
|
|
345
|
-
paused.view.configure({ motion: false }); paused.view.update(graph(10)); paused.flush()
|
|
346
|
-
assert.equal(paused.frames.size, 0, 'paused motion must not keep a rAF loop alive')
|
|
347
|
-
paused.view.dispose()
|
|
348
|
-
})
|
|
349
|
-
|
|
350
|
-
// GPU 守卫:动效开着也不能在冷却后空转重绘(曾经的实现是 30fps 永久整幅重绘)。
|
|
351
|
-
test('a cooled layout stops requesting frames even while motion is enabled', () => {
|
|
352
|
-
const h = harness(false)
|
|
353
|
-
h.view.update(graph(60))
|
|
354
|
-
for (let i = 0; i < 900 && h.frames.size; i++) h.flush()
|
|
355
|
-
assert.equal(h.frames.size, 0, 'a settled layout must not keep a rAF loop alive')
|
|
356
|
-
const painted = h.getPaints()
|
|
357
|
-
for (let i = 0; i < 200; i++) h.flush()
|
|
358
|
-
assert.equal(h.getPaints(), painted, 'idle time must not repaint the canvas')
|
|
359
|
-
h.view.zoom(1.2)
|
|
360
|
-
assert.equal(h.frames.size, 1, 'an interaction schedules exactly one repaint')
|
|
361
|
-
h.flush()
|
|
362
|
-
assert.equal(h.frames.size, 0, 'that repaint must not restart the loop')
|
|
363
|
-
h.view.dispose()
|
|
364
|
-
})
|
|
365
|
-
|
|
366
|
-
// 极小缩放下两颗节点可能只差 2-3px:纯最近邻会随鼠标 1px 抖动来回换人,
|
|
367
|
-
// 高亮反复切换就是用户看到的「节点互相打架闪现」。
|
|
368
|
-
test('hover keeps its target through 1px jitter at minimum zoom', () => {
|
|
369
|
-
const h = harness()
|
|
370
|
-
h.view.update(graph(2)); h.flush()
|
|
371
|
-
h.view.zoom(.05); h.flush()
|
|
372
|
-
const p = { ...h.arcs[0] }
|
|
373
|
-
const before = h.getPaints()
|
|
374
|
-
h.emit('pointermove', p.x, p.y); h.flush()
|
|
375
|
-
const first = h.getPaints()
|
|
376
|
-
assert.ok(first > before, 'hovering a node must repaint once')
|
|
377
|
-
h.emit('pointermove', p.x + 1, p.y + 1); h.flush()
|
|
378
|
-
assert.equal(h.getPaints(), first, 'a 1px nudge must not flip the hover target')
|
|
379
|
-
h.view.dispose()
|
|
380
|
-
})
|
|
381
|
-
|
|
382
|
-
test('Escape cancels a drag and clears the focus; toolbar actions do not throw', () => {
|
|
383
|
-
const h = harness()
|
|
384
|
-
h.view.update(graph(3)); h.flush()
|
|
385
|
-
const p = { ...h.arcs[0] }
|
|
386
|
-
h.emit('pointerdown', p.x, p.y)
|
|
387
|
-
h.emit('pointermove', p.x + 50, p.y + 30)
|
|
388
|
-
h.emit('keydown', p.x, p.y, { key: 'Escape' })
|
|
389
|
-
h.flush()
|
|
390
|
-
assert.deepEqual(h.focuses, [''])
|
|
391
|
-
h.view.unpinAll(); h.view.relayout(); h.view.configure({ center: 1, repulsion: 200, distance: 140 })
|
|
392
|
-
h.flush()
|
|
393
|
-
assert.ok(h.arcs.every(arc => Number.isFinite(arc.x) && Number.isFinite(arc.y)))
|
|
394
|
-
h.view.dispose()
|
|
395
|
-
})
|
|
396
|
-
|
|
397
|
-
test('shipped browser bundle exposes the controller with no runtime npm imports', () => {
|
|
398
|
-
const source = fs.readFileSync(new URL('../lib/webui/graph-view.bundle.js', import.meta.url), 'utf8')
|
|
399
|
-
assert.doesNotMatch(source, /d3-force|forceSimulation|forceManyBody|require\(/)
|
|
400
|
-
const scope = { setTimeout, clearTimeout, setInterval, clearInterval, performance, Date, Math, Map, Set, JSON, console }
|
|
401
|
-
vm.runInNewContext(source, scope)
|
|
402
|
-
const api = scope.DshMemuGraph
|
|
403
|
-
assert.equal(typeof api.createGraphView, 'function')
|
|
404
|
-
assert.equal(typeof api.createForceLayout, 'function')
|
|
405
|
-
assert.equal(typeof api.fitCamera, 'function')
|
|
406
|
-
assert.equal(typeof api.zoomCamera, 'function')
|
|
407
|
-
assert.equal(typeof api.placeLabels, 'function')
|
|
408
|
-
assert.equal(api.DEFAULT_OPTIONS.center, 0)
|
|
409
|
-
assert.equal(api.DEFAULT_OPTIONS.clusterScope, true)
|
|
410
|
-
const layout = api.createForceLayout()
|
|
411
|
-
layout.update(graph(251, false))
|
|
412
|
-
assert.equal(layout.nodes.length, 251, 'the shipped browser bundle must retain nodes beyond 200')
|
|
413
|
-
layout.dispose()
|
|
414
|
-
})
|
|
1
|
+
import test from 'node:test'
|
|
2
|
+
import assert from 'node:assert/strict'
|
|
3
|
+
import vm from 'node:vm'
|
|
4
|
+
import fs from 'node:fs'
|
|
5
|
+
import { createForceLayout, createGraphView, DEFAULT_OPTIONS, fitCamera, zoomCamera, placeLabels } from '../lib/webui/graph-view.js'
|
|
6
|
+
|
|
7
|
+
const graph = (count, connected = true) => ({
|
|
8
|
+
nodes: Array.from({ length: count }, (_, i) => ({ id: `n${String(i).padStart(3, '0')}`, title: `Memory ${i}`, kind: 'memory', strength: .5 })),
|
|
9
|
+
edges: connected ? Array.from({ length: count - 1 }, (_, i) => ({ from: `n${String(i).padStart(3, '0')}`, to: `n${String(i + 1).padStart(3, '0')}` })) : [],
|
|
10
|
+
})
|
|
11
|
+
|
|
12
|
+
test('layout preserves object identity, positions and cooling on equivalent refreshed snapshots', () => {
|
|
13
|
+
const data = graph(30), before = structuredClone(data), layout = createForceLayout()
|
|
14
|
+
layout.update(data); layout.settle()
|
|
15
|
+
const node = layout.nodes[0], position = { x: node.x, y: node.y }, alpha = layout.alpha
|
|
16
|
+
assert.equal(layout.update(structuredClone(data)), false)
|
|
17
|
+
assert.equal(layout.nodes[0], node)
|
|
18
|
+
assert.equal(node.x, position.x); assert.equal(node.y, position.y)
|
|
19
|
+
assert.equal(layout.alpha, alpha)
|
|
20
|
+
assert.equal(layout.active, false, 'a settled layout must stop integrating')
|
|
21
|
+
assert.deepEqual(data, before, 'the RPC snapshot must never be mutated')
|
|
22
|
+
layout.update({ nodes: data.nodes.slice(0, 4), edges: data.edges.slice(0, 3) })
|
|
23
|
+
layout.update(data)
|
|
24
|
+
assert.equal(layout.nodes[0], node, 'positions live in the cache, not in the snapshot objects')
|
|
25
|
+
layout.dispose()
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
test('disconnected 200-node layout stays finite, separated and framed without boundary clamping', () => {
|
|
29
|
+
const layout = createForceLayout()
|
|
30
|
+
layout.update(graph(200, false)); layout.settle(300)
|
|
31
|
+
const camera = fitCamera(layout.nodes, 340, 380)
|
|
32
|
+
for (const n of layout.nodes) {
|
|
33
|
+
assert.ok(Number.isFinite(n.x) && Number.isFinite(n.y))
|
|
34
|
+
const x = n.x * camera.k + camera.x, y = n.y * camera.k + camera.y
|
|
35
|
+
assert.ok(x > 30 && x < 310 && y > 30 && y < 350)
|
|
36
|
+
}
|
|
37
|
+
for (let i = 0; i < layout.nodes.length; i++) for (let j = i + 1; j < layout.nodes.length; j++) {
|
|
38
|
+
const a = layout.nodes[i], b = layout.nodes[j]
|
|
39
|
+
assert.ok(Math.hypot(a.x - b.x, a.y - b.y) > a.radius + b.radius, 'nodes must not overlap')
|
|
40
|
+
}
|
|
41
|
+
layout.dispose()
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
test('invalid/self edges are ignored, all unique nodes retained, node order deterministic', () => {
|
|
45
|
+
const data = graph(250, false)
|
|
46
|
+
data.edges.push({ from: 'missing', to: 'n000' }, { from: 'n000', to: 'n000' })
|
|
47
|
+
const a = createForceLayout(), b = createForceLayout()
|
|
48
|
+
a.update(data)
|
|
49
|
+
b.update({ nodes: [...data.nodes, data.nodes[0]].reverse(), edges: [] })
|
|
50
|
+
assert.equal(a.nodes.length, 250); assert.equal(b.nodes.length, 250); assert.equal(a.edges.length, 0)
|
|
51
|
+
a.settle(); b.settle()
|
|
52
|
+
assert.deepEqual(a.nodes.map(n => [n.id, n.x, n.y]), b.nodes.map(n => [n.id, n.x, n.y]),
|
|
53
|
+
'the jitter must be deterministic, never Math.random')
|
|
54
|
+
a.dispose(); b.dispose()
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
test('zoom keeps the cursor world point fixed and clamps extreme scales', () => {
|
|
58
|
+
const camera = { x: 100, y: 80, k: .8 }, point = { x: 350, y: 210 }
|
|
59
|
+
for (const factor of [.0001, .8, 1.25, 10000]) {
|
|
60
|
+
const next = zoomCamera(camera, factor, point)
|
|
61
|
+
assert.ok(Math.abs((point.x - camera.x) / camera.k - (point.x - next.x) / next.k) < 1e-8)
|
|
62
|
+
assert.ok(Math.abs((point.y - camera.y) / camera.k - (point.y - next.y) / next.k) < 1e-8)
|
|
63
|
+
assert.ok(next.k >= .06 && next.k <= 5)
|
|
64
|
+
}
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
test('labels neither overlap each other nor node obstacles and remain inside canvas', () => {
|
|
68
|
+
const candidates = Array.from({ length: 50 }, (_, i) => ({
|
|
69
|
+
x: 40 + i % 8 * 40, y: 40 + Math.floor(i / 8) * 40, radius: 5, textWidth: 90,
|
|
70
|
+
}))
|
|
71
|
+
const obstacles = candidates.map(c => ({ x: c.x - 7, y: c.y - 7, width: 14, height: 14 }))
|
|
72
|
+
const labels = placeLabels(candidates, obstacles, 360, 360)
|
|
73
|
+
assert.ok(labels.length > 0 && labels.length < candidates.length)
|
|
74
|
+
const intersect = (a, b) => a.x < b.x + b.width && a.x + a.width > b.x && a.y < b.y + b.height && a.y + a.height > b.y
|
|
75
|
+
for (let i = 0; i < labels.length; i++) {
|
|
76
|
+
const a = labels[i].box
|
|
77
|
+
assert.ok(a.x >= 5 && a.x + a.width <= 355 && a.y >= 5 && a.y + a.height <= 355)
|
|
78
|
+
for (const b of obstacles) assert.equal(intersect(a, b), false)
|
|
79
|
+
for (const b of labels.slice(i + 1)) assert.equal(intersect(a, b.box), false)
|
|
80
|
+
}
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
test('pin/unpin/relayout/unpinAll keep the physics state coherent', () => {
|
|
84
|
+
const layout = createForceLayout()
|
|
85
|
+
const data = graph(12)
|
|
86
|
+
layout.update(data); layout.settle(120)
|
|
87
|
+
layout.pin('n000', 321, 123)
|
|
88
|
+
assert.equal(layout.nodes[0].fx, 321); assert.equal(layout.nodes[0].x, 321)
|
|
89
|
+
assert.equal(layout.nodes[0].fy, 123); assert.equal(layout.nodes[0].y, 123)
|
|
90
|
+
layout.tick()
|
|
91
|
+
assert.equal(layout.nodes[0].x, 321, 'a pinned node is never moved by forces')
|
|
92
|
+
layout.update(structuredClone(data))
|
|
93
|
+
assert.equal(layout.nodes[0].x, 321, 'a heartbeat refresh must not drop the pin')
|
|
94
|
+
layout.unpin('n000')
|
|
95
|
+
assert.equal(layout.nodes[0].fx, null); assert.equal(layout.nodes[0].fy, null)
|
|
96
|
+
layout.pin('n001', 10, 10); layout.pin('n002', 20, 20)
|
|
97
|
+
layout.unpinAll()
|
|
98
|
+
assert.ok(layout.nodes.every(node => node.fx == null && node.fy == null))
|
|
99
|
+
layout.relayout(); layout.settle(120)
|
|
100
|
+
assert.ok(layout.nodes.every(node => Number.isFinite(node.x) && Number.isFinite(node.y) && node.fx == null))
|
|
101
|
+
assert.equal(layout.edges.length, 11)
|
|
102
|
+
layout.dispose()
|
|
103
|
+
})
|
|
104
|
+
|
|
105
|
+
test('relayout cools down and separates instead of shoving nodes forever', () => {
|
|
106
|
+
// 「缩到最小 → 重新布局 → 打架」的回归:硬分离阈值以前写成 24 / viewScale,
|
|
107
|
+
// k≈.06 时等于要求 400 世界像素间距,每帧互推、永不收敛。现在只看半径,
|
|
108
|
+
// 物理也拿不到相机(setScale 已删),缩放再小也不会改变力的量级。
|
|
109
|
+
assert.equal('setScale' in createForceLayout(), false, 'the physics must never be handed the camera scale')
|
|
110
|
+
const layout = createForceLayout()
|
|
111
|
+
layout.update(graph(60))
|
|
112
|
+
layout.relayout()
|
|
113
|
+
let previous = Infinity
|
|
114
|
+
for (let window = 0; window < 6; window++) {
|
|
115
|
+
const before = layout.nodes.map(node => [node.x, node.y])
|
|
116
|
+
for (let i = 0; i < 20; i++) layout.tick()
|
|
117
|
+
let maxMove = 0
|
|
118
|
+
layout.nodes.forEach((node, i) => { maxMove = Math.max(maxMove, Math.hypot(node.x - before[i][0], node.y - before[i][1])) })
|
|
119
|
+
assert.ok(maxMove < previous, `window ${window} must move less than the one before (${maxMove} >= ${previous})`)
|
|
120
|
+
previous = maxMove
|
|
121
|
+
}
|
|
122
|
+
assert.ok(previous < 4, `a cooled relayout must stop shoving, saw ${previous}px per window`)
|
|
123
|
+
for (let i = 0; i < layout.nodes.length; i++) for (let j = i + 1; j < layout.nodes.length; j++) {
|
|
124
|
+
const a = layout.nodes[i], b = layout.nodes[j]
|
|
125
|
+
assert.ok(Math.hypot(a.x - b.x, a.y - b.y) > a.radius + b.radius, 'relayout must not leave nodes overlapping')
|
|
126
|
+
}
|
|
127
|
+
layout.dispose()
|
|
128
|
+
})
|
|
129
|
+
|
|
130
|
+
test('configure maps all three force sliders and reheats without ever going non-finite', () => {
|
|
131
|
+
assert.equal(DEFAULT_OPTIONS.center, 0, 'center force is off by default')
|
|
132
|
+
assert.equal(DEFAULT_OPTIONS.clusterScope, true)
|
|
133
|
+
const layout = createForceLayout()
|
|
134
|
+
layout.update(graph(40)); layout.settle(80)
|
|
135
|
+
const cooled = layout.alpha
|
|
136
|
+
layout.configure({ repulsion: 300, distance: 180, center: 2 })
|
|
137
|
+
assert.ok(layout.alpha > cooled)
|
|
138
|
+
for (let i = 0; i < 200; i++) layout.tick()
|
|
139
|
+
assert.ok(layout.nodes.every(node => Number.isFinite(node.x) && Number.isFinite(node.y)))
|
|
140
|
+
layout.dispose()
|
|
141
|
+
})
|
|
142
|
+
|
|
143
|
+
test('drag scope freezes other clusters and repels the dragged one from them', () => {
|
|
144
|
+
// 两簇各 12 节点,链状相连;A 在左,B 在右,互不连通。
|
|
145
|
+
const nodes = [], edges = []
|
|
146
|
+
for (let group = 0; group < 2; group++) for (let i = 0; i < 12; i++) {
|
|
147
|
+
const id = `${group}-${String(i).padStart(2, '0')}`
|
|
148
|
+
nodes.push({ id, title: id, kind: 'memory' })
|
|
149
|
+
if (i) edges.push({ from: `${group}-${String(i - 1).padStart(2, '0')}`, to: id })
|
|
150
|
+
}
|
|
151
|
+
const layout = createForceLayout()
|
|
152
|
+
layout.update({ nodes, edges })
|
|
153
|
+
// 手动把两簇分开(布局原长由首次见到的距离决定,这里覆盖成稳定值)。
|
|
154
|
+
layout.nodes.forEach((node, i) => { node.x = i < 12 ? -260 + i * 8 : 260 + (i - 12) * 8; node.y = i < 12 ? 0 : 40; node.fx = null; node.fy = null })
|
|
155
|
+
layout.edges.forEach(edge => { edge.baseLen = 12 })
|
|
156
|
+
layout.settle(80)
|
|
157
|
+
const frozen = layout.nodes.slice(12).map(node => ({ id: node.id, x: node.x, y: node.y }))
|
|
158
|
+
const dragged = layout.nodes[0]
|
|
159
|
+
const scope = layout.startDragScope(dragged.id)
|
|
160
|
+
assert.ok(scope?.ids.size === 12, 'the drag scope is exactly one connected component')
|
|
161
|
+
assert.ok(!scope.ids.has(layout.nodes[12].id))
|
|
162
|
+
const centerX = frozen.reduce((sum, n) => sum + n.x, 0) / frozen.length
|
|
163
|
+
const centerY = frozen.reduce((sum, n) => sum + n.y, 0) / frozen.length
|
|
164
|
+
// 把整簇硬塞到冻结簇中心:靠单向斥力把活动簇推开,冻结簇自身只当障碍。
|
|
165
|
+
layout.reheat(.9)
|
|
166
|
+
for (let i = 0; i < scope.members.length; i++) {
|
|
167
|
+
const member = scope.members[i]
|
|
168
|
+
if (member === dragged) continue
|
|
169
|
+
member.x = centerX + (i % 4) * 4 - 6
|
|
170
|
+
member.y = centerY + Math.floor(i / 4) * 4 - 6
|
|
171
|
+
member.vx = 0; member.vy = 0
|
|
172
|
+
}
|
|
173
|
+
// 落点放在冻结簇包围盒之外:pin 节点优先跟手,不能被硬分离挪动。
|
|
174
|
+
layout.pin(dragged.id, Math.max(...frozen.map(n => n.x)) + 60, centerY)
|
|
175
|
+
for (let i = 0; i < 80; i++) layout.tick()
|
|
176
|
+
layout.setDragging(false)
|
|
177
|
+
for (let i = 0; i < 20; i++) layout.tick()
|
|
178
|
+
// 100 ticks 内 alpha 仍高于 .05,作用域未解除,其他簇必须一像素都不动。
|
|
179
|
+
for (const before of frozen) {
|
|
180
|
+
const after = layout.nodeById(before.id)
|
|
181
|
+
assert.equal(after.x, before.x, 'frozen clusters must not move at all')
|
|
182
|
+
assert.equal(after.y, before.y)
|
|
183
|
+
}
|
|
184
|
+
let min = Infinity
|
|
185
|
+
for (const a of scope.members) for (const b of frozen) min = Math.min(min, Math.hypot(a.x - b.x, a.y - b.y))
|
|
186
|
+
assert.ok(min > 12, `clusters must not overlap (min distance ${min.toFixed(1)} world px)`)
|
|
187
|
+
layout.dispose()
|
|
188
|
+
})
|
|
189
|
+
|
|
190
|
+
function harness(reducedMotion = true) {
|
|
191
|
+
const handlers = new Map(), windowHandlers = new Map(), frames = new Map(), arcs = [], focuses = [], texts = []
|
|
192
|
+
let id = 0, paints = 0
|
|
193
|
+
const ctx = new Proxy({
|
|
194
|
+
arc(x, y, radius) { arcs.push({ x, y, radius }) },
|
|
195
|
+
measureText(text) { return { width: text.length * 7 } },
|
|
196
|
+
clearRect() { paints++; arcs.length = 0; texts.length = 0 },
|
|
197
|
+
fillText(text) { texts.push(String(text)) },
|
|
198
|
+
}, { get(target, key) { return key in target ? target[key] : () => {} } })
|
|
199
|
+
const win = {
|
|
200
|
+
requestAnimationFrame(fn) { frames.set(++id, fn); return id },
|
|
201
|
+
cancelAnimationFrame(id) { frames.delete(id) },
|
|
202
|
+
addEventListener(type, fn) { windowHandlers.set(type, fn) },
|
|
203
|
+
removeEventListener(type) { windowHandlers.delete(type) },
|
|
204
|
+
getComputedStyle: () => ({ color: '#202020', getPropertyValue: () => '#ffffff' }),
|
|
205
|
+
devicePixelRatio: 2,
|
|
206
|
+
}
|
|
207
|
+
const captures = new Set()
|
|
208
|
+
const canvas = {
|
|
209
|
+
style: {}, ownerDocument: { defaultView: win, documentElement: {} },
|
|
210
|
+
getContext: () => ctx, getBoundingClientRect: () => ({ left: 0, top: 0, width: 600, height: 400 }),
|
|
211
|
+
focus() {},
|
|
212
|
+
setPointerCapture(id) { captures.add(id) },
|
|
213
|
+
hasPointerCapture: id => captures.has(id),
|
|
214
|
+
releasePointerCapture(id) { captures.delete(id) },
|
|
215
|
+
addEventListener(type, fn) { handlers.set(type, fn) },
|
|
216
|
+
removeEventListener(type) { handlers.delete(type) },
|
|
217
|
+
}
|
|
218
|
+
const view = createGraphView(canvas, { onFocus: id => focuses.push(id), reducedMotion })
|
|
219
|
+
const flush = () => {
|
|
220
|
+
const pending = [...frames.values()]; frames.clear()
|
|
221
|
+
for (const fn of pending) fn(1000)
|
|
222
|
+
}
|
|
223
|
+
const emit = (type, x, y, extra = {}) => handlers.get(type)?.({
|
|
224
|
+
type, clientX: x, clientY: y, button: 0, pointerId: 1, pointerType: 'mouse', preventDefault() {}, ...extra,
|
|
225
|
+
})
|
|
226
|
+
return { view, flush, emit, arcs, texts, frames, focuses, handlers, windowHandlers, getPaints: () => paints }
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
test('view snapshots retain multiple clusters and entities beyond the first 200 nodes', () => {
|
|
230
|
+
const data = graph(250, false)
|
|
231
|
+
for (let i = 0; i < 249; i++) {
|
|
232
|
+
if (i !== 119) data.edges.push({ from: data.nodes[i].id, to: data.nodes[i + 1].id })
|
|
233
|
+
}
|
|
234
|
+
for (const [id, from] of [['ent-first', 'n119'], ['ent-last', 'n249']]) {
|
|
235
|
+
data.nodes.push({ id, title: id, kind: 'person', strength: .5 })
|
|
236
|
+
data.edges.push({ from, to: id })
|
|
237
|
+
}
|
|
238
|
+
const h = harness()
|
|
239
|
+
h.view.update(data); h.flush()
|
|
240
|
+
const snapshot = h.view.snapshot()
|
|
241
|
+
assert.equal(snapshot.length, 252)
|
|
242
|
+
assert.deepEqual(new Set(snapshot.map(node => node.id)), new Set(data.nodes.map(node => node.id)))
|
|
243
|
+
for (const id of ['ent-first', 'ent-last']) {
|
|
244
|
+
const entity = snapshot.find(node => node.id === id)
|
|
245
|
+
assert.equal(entity.degree, 1, 'late entity links must survive canvas ingestion')
|
|
246
|
+
assert.ok(Number.isFinite(entity.x) && Number.isFinite(entity.y))
|
|
247
|
+
}
|
|
248
|
+
h.view.update({ nodes: [...data.nodes, data.nodes[0]].reverse(), edges: [...data.edges].reverse() }); h.flush()
|
|
249
|
+
assert.deepEqual(h.view.snapshot(), snapshot, 'equivalent refreshed input preserves every node and its position')
|
|
250
|
+
h.view.dispose()
|
|
251
|
+
})
|
|
252
|
+
|
|
253
|
+
test('the detailed layer actually paints labels and the label switch silences them', () => {
|
|
254
|
+
const h = harness()
|
|
255
|
+
h.view.update(graph(10)); h.flush()
|
|
256
|
+
assert.ok(h.texts.length > 0, 'labels must reach fillText (radius/textWidth must be finite)')
|
|
257
|
+
assert.ok(h.texts.length <= 10, 'labels must never be painted more often than there are nodes')
|
|
258
|
+
h.view.configure({ labels: false }); h.flush()
|
|
259
|
+
assert.equal(h.texts.length, 0)
|
|
260
|
+
h.view.dispose()
|
|
261
|
+
})
|
|
262
|
+
|
|
263
|
+
test('left drag never locks the node; right click toggles the pin; refresh preserves position', () => {
|
|
264
|
+
const h = harness()
|
|
265
|
+
h.view.update(graph(1)); h.flush()
|
|
266
|
+
const p = { ...h.arcs[0] }
|
|
267
|
+
h.emit('pointerdown', p.x, p.y)
|
|
268
|
+
h.emit('pointermove', p.x + 70, p.y + 25)
|
|
269
|
+
h.view.update(graph(1))
|
|
270
|
+
h.flush()
|
|
271
|
+
assert.equal(h.arcs[0].x, p.x + 70); assert.equal(h.arcs[0].y, p.y + 25)
|
|
272
|
+
h.emit('pointerup', p.x + 70, p.y + 25); h.flush()
|
|
273
|
+
assert.deepEqual(h.focuses, [], 'a drag must never focus/refetch')
|
|
274
|
+
assert.equal(h.view.isPinned('n000'), false, 'a left drag must not pin the node')
|
|
275
|
+
// 松手后位置留在落点,心跳刷新不把它拽回。
|
|
276
|
+
for (let i = 0; i < 3; i++) { h.view.update(graph(1)); h.flush() }
|
|
277
|
+
assert.equal(h.arcs[0].x, p.x + 70); assert.equal(h.arcs[0].y, p.y + 25)
|
|
278
|
+
h.emit('pointerdown', p.x + 70, p.y + 25)
|
|
279
|
+
h.emit('pointerup', p.x + 70, p.y + 25)
|
|
280
|
+
assert.deepEqual(h.focuses, ['n000'], 'a click without movement still focuses')
|
|
281
|
+
// 右键 = 锁定 / 解除,左键从不产生固定点。
|
|
282
|
+
h.emit('contextmenu', p.x + 70, p.y + 25); h.flush()
|
|
283
|
+
assert.equal(h.view.isPinned('n000'), true, 'right click pins the node')
|
|
284
|
+
assert.equal(h.view.pinnedCount(), 1)
|
|
285
|
+
assert.equal(h.arcs[0].x, p.x + 70, 'pinning must not teleport the node')
|
|
286
|
+
h.emit('contextmenu', p.x + 70, p.y + 25); h.flush()
|
|
287
|
+
assert.equal(h.view.isPinned('n000'), false, 'right click again unpins')
|
|
288
|
+
assert.equal(h.view.pinnedCount(), 0)
|
|
289
|
+
h.view.dispose()
|
|
290
|
+
assert.equal(h.handlers.size, 0); assert.equal(h.frames.size, 0); assert.equal(h.windowHandlers.size, 0)
|
|
291
|
+
})
|
|
292
|
+
|
|
293
|
+
test('dragging an already pinned node keeps the lock at the new position', () => {
|
|
294
|
+
const h = harness()
|
|
295
|
+
h.view.update(graph(1)); h.flush()
|
|
296
|
+
const p = { ...h.arcs[0] }
|
|
297
|
+
h.emit('contextmenu', p.x, p.y); h.flush()
|
|
298
|
+
assert.equal(h.view.isPinned('n000'), true)
|
|
299
|
+
h.emit('pointerdown', p.x, p.y)
|
|
300
|
+
h.emit('pointermove', p.x + 40, p.y + 10)
|
|
301
|
+
h.emit('pointerup', p.x + 40, p.y + 10); h.flush()
|
|
302
|
+
assert.equal(h.view.isPinned('n000'), true, 'an existing lock survives a left drag')
|
|
303
|
+
assert.equal(h.arcs[0].x, p.x + 40)
|
|
304
|
+
h.view.dispose()
|
|
305
|
+
})
|
|
306
|
+
|
|
307
|
+
test('pan/zoom and equivalent refresh never re-fit the camera; cancelled pointer never clicks', () => {
|
|
308
|
+
const h = harness()
|
|
309
|
+
h.view.update(graph(1)); h.flush()
|
|
310
|
+
const before = { ...h.arcs[0] }
|
|
311
|
+
h.emit('pointerdown', 30, 30); h.emit('pointermove', 70, 60); h.emit('pointerup', 70, 60); h.flush()
|
|
312
|
+
assert.equal(h.arcs[0].x, before.x + 40); assert.equal(h.arcs[0].y, before.y + 30)
|
|
313
|
+
h.view.zoom(.8); h.flush()
|
|
314
|
+
const zoomed = { ...h.arcs[0] }
|
|
315
|
+
h.view.update(graph(1)); h.flush()
|
|
316
|
+
assert.deepEqual(h.arcs[0], zoomed)
|
|
317
|
+
h.emit('pointerdown', zoomed.x, zoomed.y); h.emit('pointercancel', zoomed.x, zoomed.y)
|
|
318
|
+
assert.deepEqual(h.focuses, [])
|
|
319
|
+
h.view.dispose()
|
|
320
|
+
})
|
|
321
|
+
|
|
322
|
+
test('dense hit targets choose the closest node, not the last node in draw order', () => {
|
|
323
|
+
const h = harness()
|
|
324
|
+
h.view.update(graph(2)); h.view.zoom(.04); h.flush()
|
|
325
|
+
const p = h.arcs[0], other = h.arcs[1]
|
|
326
|
+
assert.ok(Math.hypot(other.x - p.x, other.y - p.y) < 10)
|
|
327
|
+
h.emit('pointerdown', p.x, p.y); h.emit('pointerup', p.x, p.y)
|
|
328
|
+
assert.deepEqual(h.focuses, ['n000'])
|
|
329
|
+
h.view.dispose()
|
|
330
|
+
})
|
|
331
|
+
|
|
332
|
+
test('pinch does not trigger focus and reduced-motion/paused views do not run an idle animation loop', () => {
|
|
333
|
+
const h = harness()
|
|
334
|
+
h.view.update(graph(10)); h.flush()
|
|
335
|
+
assert.equal(h.frames.size, 0, 'reduced motion must not keep a rAF loop alive')
|
|
336
|
+
const p = h.arcs[0]
|
|
337
|
+
h.emit('pointerdown', p.x, p.y, { pointerType: 'touch' })
|
|
338
|
+
h.emit('pointerdown', p.x + 40, p.y, { pointerType: 'touch', pointerId: 2 })
|
|
339
|
+
h.emit('pointermove', p.x + 80, p.y, { pointerType: 'touch', pointerId: 2 })
|
|
340
|
+
h.emit('pointerup', p.x, p.y)
|
|
341
|
+
h.emit('pointerup', p.x + 80, p.y, { pointerId: 2 })
|
|
342
|
+
assert.deepEqual(h.focuses, [])
|
|
343
|
+
h.view.dispose()
|
|
344
|
+
const paused = harness(false)
|
|
345
|
+
paused.view.configure({ motion: false }); paused.view.update(graph(10)); paused.flush()
|
|
346
|
+
assert.equal(paused.frames.size, 0, 'paused motion must not keep a rAF loop alive')
|
|
347
|
+
paused.view.dispose()
|
|
348
|
+
})
|
|
349
|
+
|
|
350
|
+
// GPU 守卫:动效开着也不能在冷却后空转重绘(曾经的实现是 30fps 永久整幅重绘)。
|
|
351
|
+
test('a cooled layout stops requesting frames even while motion is enabled', () => {
|
|
352
|
+
const h = harness(false)
|
|
353
|
+
h.view.update(graph(60))
|
|
354
|
+
for (let i = 0; i < 900 && h.frames.size; i++) h.flush()
|
|
355
|
+
assert.equal(h.frames.size, 0, 'a settled layout must not keep a rAF loop alive')
|
|
356
|
+
const painted = h.getPaints()
|
|
357
|
+
for (let i = 0; i < 200; i++) h.flush()
|
|
358
|
+
assert.equal(h.getPaints(), painted, 'idle time must not repaint the canvas')
|
|
359
|
+
h.view.zoom(1.2)
|
|
360
|
+
assert.equal(h.frames.size, 1, 'an interaction schedules exactly one repaint')
|
|
361
|
+
h.flush()
|
|
362
|
+
assert.equal(h.frames.size, 0, 'that repaint must not restart the loop')
|
|
363
|
+
h.view.dispose()
|
|
364
|
+
})
|
|
365
|
+
|
|
366
|
+
// 极小缩放下两颗节点可能只差 2-3px:纯最近邻会随鼠标 1px 抖动来回换人,
|
|
367
|
+
// 高亮反复切换就是用户看到的「节点互相打架闪现」。
|
|
368
|
+
test('hover keeps its target through 1px jitter at minimum zoom', () => {
|
|
369
|
+
const h = harness()
|
|
370
|
+
h.view.update(graph(2)); h.flush()
|
|
371
|
+
h.view.zoom(.05); h.flush()
|
|
372
|
+
const p = { ...h.arcs[0] }
|
|
373
|
+
const before = h.getPaints()
|
|
374
|
+
h.emit('pointermove', p.x, p.y); h.flush()
|
|
375
|
+
const first = h.getPaints()
|
|
376
|
+
assert.ok(first > before, 'hovering a node must repaint once')
|
|
377
|
+
h.emit('pointermove', p.x + 1, p.y + 1); h.flush()
|
|
378
|
+
assert.equal(h.getPaints(), first, 'a 1px nudge must not flip the hover target')
|
|
379
|
+
h.view.dispose()
|
|
380
|
+
})
|
|
381
|
+
|
|
382
|
+
test('Escape cancels a drag and clears the focus; toolbar actions do not throw', () => {
|
|
383
|
+
const h = harness()
|
|
384
|
+
h.view.update(graph(3)); h.flush()
|
|
385
|
+
const p = { ...h.arcs[0] }
|
|
386
|
+
h.emit('pointerdown', p.x, p.y)
|
|
387
|
+
h.emit('pointermove', p.x + 50, p.y + 30)
|
|
388
|
+
h.emit('keydown', p.x, p.y, { key: 'Escape' })
|
|
389
|
+
h.flush()
|
|
390
|
+
assert.deepEqual(h.focuses, [''])
|
|
391
|
+
h.view.unpinAll(); h.view.relayout(); h.view.configure({ center: 1, repulsion: 200, distance: 140 })
|
|
392
|
+
h.flush()
|
|
393
|
+
assert.ok(h.arcs.every(arc => Number.isFinite(arc.x) && Number.isFinite(arc.y)))
|
|
394
|
+
h.view.dispose()
|
|
395
|
+
})
|
|
396
|
+
|
|
397
|
+
test('shipped browser bundle exposes the controller with no runtime npm imports', () => {
|
|
398
|
+
const source = fs.readFileSync(new URL('../lib/webui/graph-view.bundle.js', import.meta.url), 'utf8')
|
|
399
|
+
assert.doesNotMatch(source, /d3-force|forceSimulation|forceManyBody|require\(/)
|
|
400
|
+
const scope = { setTimeout, clearTimeout, setInterval, clearInterval, performance, Date, Math, Map, Set, JSON, console }
|
|
401
|
+
vm.runInNewContext(source, scope)
|
|
402
|
+
const api = scope.DshMemuGraph
|
|
403
|
+
assert.equal(typeof api.createGraphView, 'function')
|
|
404
|
+
assert.equal(typeof api.createForceLayout, 'function')
|
|
405
|
+
assert.equal(typeof api.fitCamera, 'function')
|
|
406
|
+
assert.equal(typeof api.zoomCamera, 'function')
|
|
407
|
+
assert.equal(typeof api.placeLabels, 'function')
|
|
408
|
+
assert.equal(api.DEFAULT_OPTIONS.center, 0)
|
|
409
|
+
assert.equal(api.DEFAULT_OPTIONS.clusterScope, true)
|
|
410
|
+
const layout = api.createForceLayout()
|
|
411
|
+
layout.update(graph(251, false))
|
|
412
|
+
assert.equal(layout.nodes.length, 251, 'the shipped browser bundle must retain nodes beyond 200')
|
|
413
|
+
layout.dispose()
|
|
414
|
+
})
|