@@ -4,15 +4,18 @@ import path from "node:path";
44import { DatabaseSync } from "node:sqlite" ;
55import { afterEach , describe , expect , it , vi } from "vitest" ;
66import { loadSqliteVecExtension } from "../../packages/memory-host-sdk/src/engine-storage.js" ;
7- import { requireGitCommand as requireGit } from "../infra/git-exec.js" ;
7+ import { backupGitCreateCommand } from "../commands/backup-git.js" ;
8+ import { readBackupFreshness } from "../commands/backup-health.js" ;
9+ import { createTestRuntime } from "../commands/test-runtime-config-helpers.js" ;
10+ import { executeGitCommand , requireGitCommand as requireGit } from "../infra/git-exec.js" ;
811import { OPENCLAW_AGENT_SCHEMA_VERSION } from "../state/openclaw-agent-db-contract.js" ;
912import { OPENCLAW_STATE_SCHEMA_VERSION } from "../state/openclaw-state-db-contract.js" ;
1013import {
1114 closeOpenClawStateDatabaseForTest ,
1215 openOpenClawStateDatabase ,
1316} from "../state/openclaw-state-db.js" ;
1417import { resolveOpenClawStateSqlitePath } from "../state/openclaw-state-db.paths.js" ;
15- import { createPathResolutionEnv } from "../test-utils/env.js" ;
18+ import { createPathResolutionEnv , withEnvAsync } from "../test-utils/env.js" ;
1619import { dumpGitBackupDatabase , restoreGitBackupDirectory } from "./git-backup-codec.js" ;
1720import { createGitBackup , initializeGitBackupRepository } from "./git-backup.js" ;
1821
@@ -412,6 +415,62 @@ describe("Git-backed SQLite snapshots", () => {
412415 expect ( result . pushWarning ?. length ) . toBeLessThanOrEqual ( 500 ) ;
413416 } ) ;
414417
418+ it ( "refuses adopted non-backup ancestry and records local push degradation" , async ( ) => {
419+ const root = await tempRoot ( ) ;
420+ const { stateDir } = createStateDatabaseFixture ( root ) ;
421+ const repositoryPath = path . join ( root , "adopted-repository" ) ;
422+ const remotePath = path . join ( root , "remote.git" ) ;
423+ await requireGit ( root , [ "init" , "--bare" , remotePath ] ) ;
424+ await initializeGitBackupRepository ( { repositoryPath, stateDir, remote : remotePath } ) ;
425+ await requireGit ( repositoryPath , [ "config" , "user.name" , "OpenClaw Backup Test" ] ) ;
426+ await requireGit ( repositoryPath , [ "config" , "user.email" , "[email protected] " ] ) ; 427+ await fs . writeFile ( path . join ( repositoryPath , "unrelated.txt" ) , "operator-owned\n" ) ;
428+ await requireGit ( repositoryPath , [ "add" , "unrelated.txt" ] ) ;
429+ await requireGit ( repositoryPath , [ "commit" , "-m" , "operator history" ] ) ;
430+
431+ const warning =
432+ "repository history contains non-backup commits; use a dedicated backup repository" ;
433+ await withEnvAsync ( { OPENCLAW_STATE_DIR : stateDir } , async ( ) => {
434+ const result = await backupGitCreateCommand ( createTestRuntime ( ) , {
435+ repository : repositoryPath ,
436+ global : true ,
437+ push : true ,
438+ excludeSecrets : true ,
439+ } ) ;
440+
441+ expect ( result ) . toMatchObject ( { noChanges : false , pushed : false , pushWarning : warning } ) ;
442+ expect ( result . commit ) . toMatch ( / ^ [ a - f 0 - 9 ] { 40 } $ / u) ;
443+ expect ( readBackupFreshness ( process . env ) ) . toMatchObject ( {
444+ latest : { status : "ok" , kind : "git" , pushFailed : true , error : warning } ,
445+ latestOk : { status : "ok" , kind : "git" , pushFailed : true , error : warning } ,
446+ } ) ;
447+ } ) ;
448+ expect ( ( await executeGitCommand ( remotePath , [ "show-ref" ] ) ) . code ) . not . toBe ( 0 ) ;
449+ } ) ;
450+
451+ it ( "pushes backup-only ancestry to a new remote" , async ( ) => {
452+ const root = await tempRoot ( ) ;
453+ const { stateDir, database } = createStateDatabaseFixture ( root ) ;
454+ const repositoryPath = path . join ( root , "backup-repository" ) ;
455+ const remotePath = path . join ( root , "remote.git" ) ;
456+ await requireGit ( root , [ "init" , "--bare" , remotePath ] ) ;
457+ await initializeGitBackupRepository ( { repositoryPath, stateDir, remote : remotePath } ) ;
458+ await requireGit ( repositoryPath , [ "config" , "user.name" , "OpenClaw Backup Test" ] ) ;
459+ await requireGit ( repositoryPath , [ "config" , "user.email" , "[email protected] " ] ) ; 460+
461+ const result = await createGitBackup ( {
462+ repositoryPath,
463+ stateDir,
464+ databases : [ database ] ,
465+ push : true ,
466+ } ) ;
467+
468+ const branch = await requireGit ( repositoryPath , [ "branch" , "--show-current" ] ) ;
469+ expect ( result ) . toMatchObject ( { noChanges : false , pushed : true } ) ;
470+ expect ( result ) . not . toHaveProperty ( "pushWarning" ) ;
471+ expect ( await requireGit ( remotePath , [ "rev-parse" , `refs/heads/${ branch } ` ] ) ) . toBe ( result . commit ) ;
472+ } ) ;
473+
415474 it ( "redacts credential-bearing origins in conflict errors" , async ( ) => {
416475 const root = await tempRoot ( ) ;
417476 const stateDir = path . join ( root , "state" ) ;
0 commit comments