@logictan/dsh-config-manager 0.1.60
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/LICENSE +21 -0
- package/README.md +105 -0
- package/cordis.patch.yml +21 -0
- package/lib/adapters/agent-instructions.d.ts +13 -0
- package/lib/adapters/agent-instructions.js +46 -0
- package/lib/adapters/agent-presets.d.ts +13 -0
- package/lib/adapters/agent-presets.js +14 -0
- package/lib/adapters/credentials.d.ts +37 -0
- package/lib/adapters/credentials.js +122 -0
- package/lib/adapters/file-collection.d.ts +15 -0
- package/lib/adapters/file-collection.js +127 -0
- package/lib/adapters/index.d.ts +57 -0
- package/lib/adapters/index.js +55 -0
- package/lib/adapters/link-report.d.ts +16 -0
- package/lib/adapters/link-report.js +47 -0
- package/lib/adapters/mcp.d.ts +28 -0
- package/lib/adapters/mcp.js +178 -0
- package/lib/adapters/plugin-files.d.ts +18 -0
- package/lib/adapters/plugin-files.js +142 -0
- package/lib/adapters/plugins.d.ts +41 -0
- package/lib/adapters/plugins.js +603 -0
- package/lib/adapters/pnpm-workspace.d.ts +26 -0
- package/lib/adapters/pnpm-workspace.js +147 -0
- package/lib/adapters/prompts.d.ts +30 -0
- package/lib/adapters/prompts.js +231 -0
- package/lib/adapters/providers.d.ts +34 -0
- package/lib/adapters/providers.js +155 -0
- package/lib/adapters/self.d.ts +17 -0
- package/lib/adapters/self.js +71 -0
- package/lib/adapters/sessions.d.ts +14 -0
- package/lib/adapters/sessions.js +15 -0
- package/lib/adapters/settings.d.ts +28 -0
- package/lib/adapters/settings.js +165 -0
- package/lib/adapters/skills.d.ts +13 -0
- package/lib/adapters/skills.js +14 -0
- package/lib/adapters/test-helpers.d.ts +125 -0
- package/lib/adapters/test-helpers.js +236 -0
- package/lib/adapters/ui.d.ts +27 -0
- package/lib/adapters/ui.js +89 -0
- package/lib/adapters/workspaces.d.ts +13 -0
- package/lib/adapters/workspaces.js +106 -0
- package/lib/client.d.ts +1026 -0
- package/lib/client.js +10047 -0
- package/lib/core/analyzer.d.ts +99 -0
- package/lib/core/analyzer.js +934 -0
- package/lib/core/backup.d.ts +138 -0
- package/lib/core/backup.js +513 -0
- package/lib/core/boot-rescue.d.ts +146 -0
- package/lib/core/boot-rescue.js +618 -0
- package/lib/core/cache-cleaner.d.ts +31 -0
- package/lib/core/cache-cleaner.js +95 -0
- package/lib/core/config-lifecycle.d.ts +180 -0
- package/lib/core/config-lifecycle.js +481 -0
- package/lib/core/config-snapshot.d.ts +135 -0
- package/lib/core/config-snapshot.js +424 -0
- package/lib/core/config-state.d.ts +70 -0
- package/lib/core/config-state.js +177 -0
- package/lib/core/consult-source.d.ts +58 -0
- package/lib/core/consult-source.js +260 -0
- package/lib/core/crash-report.d.ts +84 -0
- package/lib/core/crash-report.js +317 -0
- package/lib/core/exporter.d.ts +68 -0
- package/lib/core/exporter.js +375 -0
- package/lib/core/ghost-sweep.d.ts +19 -0
- package/lib/core/ghost-sweep.js +52 -0
- package/lib/core/importer.d.ts +60 -0
- package/lib/core/importer.js +29 -0
- package/lib/core/index.d.ts +14 -0
- package/lib/core/index.js +14 -0
- package/lib/core/journal.d.ts +197 -0
- package/lib/core/journal.js +418 -0
- package/lib/core/local-plugin-host.d.ts +35 -0
- package/lib/core/local-plugin-host.js +120 -0
- package/lib/core/local-plugin-pack.d.ts +135 -0
- package/lib/core/local-plugin-pack.js +278 -0
- package/lib/core/messages.d.ts +310 -0
- package/lib/core/messages.js +621 -0
- package/lib/core/migration-consult.d.ts +117 -0
- package/lib/core/migration-consult.js +363 -0
- package/lib/core/migration-history.d.ts +153 -0
- package/lib/core/migration-history.js +411 -0
- package/lib/core/model-tools.d.ts +50 -0
- package/lib/core/model-tools.js +148 -0
- package/lib/core/msg-types.d.ts +8 -0
- package/lib/core/msg-types.js +6 -0
- package/lib/core/phase3-child-crash.d.ts +1 -0
- package/lib/core/phase3-child-crash.js +87 -0
- package/lib/core/phase3-host.d.ts +138 -0
- package/lib/core/phase3-host.js +359 -0
- package/lib/core/phase3-prod-child.d.ts +1 -0
- package/lib/core/phase3-prod-child.js +35 -0
- package/lib/core/phase4-crash-child.d.ts +1 -0
- package/lib/core/phase4-crash-child.js +103 -0
- package/lib/core/plugin-cli.d.ts +150 -0
- package/lib/core/plugin-cli.js +497 -0
- package/lib/core/reconcile.d.ts +120 -0
- package/lib/core/reconcile.js +389 -0
- package/lib/core/recovery-orchestrator.d.ts +94 -0
- package/lib/core/recovery-orchestrator.js +327 -0
- package/lib/core/restore.d.ts +162 -0
- package/lib/core/restore.js +703 -0
- package/lib/core/rollback.d.ts +12 -0
- package/lib/core/rollback.js +145 -0
- package/lib/core/run-registry.d.ts +85 -0
- package/lib/core/run-registry.js +157 -0
- package/lib/core/startup-barrier.d.ts +53 -0
- package/lib/core/startup-barrier.js +77 -0
- package/lib/core/types.d.ts +515 -0
- package/lib/core/types.js +27 -0
- package/lib/core/undo.d.ts +73 -0
- package/lib/core/undo.js +87 -0
- package/lib/core/validator.d.ts +18 -0
- package/lib/core/validator.js +77 -0
- package/lib/core/verify-recovery.d.ts +21 -0
- package/lib/core/verify-recovery.js +256 -0
- package/lib/core/watcher.d.ts +101 -0
- package/lib/core/watcher.js +219 -0
- package/lib/index.d.ts +238 -0
- package/lib/index.js +2764 -0
- package/lib/migrations/index.d.ts +29 -0
- package/lib/migrations/index.js +56 -0
- package/lib/migrations/v1-to-v2.d.ts +9 -0
- package/lib/migrations/v1-to-v2.js +10 -0
- package/lib/profiles/index.d.ts +4 -0
- package/lib/profiles/index.js +5 -0
- package/lib/profiles/profile-manager.d.ts +106 -0
- package/lib/profiles/profile-manager.js +543 -0
- package/lib/schema/config.d.ts +28 -0
- package/lib/schema/config.js +165 -0
- package/lib/schema/index.d.ts +24 -0
- package/lib/schema/index.js +26 -0
- package/lib/schema/manifest.d.ts +28 -0
- package/lib/schema/manifest.js +109 -0
- package/lib/schema/tombstones.d.ts +46 -0
- package/lib/schema/tombstones.js +64 -0
- package/lib/schema/types.d.ts +232 -0
- package/lib/schema/types.js +8 -0
- package/lib/schema/versions.d.ts +27 -0
- package/lib/schema/versions.js +50 -0
- package/lib/security/encryption.d.ts +85 -0
- package/lib/security/encryption.js +279 -0
- package/lib/security/index.d.ts +16 -0
- package/lib/security/index.js +17 -0
- package/lib/security/integrity.d.ts +30 -0
- package/lib/security/integrity.js +78 -0
- package/lib/security/redaction.d.ts +26 -0
- package/lib/security/redaction.js +95 -0
- package/lib/security/secret-scanner.d.ts +86 -0
- package/lib/security/secret-scanner.js +404 -0
- package/lib/security/vault.d.ts +59 -0
- package/lib/security/vault.js +151 -0
- package/lib/security/zip-security.d.ts +27 -0
- package/lib/security/zip-security.js +154 -0
- package/lib/sync/autosync-config.d.ts +39 -0
- package/lib/sync/autosync-config.js +151 -0
- package/lib/sync/autosync-scheduler.d.ts +139 -0
- package/lib/sync/autosync-scheduler.js +564 -0
- package/lib/sync/fs.d.ts +18 -0
- package/lib/sync/fs.js +49 -0
- package/lib/sync/git/git-transport.d.ts +100 -0
- package/lib/sync/git/git-transport.js +437 -0
- package/lib/sync/github-auth.d.ts +131 -0
- package/lib/sync/github-auth.js +264 -0
- package/lib/sync/layout.d.ts +43 -0
- package/lib/sync/layout.js +203 -0
- package/lib/sync/snapshot-json.d.ts +9 -0
- package/lib/sync/snapshot-json.js +107 -0
- package/lib/sync/sync-config.d.ts +81 -0
- package/lib/sync/sync-config.js +281 -0
- package/lib/sync/sync-engine.d.ts +269 -0
- package/lib/sync/sync-engine.js +591 -0
- package/lib/sync/sync-history.d.ts +42 -0
- package/lib/sync/sync-history.js +78 -0
- package/lib/sync/sync-selection.d.ts +29 -0
- package/lib/sync/sync-selection.js +127 -0
- package/lib/sync/sync-session.d.ts +40 -0
- package/lib/sync/sync-session.js +54 -0
- package/lib/sync/sync-state.d.ts +37 -0
- package/lib/sync/sync-state.js +108 -0
- package/lib/sync/transport.d.ts +75 -0
- package/lib/sync/transport.js +55 -0
- package/lib/sync/ui-prefs.d.ts +34 -0
- package/lib/sync/ui-prefs.js +109 -0
- package/lib/sync/webdav/webdav-transport.d.ts +77 -0
- package/lib/sync/webdav/webdav-transport.js +451 -0
- package/lib/ui/errors.d.ts +19 -0
- package/lib/ui/errors.js +81 -0
- package/lib/ui/export-flow.d.ts +82 -0
- package/lib/ui/export-flow.js +98 -0
- package/lib/ui/i18n.d.ts +289 -0
- package/lib/ui/i18n.js +581 -0
- package/lib/ui/migration-consult-view.d.ts +91 -0
- package/lib/ui/migration-consult-view.js +115 -0
- package/lib/ui/progress.d.ts +38 -0
- package/lib/ui/progress.js +87 -0
- package/lib/ui/report.d.ts +23 -0
- package/lib/ui/report.js +153 -0
- package/lib/ui/test-helpers.d.ts +69 -0
- package/lib/ui/test-helpers.js +154 -0
- package/lib/ui/types.d.ts +257 -0
- package/lib/ui/types.js +21 -0
- package/lib/utils/atomic-write.d.ts +88 -0
- package/lib/utils/atomic-write.js +399 -0
- package/lib/utils/env-lock.d.ts +393 -0
- package/lib/utils/env-lock.js +930 -0
- package/lib/utils/hashing.d.ts +17 -0
- package/lib/utils/hashing.js +41 -0
- package/lib/utils/json.d.ts +24 -0
- package/lib/utils/json.js +76 -0
- package/lib/utils/logger.d.ts +39 -0
- package/lib/utils/logger.js +82 -0
- package/lib/utils/paths.d.ts +42 -0
- package/lib/utils/paths.js +189 -0
- package/lib/utils/proxy.d.ts +69 -0
- package/lib/utils/proxy.js +455 -0
- package/lib/utils/recursive-walk.d.ts +24 -0
- package/lib/utils/recursive-walk.js +134 -0
- package/lib/utils/zip.d.ts +54 -0
- package/lib/utils/zip.js +285 -0
- package/package.json +188 -0
- package/src/adapters/agent-instructions.ts +48 -0
- package/src/adapters/agent-presets.ts +14 -0
- package/src/adapters/credentials.test.ts +90 -0
- package/src/adapters/credentials.ts +143 -0
- package/src/adapters/file-collection.ts +137 -0
- package/src/adapters/files.test.ts +353 -0
- package/src/adapters/index.ts +94 -0
- package/src/adapters/link-report.test.ts +65 -0
- package/src/adapters/link-report.ts +61 -0
- package/src/adapters/mcp.test.ts +96 -0
- package/src/adapters/mcp.ts +185 -0
- package/src/adapters/plugin-files.ts +146 -0
- package/src/adapters/plugins.test.ts +326 -0
- package/src/adapters/plugins.ts +615 -0
- package/src/adapters/pnpm-workspace.test.ts +87 -0
- package/src/adapters/pnpm-workspace.ts +150 -0
- package/src/adapters/prompts.test.ts +110 -0
- package/src/adapters/prompts.ts +230 -0
- package/src/adapters/providers.test.ts +126 -0
- package/src/adapters/providers.ts +173 -0
- package/src/adapters/roundtrip.test.ts +190 -0
- package/src/adapters/self.test.ts +92 -0
- package/src/adapters/self.ts +75 -0
- package/src/adapters/sessions.ts +15 -0
- package/src/adapters/settings.test.ts +100 -0
- package/src/adapters/settings.ts +187 -0
- package/src/adapters/skills.ts +14 -0
- package/src/adapters/test-helpers.ts +248 -0
- package/src/adapters/ui.test.ts +57 -0
- package/src/adapters/ui.ts +108 -0
- package/src/adapters/workspaces.test.ts +106 -0
- package/src/adapters/workspaces.ts +109 -0
- package/src/client/ConfigManagerSection.tsx +90 -0
- package/src/client/api.ts +66 -0
- package/src/client/client-types.ts +18 -0
- package/src/client/common/ErrorBanner.tsx +73 -0
- package/src/client/common/Icon.tsx +129 -0
- package/src/client/common/Modal.tsx +173 -0
- package/src/client/common/ToastViewport.tsx +73 -0
- package/src/client/common/toast-store.test.ts +192 -0
- package/src/client/common/toast-store.ts +181 -0
- package/src/client/common/toast-types.ts +7 -0
- package/src/client/common/ui.tsx +342 -0
- package/src/client/config-manager.module.css +2971 -0
- package/src/client/css-modules.d.ts +8 -0
- package/src/client/index.ts +65 -0
- package/src/client/locales.ts +56 -0
- package/src/client/lucide-icons.d.ts +38 -0
- package/src/client/run-store.test.ts +223 -0
- package/src/client/run-store.ts +372 -0
- package/src/client/sync/SyncConfirmView.tsx +379 -0
- package/src/client/sync/SyncHistoryView.test.ts +40 -0
- package/src/client/sync/SyncHistoryView.tsx +220 -0
- package/src/client/sync/SyncSettingsView.tsx +1535 -0
- package/src/client/sync/history-model.test.ts +194 -0
- package/src/client/sync/history-model.ts +195 -0
- package/src/client/sync/sync-api.test.ts +585 -0
- package/src/client/sync/sync-api.ts +543 -0
- package/src/client/sync/sync-locales.ts +416 -0
- package/src/client/sync/sync-push-preview.test.ts +52 -0
- package/src/client/sync/sync-view-v2.test.ts +213 -0
- package/src/client/sync/sync-view.test.ts +487 -0
- package/src/client/sync/sync-view.ts +759 -0
- package/src/core/analyzer.ts +1068 -0
- package/src/core/backup.ts +605 -0
- package/src/core/boot-rescue.test.ts +847 -0
- package/src/core/boot-rescue.ts +737 -0
- package/src/core/cache-cleaner.test.ts +169 -0
- package/src/core/cache-cleaner.ts +121 -0
- package/src/core/config-lifecycle.test.ts +727 -0
- package/src/core/config-lifecycle.ts +573 -0
- package/src/core/config-snapshot.test.ts +369 -0
- package/src/core/config-snapshot.ts +522 -0
- package/src/core/config-state.test.ts +381 -0
- package/src/core/config-state.ts +207 -0
- package/src/core/consult-source.test.ts +199 -0
- package/src/core/consult-source.ts +293 -0
- package/src/core/crash-report.test.ts +380 -0
- package/src/core/crash-report.ts +369 -0
- package/src/core/exporter.ts +417 -0
- package/src/core/ghost-sweep.test.ts +94 -0
- package/src/core/ghost-sweep.ts +67 -0
- package/src/core/importer.ts +83 -0
- package/src/core/index.ts +56 -0
- package/src/core/journal.test.ts +257 -0
- package/src/core/journal.ts +518 -0
- package/src/core/local-plugin-host.ts +152 -0
- package/src/core/local-plugin-pack.test.ts +359 -0
- package/src/core/local-plugin-pack.ts +363 -0
- package/src/core/messages.test.ts +54 -0
- package/src/core/messages.ts +664 -0
- package/src/core/migration-consult.test.ts +315 -0
- package/src/core/migration-consult.ts +494 -0
- package/src/core/migration-history.test.ts +319 -0
- package/src/core/migration-history.ts +484 -0
- package/src/core/model-tools.test.ts +190 -0
- package/src/core/model-tools.ts +208 -0
- package/src/core/msg-types.ts +10 -0
- package/src/core/phase3-child-crash.ts +95 -0
- package/src/core/phase3-host.ts +411 -0
- package/src/core/phase3-p1.test.ts +212 -0
- package/src/core/phase3-p2b-fingerprint.test.ts +189 -0
- package/src/core/phase3-prod-child.ts +36 -0
- package/src/core/phase3-production-integration.test.ts +286 -0
- package/src/core/phase4-crash-child.ts +101 -0
- package/src/core/phase4-crash-injection.test.ts +166 -0
- package/src/core/plugin-cli.fs.test.ts +184 -0
- package/src/core/plugin-cli.test.ts +137 -0
- package/src/core/plugin-cli.ts +563 -0
- package/src/core/reconcile.test.ts +392 -0
- package/src/core/reconcile.ts +498 -0
- package/src/core/recovery-orchestrator.test.ts +145 -0
- package/src/core/recovery-orchestrator.ts +366 -0
- package/src/core/restore-manage.test.ts +94 -0
- package/src/core/restore.ts +849 -0
- package/src/core/rollback.ts +151 -0
- package/src/core/run-progress.test.ts +237 -0
- package/src/core/run-registry.test.ts +232 -0
- package/src/core/run-registry.ts +209 -0
- package/src/core/smoke.test.ts +842 -0
- package/src/core/startup-barrier.ts +99 -0
- package/src/core/types.ts +542 -0
- package/src/core/undo.ts +116 -0
- package/src/core/validator.ts +84 -0
- package/src/core/verify-recovery.ts +265 -0
- package/src/core/watcher.test.ts +326 -0
- package/src/core/watcher.ts +263 -0
- package/src/index.facade.test.ts +265 -0
- package/src/index.sync.test.ts +217 -0
- package/src/index.ts +2975 -0
- package/src/migrations/index.ts +71 -0
- package/src/migrations/v1-to-v2.ts +17 -0
- package/src/profiles/index.ts +8 -0
- package/src/profiles/profile-manager.test.ts +282 -0
- package/src/profiles/profile-manager.ts +630 -0
- package/src/schema/config.ts +165 -0
- package/src/schema/index.ts +71 -0
- package/src/schema/manifest-schema.test.ts +519 -0
- package/src/schema/manifest.ts +124 -0
- package/src/schema/tombstones.test.ts +131 -0
- package/src/schema/tombstones.ts +89 -0
- package/src/schema/types.ts +245 -0
- package/src/schema/versions.ts +55 -0
- package/src/security/encryption.ts +335 -0
- package/src/security/index.ts +16 -0
- package/src/security/integrity.ts +102 -0
- package/src/security/redaction.ts +101 -0
- package/src/security/secret-scanner.ts +445 -0
- package/src/security/security.test.ts +1215 -0
- package/src/security/vault.test.ts +164 -0
- package/src/security/vault.ts +198 -0
- package/src/security/zip-security.ts +180 -0
- package/src/sync/autosync-config.test.ts +147 -0
- package/src/sync/autosync-config.ts +179 -0
- package/src/sync/autosync-scheduler.test.ts +490 -0
- package/src/sync/autosync-scheduler.ts +646 -0
- package/src/sync/fs.ts +48 -0
- package/src/sync/git/git-transport.test.ts +673 -0
- package/src/sync/git/git-transport.ts +491 -0
- package/src/sync/github-auth.test.ts +280 -0
- package/src/sync/github-auth.ts +356 -0
- package/src/sync/layout.test.ts +270 -0
- package/src/sync/layout.ts +222 -0
- package/src/sync/snapshot-json.test.ts +119 -0
- package/src/sync/snapshot-json.ts +128 -0
- package/src/sync/sync-config.test.ts +310 -0
- package/src/sync/sync-config.ts +299 -0
- package/src/sync/sync-engine.test.ts +1074 -0
- package/src/sync/sync-engine.ts +754 -0
- package/src/sync/sync-history.test.ts +116 -0
- package/src/sync/sync-history.ts +120 -0
- package/src/sync/sync-selection.test.ts +186 -0
- package/src/sync/sync-selection.ts +143 -0
- package/src/sync/sync-session.test.ts +143 -0
- package/src/sync/sync-session.ts +77 -0
- package/src/sync/sync-state.test.ts +164 -0
- package/src/sync/sync-state.ts +131 -0
- package/src/sync/transport.test.ts +111 -0
- package/src/sync/transport.ts +118 -0
- package/src/sync/ui-prefs.test.ts +152 -0
- package/src/sync/ui-prefs.ts +135 -0
- package/src/sync/webdav/webdav-transport.test.ts +871 -0
- package/src/sync/webdav/webdav-transport.ts +535 -0
- package/src/ui/errors.test.ts +47 -0
- package/src/ui/errors.ts +100 -0
- package/src/ui/export-flow.test.ts +87 -0
- package/src/ui/export-flow.ts +156 -0
- package/src/ui/i18n.ts +590 -0
- package/src/ui/migration-consult-view.test.ts +151 -0
- package/src/ui/migration-consult-view.ts +178 -0
- package/src/ui/progress.test.ts +47 -0
- package/src/ui/progress.ts +102 -0
- package/src/ui/report.test.ts +101 -0
- package/src/ui/report.ts +146 -0
- package/src/ui/test-helpers.ts +184 -0
- package/src/ui/types.ts +325 -0
- package/src/utils/atomic-write.test.ts +312 -0
- package/src/utils/atomic-write.ts +422 -0
- package/src/utils/env-lock.test.ts +999 -0
- package/src/utils/env-lock.ts +1086 -0
- package/src/utils/hashing.ts +53 -0
- package/src/utils/json.ts +77 -0
- package/src/utils/logger.ts +114 -0
- package/src/utils/paths.ts +176 -0
- package/src/utils/proxy.test.ts +465 -0
- package/src/utils/proxy.ts +493 -0
- package/src/utils/recursive-walk.test.ts +148 -0
- package/src/utils/recursive-walk.ts +145 -0
- package/src/utils/zip.ts +332 -0
|
@@ -0,0 +1,490 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AutoSyncScheduler 测试:interval 换算、shouldTriggerStartupRun 阈值、
|
|
3
|
+
* enabled=false 不执行、连续失败通知、冲突跳过。
|
|
4
|
+
*
|
|
5
|
+
* 采用真实 RunRegistry + 注入 readConfig/writeConfig/readSyncConfigFn/readHistoryFn/
|
|
6
|
+
* appendHistoryFn/makeSyncEngine/now,全程不触碰真实网络与真实定时器。
|
|
7
|
+
*/
|
|
8
|
+
import test from 'node:test';
|
|
9
|
+
import assert from 'node:assert/strict';
|
|
10
|
+
import fs from 'node:fs/promises';
|
|
11
|
+
import os from 'node:os';
|
|
12
|
+
import path from 'node:path';
|
|
13
|
+
|
|
14
|
+
import {
|
|
15
|
+
AutoSyncScheduler, intervalToMs, shouldTriggerStartupRun,
|
|
16
|
+
} from './autosync-scheduler.ts';
|
|
17
|
+
import { RunRegistry } from '../core/run-registry.ts';
|
|
18
|
+
import { nullLogger, createLogger } from '../utils/logger.ts';
|
|
19
|
+
import type { LogSink } from '../utils/logger.ts';
|
|
20
|
+
import type { MutationLockPort, MutationLockToken } from '../utils/env-lock.ts';
|
|
21
|
+
import type { AutosyncConfig } from './autosync-config.ts';
|
|
22
|
+
import type { AutosyncHistoryEntry } from './sync-history.ts';
|
|
23
|
+
import type { SyncPullReport, ApplyItemsReport } from './sync-engine.ts';
|
|
24
|
+
import type { SectionId } from '../schema/types.ts';
|
|
25
|
+
import type { SyncEngine } from './sync-engine.ts';
|
|
26
|
+
import type { SyncConfig, SyncTransportType } from './sync-config.ts';
|
|
27
|
+
import { syncIsConfigured } from './autosync-scheduler.ts';
|
|
28
|
+
|
|
29
|
+
test('intervalToMs: 间隔换算正确', () => {
|
|
30
|
+
assert.equal(intervalToMs('5m'), 5 * 60 * 1000);
|
|
31
|
+
assert.equal(intervalToMs('15m'), 15 * 60 * 1000);
|
|
32
|
+
assert.equal(intervalToMs('30m'), 30 * 60 * 1000);
|
|
33
|
+
assert.equal(intervalToMs('60m'), 60 * 60 * 1000);
|
|
34
|
+
assert.equal(intervalToMs('6h'), 6 * 60 * 60 * 1000);
|
|
35
|
+
assert.equal(intervalToMs('12h'), 12 * 60 * 60 * 1000);
|
|
36
|
+
assert.equal(intervalToMs('24h'), 24 * 60 * 60 * 1000);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
test('shouldTriggerStartupRun: 阈值判断', () => {
|
|
40
|
+
const threshold = 5 * 60 * 1000;
|
|
41
|
+
const now = 1_000_000_000_000;
|
|
42
|
+
assert.equal(shouldTriggerStartupRun(new Date(now - 60 * 1000).toISOString(), threshold, now), false);
|
|
43
|
+
assert.equal(shouldTriggerStartupRun(new Date(now - 6 * 60 * 1000).toISOString(), threshold, now), true);
|
|
44
|
+
assert.equal(shouldTriggerStartupRun(undefined, threshold, now), true);
|
|
45
|
+
assert.equal(shouldTriggerStartupRun(new Date(now - threshold).toISOString(), threshold, now), true);
|
|
46
|
+
assert.equal(shouldTriggerStartupRun(new Date(now - threshold - 1).toISOString(), threshold, now), true);
|
|
47
|
+
assert.equal(shouldTriggerStartupRun(new Date(now - threshold + 1).toISOString(), threshold, now), false);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
/** 构造一个可控 scheduler:注入全部 fs/engine 依赖,验证 runOnce 行为。 */
|
|
51
|
+
function makeScheduler(opts: {
|
|
52
|
+
cfg: AutosyncConfig;
|
|
53
|
+
engine: Partial<SyncEngine>;
|
|
54
|
+
history: AutosyncHistoryEntry[];
|
|
55
|
+
syncCfg?: SyncConfig | null;
|
|
56
|
+
/** issue #31:注入锁端口以覆盖「被锁挡下」的路径(缺省 = 无锁环境) */
|
|
57
|
+
mutationLock?: MutationLockPort;
|
|
58
|
+
/** issue #31:捕获日志行(断言 stale 指引确实写给用户) */
|
|
59
|
+
logSink?: LogSink;
|
|
60
|
+
}) {
|
|
61
|
+
const runs = new RunRegistry();
|
|
62
|
+
const entries: AutosyncHistoryEntry[] = [...opts.history];
|
|
63
|
+
let config = opts.cfg;
|
|
64
|
+
const scheduler = new AutoSyncScheduler({
|
|
65
|
+
syncDir: '/tmp',
|
|
66
|
+
host: { log: opts.logSink !== undefined ? createLogger({ level: 'debug', sink: opts.logSink }) : nullLogger() },
|
|
67
|
+
makeSyncEngine: () => opts.engine as SyncEngine,
|
|
68
|
+
msg: (k: string) => k,
|
|
69
|
+
runs,
|
|
70
|
+
now: () => new Date(1_000_000_000_000),
|
|
71
|
+
readConfig: async (_channel: SyncTransportType) => config,
|
|
72
|
+
writeConfig: async (_channel: SyncTransportType, c: AutosyncConfig) => { config = c; },
|
|
73
|
+
readSyncConfigFn: async (_channel: SyncTransportType) => opts.syncCfg !== undefined ? opts.syncCfg : ({ schemaVersion: 2, transport: 'git', git: { repoUrl: 'git@github.com:foo/bar.git' } }),
|
|
74
|
+
readHistoryFn: async () => ({ schemaVersion: 1, autosyncEntries: entries, updatedAt: '' }),
|
|
75
|
+
appendHistoryFn: async (e) => { entries.push(e); },
|
|
76
|
+
...(opts.mutationLock !== undefined ? { mutationLock: opts.mutationLock } : {}),
|
|
77
|
+
// 测试不用真实定时器:不调 start()
|
|
78
|
+
});
|
|
79
|
+
return { scheduler, runs, getConfig: () => config, getEntries: () => entries };
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** issue #31:模拟 acquire 被判 stale 的锁端口(真实 manager 在残留锁下返回同一状态)。 */
|
|
83
|
+
function staleLockPort(detail = 'owner pid=24140 确证不存在 (heartbeat expired)'): MutationLockPort {
|
|
84
|
+
return {
|
|
85
|
+
acquire: async () => ({ state: 'STALE_LOCK_DETECTED', token: null, detail }),
|
|
86
|
+
validate: (tok: unknown): tok is MutationLockToken => false,
|
|
87
|
+
release: async () => undefined,
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** 构造 pull 报告:changes 决定 needsReview(Conflict 等需人工决策项)。 */
|
|
92
|
+
function pullReport(entries: Array<{ adapter: SectionId; kind?: string }>): SyncPullReport {
|
|
93
|
+
const changes = entries.map((e, i) => ({
|
|
94
|
+
id: e.adapter + ':' + i,
|
|
95
|
+
adapter: e.adapter,
|
|
96
|
+
kind: (e.kind ?? 'Update') as never,
|
|
97
|
+
description: 'test',
|
|
98
|
+
severity: 'info' as never,
|
|
99
|
+
}));
|
|
100
|
+
return {
|
|
101
|
+
ok: true,
|
|
102
|
+
snapshotId: 'remote-1',
|
|
103
|
+
changes,
|
|
104
|
+
needsReview: changes.some((c) => ['Conflict', 'MissingSecret', 'MissingDependency', 'Error'].includes(c.kind)),
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** 构造 applyItems 报告(默认成功、applied = 给定分区)。 */
|
|
109
|
+
function applyReport(applied: string[], ok = true): ApplyItemsReport {
|
|
110
|
+
return {
|
|
111
|
+
ok, applied: ok ? applied : [], restoreId: 'r1', rolledBack: !ok,
|
|
112
|
+
warnings: [], failed: [], result: null,
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** 构造 preview 结果(applyItems 的会话级输入)。 */
|
|
117
|
+
function previewResult(): never {
|
|
118
|
+
return { ok: true, zipPath: '/tmp/session.zip', plan: { items: [] }, analysis: { valid: true }, snapshotId: 'remote-1' } as never;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
test('runOnce: enabled=false → skipped(disabled),不写历史', async () => {
|
|
122
|
+
const cfg: AutosyncConfig = { enabled: false, interval: '30m', startupMinIntervalMs: 300000, consecutiveFailures: 0 };
|
|
123
|
+
const { scheduler, getConfig, getEntries } = makeScheduler({ cfg, engine: {}, history: [] });
|
|
124
|
+
const result = await scheduler.runOnce('git');
|
|
125
|
+
assert.equal(result.status, 'skipped');
|
|
126
|
+
assert.equal(result.skipReason, 'disabled');
|
|
127
|
+
assert.equal(getEntries().length, 0, 'disabled 不写历史');
|
|
128
|
+
assert.equal(getConfig().consecutiveFailures, 0);
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
// ---------- issue #31:被锁挡下必须留痕(历史 + 指引),不能静默 ----------
|
|
132
|
+
|
|
133
|
+
test('runOnce: 残留锁挡住 → skipped(mutation-locked) 且**写历史**(issue #31:此前连历史都不写)', async () => {
|
|
134
|
+
const cfg: AutosyncConfig = { enabled: true, interval: '30m', startupMinIntervalMs: 300000, consecutiveFailures: 0 };
|
|
135
|
+
const { scheduler, getEntries, getConfig } = makeScheduler({
|
|
136
|
+
cfg, engine: {}, history: [], mutationLock: staleLockPort(),
|
|
137
|
+
});
|
|
138
|
+
const result = await scheduler.runOnce('git');
|
|
139
|
+
assert.equal(result.status, 'skipped');
|
|
140
|
+
assert.equal(result.skipReason, 'mutation-locked');
|
|
141
|
+
const entries = getEntries();
|
|
142
|
+
assert.equal(entries.length, 1, '被锁挡下必须留一条历史(否则用户只看得到「自动同步不再更新」)');
|
|
143
|
+
assert.equal(entries[0]!.status, 'skipped');
|
|
144
|
+
assert.equal(entries[0]!.skipReason, 'mutation-locked');
|
|
145
|
+
assert.equal(entries[0]!.transport, 'git');
|
|
146
|
+
assert.equal(getConfig().consecutiveFailures, 0, '被锁挡下不计入连续失败');
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
test('runOnce: 残留锁挡住 → 日志给出与 423 同源的 stale 指引(重试/重启无效 + 回收方式)', async () => {
|
|
150
|
+
const cfg: AutosyncConfig = { enabled: true, interval: '30m', startupMinIntervalMs: 300000, consecutiveFailures: 0 };
|
|
151
|
+
const lines: Array<{ level: string; message: string }> = [];
|
|
152
|
+
const { scheduler } = makeScheduler({
|
|
153
|
+
cfg, engine: {}, history: [], mutationLock: staleLockPort(),
|
|
154
|
+
logSink: (level, message) => { lines.push({ level, message }); },
|
|
155
|
+
});
|
|
156
|
+
await scheduler.runOnce('git');
|
|
157
|
+
const staleLine = lines.find((l) => l.message.includes('自动同步已跳过'));
|
|
158
|
+
assert.ok(staleLine !== undefined, `应有跳过日志: ${JSON.stringify(lines)}`);
|
|
159
|
+
assert.match(staleLine!.message, /残留/);
|
|
160
|
+
assert.match(staleLine!.message, /recover-stale-lock/, '必须给出可执行的回收方式');
|
|
161
|
+
assert.equal(staleLine!.message.includes('24140'), false, 'owner pid 属内部诊断,不得进用户文案');
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
test('issue #31 D:acquire 本身抛错(锁目录 IO)同样必须写历史,不得静默跳过', async () => {
|
|
165
|
+
const cfg: AutosyncConfig = { enabled: true, interval: '30m', startupMinIntervalMs: 300000, consecutiveFailures: 0 };
|
|
166
|
+
// 端口「已配置但 acquire 抛错」:模拟锁目录 IO/权限故障
|
|
167
|
+
const throwingPort = {
|
|
168
|
+
acquire: async () => { throw new Error('lock dir IO error') },
|
|
169
|
+
validate: () => false,
|
|
170
|
+
release: async () => {},
|
|
171
|
+
} as unknown as Parameters<typeof makeScheduler>[0]['mutationLock'];
|
|
172
|
+
const { scheduler, getEntries, getConfig } = makeScheduler({
|
|
173
|
+
cfg, engine: {}, history: [], mutationLock: throwingPort,
|
|
174
|
+
});
|
|
175
|
+
const result = await scheduler.runOnce('git');
|
|
176
|
+
assert.equal(result.status, 'skipped');
|
|
177
|
+
assert.equal(result.skipReason, 'mutation-locked');
|
|
178
|
+
assert.equal(getEntries().length, 1, 'acquire 抛错也是「被挡」,同样要留历史');
|
|
179
|
+
assert.equal(getEntries()[0]!.skipReason, 'mutation-locked');
|
|
180
|
+
assert.equal(getConfig().consecutiveFailures, 0, '不计入连续失败');
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
test('runOnce: 历史记录携带触发通道(transport=调用通道 git/webdav)', async () => { const cfg: AutosyncConfig = { enabled: true, interval: '30m', startupMinIntervalMs: 300000, consecutiveFailures: 0 };
|
|
184
|
+
// 两端无变化 → upToDate 成功历史(走 appendHistory)
|
|
185
|
+
const engine = {
|
|
186
|
+
hasNewRemoteSnapshot: async () => false,
|
|
187
|
+
hasLocalChanges: async () => false,
|
|
188
|
+
};
|
|
189
|
+
const { scheduler, getEntries } = makeScheduler({ cfg, engine, history: [] });
|
|
190
|
+
await scheduler.runOnce('git');
|
|
191
|
+
let entries = getEntries();
|
|
192
|
+
assert.ok(entries.some((e) => e.transport === 'git' && e.skipReason === 'upToDate'), `git 通道历史应带 transport=git: ${JSON.stringify(entries)}`);
|
|
193
|
+
await scheduler.runOnce('webdav');
|
|
194
|
+
entries = getEntries();
|
|
195
|
+
assert.ok(entries.some((e) => e.transport === 'webdav' && e.skipReason === 'upToDate'), 'webdav 通道历史应带 transport=webdav');
|
|
196
|
+
// 全部历史条目都应携带 channel
|
|
197
|
+
for (const e of entries) assert.ok(e.transport === 'git' || e.transport === 'webdav', `历史必须带 transport: ${JSON.stringify(e)}`);
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
test('runOnce: 未配置仓库 → skipped(unconfigured),写历史但不计失败', async () => {
|
|
201
|
+
const cfg: AutosyncConfig = { enabled: true, interval: '30m', startupMinIntervalMs: 300000, consecutiveFailures: 0 };
|
|
202
|
+
const scheduler = new AutoSyncScheduler({
|
|
203
|
+
syncDir: '/tmp',
|
|
204
|
+
host: { log: nullLogger() },
|
|
205
|
+
makeSyncEngine: () => ({} as SyncEngine),
|
|
206
|
+
msg: (k: string) => k,
|
|
207
|
+
runs: new RunRegistry(),
|
|
208
|
+
now: () => new Date(1_000_000_000_000),
|
|
209
|
+
readConfig: async () => cfg,
|
|
210
|
+
writeConfig: async () => {},
|
|
211
|
+
readSyncConfigFn: async () => null,
|
|
212
|
+
readHistoryFn: async () => ({ schemaVersion: 1, autosyncEntries: [], updatedAt: '' }),
|
|
213
|
+
appendHistoryFn: async () => {},
|
|
214
|
+
});
|
|
215
|
+
const result = await scheduler.runOnce('git');
|
|
216
|
+
assert.equal(result.status, 'skipped');
|
|
217
|
+
assert.equal(result.skipReason, 'unconfigured');
|
|
218
|
+
assert.equal(result.consecutiveFailures, 0, '未配置不累计失败');
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
test('runOnce: webdav 已配置(webdav.url 非空)→ 不判 unconfigured,正常走 pull(按通道判定)', async () => {
|
|
222
|
+
const cfg: AutosyncConfig = { enabled: true, interval: '30m', startupMinIntervalMs: 300000, consecutiveFailures: 0 };
|
|
223
|
+
const engine = {
|
|
224
|
+
pull: async (): Promise<SyncPullReport> => pullReport([]),
|
|
225
|
+
hasLocalChanges: async () => false,
|
|
226
|
+
};
|
|
227
|
+
const syncCfg: SyncConfig = { schemaVersion: 2, transport: 'webdav', webdav: { url: 'https://dav.example.com/remote.php/dav/files/u' } };
|
|
228
|
+
const { scheduler } = makeScheduler({ cfg, engine, history: [], syncCfg });
|
|
229
|
+
const result = await scheduler.runOnce('webdav');
|
|
230
|
+
assert.equal(result.status, 'success', 'webdav 已配置应进入同步流程');
|
|
231
|
+
assert.equal(result.skipReason, 'unchanged', '空变更 → pull/unchanged');
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
test('runOnce: webdav 未配置(webdav.url 缺)→ skipped(unconfigured),不计失败', async () => {
|
|
235
|
+
const cfg: AutosyncConfig = { enabled: true, interval: '30m', startupMinIntervalMs: 300000, consecutiveFailures: 1 };
|
|
236
|
+
const syncCfg: SyncConfig = { schemaVersion: 2, transport: 'webdav', webdav: { url: '' } };
|
|
237
|
+
const { scheduler, getConfig } = makeScheduler({ cfg, engine: {}, history: [], syncCfg });
|
|
238
|
+
const result = await scheduler.runOnce('webdav');
|
|
239
|
+
assert.equal(result.status, 'skipped');
|
|
240
|
+
assert.equal(result.skipReason, 'unconfigured');
|
|
241
|
+
assert.equal(result.consecutiveFailures, 1, '未配置不计失败(保持 1)');
|
|
242
|
+
assert.equal(getConfig().consecutiveFailures, 1);
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
test('syncIsConfigured: git 看 git.repoUrl、webdav 看 webdav.url;null 未配置', () => {
|
|
246
|
+
assert.equal(syncIsConfigured({ schemaVersion: 2, transport: 'git', git: { repoUrl: 'git@github.com:foo/bar.git' } }), true);
|
|
247
|
+
assert.equal(syncIsConfigured({ schemaVersion: 2, transport: 'git', git: { repoUrl: '' } }), false, 'git 缺 repoUrl → 未配置');
|
|
248
|
+
assert.equal(syncIsConfigured({ schemaVersion: 2, transport: 'webdav', webdav: { url: 'https://dav.example.com' } }), true);
|
|
249
|
+
assert.equal(syncIsConfigured({ schemaVersion: 2, transport: 'webdav', webdav: { url: '' } }), false, 'webdav 缺 url → 未配置');
|
|
250
|
+
assert.equal(syncIsConfigured(null), false);
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
test('runOnce: pull 抛错 → failed,连续失败计数 +1', async () => {
|
|
254
|
+
const cfg: AutosyncConfig = { enabled: true, interval: '30m', startupMinIntervalMs: 300000, consecutiveFailures: 2 };
|
|
255
|
+
const engine = { pull: async () => { throw new Error('network down'); } };
|
|
256
|
+
const { scheduler, getConfig, getEntries } = makeScheduler({ cfg, engine, history: [] });
|
|
257
|
+
const result = await scheduler.runOnce('git');
|
|
258
|
+
assert.equal(result.status, 'failed');
|
|
259
|
+
assert.equal(result.consecutiveFailures, 3, '连续失败 2→3');
|
|
260
|
+
assert.equal(getConfig().consecutiveFailures, 3);
|
|
261
|
+
assert.ok(getEntries().some((e) => e.status === 'failed'), '写入失败历史');
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
test('runOnce: 需人工决策项 → skipped(conflict),不写本地,不计失败', async () => {
|
|
265
|
+
const cfg: AutosyncConfig = { enabled: true, interval: '30m', startupMinIntervalMs: 300000, consecutiveFailures: 1 };
|
|
266
|
+
const engine = {
|
|
267
|
+
pull: async (): Promise<SyncPullReport> => pullReport([{ adapter: 'settings', kind: 'Conflict' }]),
|
|
268
|
+
};
|
|
269
|
+
const { scheduler, getConfig, getEntries } = makeScheduler({ cfg, engine, history: [] });
|
|
270
|
+
const result = await scheduler.runOnce('git');
|
|
271
|
+
assert.equal(result.status, 'skipped');
|
|
272
|
+
assert.equal(result.skipReason, 'conflict');
|
|
273
|
+
assert.deepEqual(result.conflictedSections, ['settings']);
|
|
274
|
+
assert.equal(result.consecutiveFailures, 1, '冲突跳过不计失败');
|
|
275
|
+
assert.equal(getConfig().consecutiveFailures, 1);
|
|
276
|
+
assert.ok(getEntries().some((e) => e.skipReason === 'conflict'), '写入冲突跳过历史');
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
test('runOnce: 无变化 → success(pull,unchanged),不上传(本地也无改动)', async () => {
|
|
280
|
+
const cfg: AutosyncConfig = { enabled: true, interval: '30m', startupMinIntervalMs: 300000, consecutiveFailures: 0 };
|
|
281
|
+
const engine = {
|
|
282
|
+
pull: async (): Promise<SyncPullReport> => pullReport([]),
|
|
283
|
+
hasLocalChanges: async () => false,
|
|
284
|
+
};
|
|
285
|
+
const { scheduler } = makeScheduler({ cfg, engine, history: [] });
|
|
286
|
+
const result = await scheduler.runOnce('git');
|
|
287
|
+
assert.equal(result.status, 'success');
|
|
288
|
+
assert.equal(result.direction, 'pull');
|
|
289
|
+
assert.equal(result.skipReason, 'unchanged');
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
test('runOnce: 完整双向 → 无冲突覆盖写本地 + push', async () => {
|
|
293
|
+
const cfg: AutosyncConfig = { enabled: true, interval: '30m', startupMinIntervalMs: 300000, consecutiveFailures: 0 };
|
|
294
|
+
const applyCalls: string[] = [];
|
|
295
|
+
const engine = {
|
|
296
|
+
pull: async (): Promise<SyncPullReport> => pullReport([{ adapter: 'settings' }]),
|
|
297
|
+
preview: async () => previewResult(),
|
|
298
|
+
applyItems: async () => { applyCalls.push('apply'); return applyReport(['settings']); },
|
|
299
|
+
push: async () => ({ ok: true, snapshotId: 'snap-push', sections: ['settings'] as never, warnings: [] }),
|
|
300
|
+
};
|
|
301
|
+
const { scheduler, getEntries } = makeScheduler({ cfg, engine, history: [] });
|
|
302
|
+
const result = await scheduler.runOnce('git');
|
|
303
|
+
assert.equal(result.status, 'success');
|
|
304
|
+
assert.equal(result.direction, 'both');
|
|
305
|
+
assert.deepEqual(result.appliedSections, ['settings']);
|
|
306
|
+
assert.equal(result.pushedSnapshotId, 'snap-push');
|
|
307
|
+
assert.equal(applyCalls.length, 1);
|
|
308
|
+
assert.ok(getEntries().some((e) => e.status === 'success' && e.direction === 'both'), '写入双向成功历史');
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
test('runOnce: startup 变体 → 只做 pull 覆盖,不上传', async () => {
|
|
312
|
+
const cfg: AutosyncConfig = { enabled: true, interval: '30m', startupMinIntervalMs: 300000, consecutiveFailures: 0 };
|
|
313
|
+
const pushCalls: string[] = [];
|
|
314
|
+
const engine = {
|
|
315
|
+
pull: async (): Promise<SyncPullReport> => pullReport([{ adapter: 'settings' }]),
|
|
316
|
+
preview: async () => previewResult(),
|
|
317
|
+
applyItems: async () => applyReport(['settings']),
|
|
318
|
+
push: async () => { pushCalls.push('push'); return { ok: true, snapshotId: 'x', sections: [] as never, warnings: [] }; },
|
|
319
|
+
};
|
|
320
|
+
const { scheduler } = makeScheduler({ cfg, engine, history: [] });
|
|
321
|
+
const result = await scheduler.runOnce('git', { startup: true });
|
|
322
|
+
assert.equal(result.direction, 'pull', 'startup 不上传');
|
|
323
|
+
assert.equal(pushCalls.length, 0, 'startup 变体不调用 push');
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
test('runOnce: 连续两次执行不再 skip(conflict)(run 完成收尾)', async () => {
|
|
327
|
+
const cfg: AutosyncConfig = { enabled: true, interval: '30m', startupMinIntervalMs: 300000, consecutiveFailures: 0 };
|
|
328
|
+
const engine = {
|
|
329
|
+
pull: async (): Promise<SyncPullReport> => pullReport([{ adapter: 'settings' }]),
|
|
330
|
+
preview: async () => previewResult(),
|
|
331
|
+
applyItems: async () => applyReport(['settings']),
|
|
332
|
+
push: async () => ({ ok: true, snapshotId: 's1', sections: ['settings'] as never, warnings: [] }),
|
|
333
|
+
};
|
|
334
|
+
const { scheduler, runs } = makeScheduler({ cfg, engine, history: [] });
|
|
335
|
+
const first = await scheduler.runOnce('git');
|
|
336
|
+
assert.equal(first.status, 'success', '首次执行成功');
|
|
337
|
+
assert.equal(runs.listActive().filter((r) => r.kind === 'autosync').length, 0, '执行后 registry 无滞留 autosync running 记录');
|
|
338
|
+
const second = await scheduler.runOnce('git');
|
|
339
|
+
assert.equal(second.status, 'success', '第二次执行不再被同 kind 注册冲突拦截');
|
|
340
|
+
assert.equal(second.direction, 'both');
|
|
341
|
+
assert.equal(runs.listActive().filter((r) => r.kind === 'autosync').length, 0);
|
|
342
|
+
});
|
|
343
|
+
|
|
344
|
+
test('runOnce: 远端无新快照且本地无改动 → success(upToDate),不 pull 不 push', async () => {
|
|
345
|
+
const cfg: AutosyncConfig = { enabled: true, interval: '30m', startupMinIntervalMs: 300000, consecutiveFailures: 0 };
|
|
346
|
+
const mergeCalls: string[] = [];
|
|
347
|
+
const pushCalls: string[] = [];
|
|
348
|
+
const engine = {
|
|
349
|
+
hasNewRemoteSnapshot: async () => false,
|
|
350
|
+
hasLocalChanges: async () => false,
|
|
351
|
+
pull: async () => { mergeCalls.push('pull'); return pullReport([]); },
|
|
352
|
+
push: async () => { pushCalls.push('push'); return { ok: true, snapshotId: 's', sections: [] as never, warnings: [] }; },
|
|
353
|
+
};
|
|
354
|
+
const { scheduler, getEntries } = makeScheduler({ cfg, engine, history: [] });
|
|
355
|
+
const result = await scheduler.runOnce('git');
|
|
356
|
+
assert.equal(result.status, 'success');
|
|
357
|
+
assert.equal(result.direction, 'none');
|
|
358
|
+
assert.equal(result.skipReason, 'upToDate');
|
|
359
|
+
assert.equal(mergeCalls.length, 0, '远端无新生不拉取');
|
|
360
|
+
assert.equal(pushCalls.length, 0, '本地无改动不上传');
|
|
361
|
+
assert.ok(getEntries().some((e) => e.skipReason === 'upToDate'), '写入 upToDate 历史');
|
|
362
|
+
});
|
|
363
|
+
|
|
364
|
+
test('runOnce: 远端无新快照但本地有改动 → 只 push 不拉取(direction=push)', async () => {
|
|
365
|
+
const cfg: AutosyncConfig = { enabled: true, interval: '30m', startupMinIntervalMs: 300000, consecutiveFailures: 0 };
|
|
366
|
+
const mergeCalls: string[] = [];
|
|
367
|
+
const engine = {
|
|
368
|
+
hasNewRemoteSnapshot: async () => false,
|
|
369
|
+
hasLocalChanges: async () => true,
|
|
370
|
+
pull: async () => { mergeCalls.push('pull'); return pullReport([]); },
|
|
371
|
+
push: async () => ({ ok: true, snapshotId: 'snap-local', sections: ['settings'] as never, warnings: [] }),
|
|
372
|
+
};
|
|
373
|
+
const { scheduler, getEntries } = makeScheduler({ cfg, engine, history: [] });
|
|
374
|
+
const result = await scheduler.runOnce('git');
|
|
375
|
+
assert.equal(result.status, 'success');
|
|
376
|
+
assert.equal(result.direction, 'push', '远端无新生只上传本地改动');
|
|
377
|
+
assert.equal(result.pushedSnapshotId, 'snap-local');
|
|
378
|
+
assert.equal(mergeCalls.length, 0, '远端无新生不执行拉取');
|
|
379
|
+
assert.ok(getEntries().some((e) => e.status === 'success' && e.direction === 'push'), '写入 push 历史');
|
|
380
|
+
});
|
|
381
|
+
|
|
382
|
+
test('runOnce: 远端有新快照但本地无改动 → 只 pull 覆盖,不 push', async () => {
|
|
383
|
+
const cfg: AutosyncConfig = { enabled: true, interval: '30m', startupMinIntervalMs: 300000, consecutiveFailures: 0 };
|
|
384
|
+
const pushCalls: string[] = [];
|
|
385
|
+
const engine = {
|
|
386
|
+
hasNewRemoteSnapshot: async () => true,
|
|
387
|
+
hasLocalChanges: async () => false,
|
|
388
|
+
pull: async (): Promise<SyncPullReport> => pullReport([{ adapter: 'settings' }]),
|
|
389
|
+
preview: async () => previewResult(),
|
|
390
|
+
applyItems: async () => applyReport(['settings']),
|
|
391
|
+
push: async () => { pushCalls.push('push'); return { ok: true, snapshotId: 'x', sections: [] as never, warnings: [] }; },
|
|
392
|
+
};
|
|
393
|
+
const { scheduler } = makeScheduler({ cfg, engine, history: [] });
|
|
394
|
+
const result = await scheduler.runOnce('git');
|
|
395
|
+
assert.equal(result.status, 'success');
|
|
396
|
+
assert.equal(result.direction, 'pull', '本地无改动不上传,只拉取覆盖');
|
|
397
|
+
assert.deepEqual(result.appliedSections, ['settings']);
|
|
398
|
+
assert.equal(pushCalls.length, 0, '本地无改动不 push');
|
|
399
|
+
});
|
|
400
|
+
|
|
401
|
+
test('start(): 定时器触发后自动重排下一次(周期性后台同步)', async () => {
|
|
402
|
+
const cfg: AutosyncConfig = { enabled: true, interval: '30m', startupMinIntervalMs: 300000, consecutiveFailures: 0 };
|
|
403
|
+
const pending: Array<() => void> = [];
|
|
404
|
+
let timerSeq = 0;
|
|
405
|
+
const engine = {
|
|
406
|
+
pull: async (): Promise<SyncPullReport> => pullReport([]),
|
|
407
|
+
hasLocalChanges: async () => false,
|
|
408
|
+
};
|
|
409
|
+
const scheduler = new AutoSyncScheduler({
|
|
410
|
+
syncDir: '/tmp',
|
|
411
|
+
host: { log: nullLogger() },
|
|
412
|
+
makeSyncEngine: () => engine as SyncEngine,
|
|
413
|
+
msg: (k: string) => k,
|
|
414
|
+
runs: new RunRegistry(),
|
|
415
|
+
now: () => new Date(1_000_000_000_000),
|
|
416
|
+
readConfig: async (channel: SyncTransportType) => channel === 'git' ? cfg : { ...cfg, enabled: false },
|
|
417
|
+
writeConfig: async () => {},
|
|
418
|
+
readSyncConfigFn: async (_channel: SyncTransportType) => ({ schemaVersion: 2, transport: 'git', git: { repoUrl: 'git@github.com:foo/bar.git' } }),
|
|
419
|
+
readHistoryFn: async () => ({ schemaVersion: 1, autosyncEntries: [], updatedAt: '' }),
|
|
420
|
+
appendHistoryFn: async () => {},
|
|
421
|
+
setTimer: (fn) => { pending.push(fn); timerSeq += 1; return String(timerSeq) as unknown as ReturnType<typeof setTimeout>; },
|
|
422
|
+
clearTimer: () => {},
|
|
423
|
+
});
|
|
424
|
+
scheduler.start();
|
|
425
|
+
// start() → refreshTimer 异步读配置后排入首个定时器(startupRun 不会排定时器)
|
|
426
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
427
|
+
assert.equal(pending.length, 1, '启动后已排入首个定时器');
|
|
428
|
+
// 触发一次定时回调:应执行 runOnce 并在结束后重新排定下一次
|
|
429
|
+
const firstTimer = pending[0];
|
|
430
|
+
assert.ok(firstTimer, '首个定时器句柄存在');
|
|
431
|
+
firstTimer();
|
|
432
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
433
|
+
assert.ok(pending.length >= 2, '一轮执行后重新排入下一次定时器(循环调度)');
|
|
434
|
+
scheduler.stop();
|
|
435
|
+
// stop 后触发已排定时器不再重排
|
|
436
|
+
const countAfterStop = pending.length;
|
|
437
|
+
const lastTimer = pending[countAfterStop - 1];
|
|
438
|
+
assert.ok(lastTimer, '停止前最后一次排期的定时器存在');
|
|
439
|
+
lastTimer();
|
|
440
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
441
|
+
assert.equal(pending.length, countAfterStop, 'stop 后不再排期');
|
|
442
|
+
});
|
|
443
|
+
|
|
444
|
+
test('双通道:git/webdav 同时 enabled → 各自独立排期;runOnce 写各自通道配置', async () => {
|
|
445
|
+
const gitCfg: AutosyncConfig = { enabled: true, interval: '30m', startupMinIntervalMs: 300000, consecutiveFailures: 0 };
|
|
446
|
+
const webdavCfg: AutosyncConfig = { enabled: true, interval: '5m', startupMinIntervalMs: 300000, consecutiveFailures: 0 };
|
|
447
|
+
const configs: Record<SyncTransportType, AutosyncConfig> = { git: gitCfg, webdav: webdavCfg };
|
|
448
|
+
const pending: Array<() => void> = [];
|
|
449
|
+
let timerSeq = 0;
|
|
450
|
+
const engine = {
|
|
451
|
+
hasNewRemoteSnapshot: async () => false,
|
|
452
|
+
hasLocalChanges: async () => false,
|
|
453
|
+
pull: async (): Promise<SyncPullReport> => pullReport([]),
|
|
454
|
+
};
|
|
455
|
+
const scheduler = new AutoSyncScheduler({
|
|
456
|
+
syncDir: '/tmp',
|
|
457
|
+
host: { log: nullLogger() },
|
|
458
|
+
makeSyncEngine: () => engine as SyncEngine,
|
|
459
|
+
msg: (k: string) => k,
|
|
460
|
+
runs: new RunRegistry(),
|
|
461
|
+
now: () => new Date(1_000_000_000_000),
|
|
462
|
+
readConfig: async (channel: SyncTransportType) => configs[channel],
|
|
463
|
+
writeConfig: async (channel: SyncTransportType, c: AutosyncConfig) => { configs[channel] = c; },
|
|
464
|
+
readSyncConfigFn: async (channel: SyncTransportType) => channel === 'webdav'
|
|
465
|
+
? ({ schemaVersion: 2, transport: 'webdav', webdav: { url: 'https://dav.example.com/dav' } } as SyncConfig)
|
|
466
|
+
: ({ schemaVersion: 2, transport: 'git', git: { repoUrl: 'git@github.com:foo/bar.git' } } as SyncConfig),
|
|
467
|
+
readHistoryFn: async () => ({ schemaVersion: 1, autosyncEntries: [], updatedAt: '' }),
|
|
468
|
+
appendHistoryFn: async () => {},
|
|
469
|
+
setTimer: (fn) => { pending.push(fn); timerSeq += 1; return String(timerSeq) as unknown as ReturnType<typeof setTimeout>; },
|
|
470
|
+
clearTimer: () => {},
|
|
471
|
+
});
|
|
472
|
+
scheduler.start();
|
|
473
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
474
|
+
assert.equal(pending.length, 2, '两个 enabled 通道各自排期');
|
|
475
|
+
scheduler.stop();
|
|
476
|
+
// 停止后不再重排
|
|
477
|
+
const countAfterStop = pending.length;
|
|
478
|
+
const lastTimer = pending[countAfterStop - 1];
|
|
479
|
+
assert.ok(lastTimer, '停止前最后一次排期的定时器存在');
|
|
480
|
+
lastTimer();
|
|
481
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
482
|
+
assert.equal(pending.length, countAfterStop, 'stop 后不再排期');
|
|
483
|
+
// runOnce 按通道独立:只跑 webdav 时 git 配置不受影响
|
|
484
|
+
// (start() 的启动触发已对 git 通道跑过一次 startup 变体,故先记录基线再对比)
|
|
485
|
+
const gitBefore = { ...configs.git };
|
|
486
|
+
const result = await scheduler.runOnce('webdav');
|
|
487
|
+
assert.equal(result.status, 'success', 'webdav 通道照常执行');
|
|
488
|
+
assert.deepEqual(configs.git, gitBefore, 'git 通道配置未被 webdav 运行改变');
|
|
489
|
+
assert.equal(configs.webdav.lastRunStatus, 'success', 'webdav 通道写入自己的运行状态');
|
|
490
|
+
});
|