11import fs from "node:fs/promises" ;
2+ import os from "node:os" ;
23import path from "node:path" ;
34import { afterEach , beforeEach , describe , expect , it } from "vitest" ;
45import { runCommandBuffered } from "../process/exec.js" ;
@@ -14,6 +15,8 @@ import {
1415import {
1516 assertGitHubPublicationTreeHasNoFilters ,
1617 assertSafeGitPublicationWorkspace ,
18+ githubPublicationPushArgs ,
19+ githubPublicationUpdateRefArgs ,
1720} from "./github-publication-git-transport.js" ;
1821
1922let testState : OpenClawTestState ;
@@ -114,6 +117,92 @@ describe("GitHub publication index update", () => {
114117 ) . resolves . toBeUndefined ( ) ;
115118 } ) ;
116119
120+ it . each ( [
121+ { scope : "--local" as const , label : "repository-local" } ,
122+ { scope : "--worktree" as const , label : "worktree-scoped" } ,
123+ ] ) ( "publishes and recovers with $label hooks without executing them" , async ( { scope } ) => {
124+ const fixture = await createFixture ( ) ;
125+ const remote = await makeDirectory ( "remote" ) ;
126+ const hooks = await makeDirectory ( "hooks" ) ;
127+ const marker = path . join ( hooks , "invoked" ) ;
128+ const hookEnv = { ...process . env , OPENCLAW_PUBLICATION_HOOK_MARKER : marker } ;
129+ await git ( remote , [ "init" , "--bare" ] ) ;
130+ await Promise . all (
131+ [ "pre-push" , "post-index-change" , "reference-transaction" ] . map (
132+ async ( hook ) =>
133+ await fs . writeFile (
134+ path . join ( hooks , hook ) ,
135+ '#!/bin/sh\nprintf invoked > "$OPENCLAW_PUBLICATION_HOOK_MARKER"\nexit 97\n' ,
136+ { mode : 0o755 } ,
137+ ) ,
138+ ) ,
139+ ) ;
140+ if ( scope === "--worktree" ) {
141+ await git ( fixture . cwd , [ "config" , "--local" , "extensions.worktreeConfig" , "true" ] ) ;
142+ }
143+ await git ( fixture . cwd , [ "config" , scope , "core.hooksPath" , hooks ] ) ;
144+
145+ await expect (
146+ assertSafeGitPublicationWorkspace (
147+ fixture . cwd ,
148+ async ( argv , options ) =>
149+ await runCommandBuffered ( argv , {
150+ cwd : options ?. cwd ?? fixture . cwd ,
151+ env : options ?. env ,
152+ timeoutMs : 10_000 ,
153+ maxOutputBytes : 64 * 1024 ,
154+ } ) ,
155+ ) ,
156+ ) . resolves . toBeUndefined ( ) ;
157+
158+ await expect (
159+ updateGitHubPublicationBranchAndIndex ( {
160+ ...publicationIndexParams ( fixture ) ,
161+ env : hookEnv ,
162+ updateRef : async ( ) => {
163+ await git (
164+ fixture . cwd ,
165+ githubPublicationUpdateRefArgs ( "main" , fixture . headCommit , fixture . previousHead ) . slice (
166+ 1 ,
167+ ) ,
168+ undefined ,
169+ hookEnv ,
170+ ) ;
171+ throw new Error ( "response lost" ) ;
172+ } ,
173+ } ) ,
174+ ) . rejects . toThrow ( "workspace recovery is pending" ) ;
175+ await fs . rm ( path . join ( fixture . cwd , ".git" , "index.lock" ) ) ;
176+ const hardenedGit = [ "-c" , `core.hooksPath=${ os . devNull } ` , "-c" , "core.fsmonitor=false" ] ;
177+ await git (
178+ fixture . cwd ,
179+ [ ...hardenedGit , "update-index" , "--force-remove" , "artifact.txt" ] ,
180+ undefined ,
181+ hookEnv ,
182+ ) ;
183+ await git ( fixture . cwd , [ ...hardenedGit , "add" , "artifact.txt" ] , undefined , hookEnv ) ;
184+ await recoverGitHubPublicationBranchAndIndex ( {
185+ cwd : fixture . cwd ,
186+ requestId : REQUEST_ID ,
187+ branch : "main" ,
188+ sourceHeadCommit : fixture . previousHead ,
189+ workspaceTree : fixture . workspaceTree ,
190+ assertCurrent : ( ) => undefined ,
191+ run : async ( argv , options ) =>
192+ await git ( fixture . cwd , argv . slice ( 1 ) , options ?. input , options ?. env ?? hookEnv ) ,
193+ } ) ;
194+ await expect ( fs . access ( marker ) ) . rejects . toThrow ( ) ;
195+ await git (
196+ fixture . cwd ,
197+ githubPublicationPushArgs ( remote , fixture . headCommit , "publication" ) . slice ( 1 ) ,
198+ undefined ,
199+ hookEnv ,
200+ ) ;
201+
202+ expect ( await git ( remote , [ "rev-parse" , "refs/heads/publication" ] ) ) . toBe ( fixture . headCommit ) ;
203+ await expect ( fs . access ( marker ) ) . rejects . toThrow ( ) ;
204+ } ) ;
205+
117206 it ( "rejects a filter from Git's default global attributes file" , async ( ) => {
118207 const cwd = await makeDirectory ( "attributes" ) ;
119208 const globalAttributes = path . join ( cwd , "global-attributes" ) ;
0 commit comments