Spaces:
Running
Running
File size: 19,231 Bytes
9d44811 6825c0c 4d6d6c8 5599c48 e344055 96bdf6c a76e03b 76bde47 9d44811 6825c0c e344055 6825c0c 9d44811 76bde47 3e8ea5d 96bdf6c 76bde47 96bdf6c 3e8ea5d 84810e9 3e8ea5d 76bde47 3e8ea5d 84810e9 3e8ea5d 84810e9 3e8ea5d 84810e9 3e8ea5d 76bde47 3e8ea5d 76bde47 3e8ea5d 76bde47 84810e9 76bde47 84810e9 76bde47 3e8ea5d 9d44811 e344055 9d44811 4d6d6c8 6825c0c 4d6d6c8 6825c0c 4d6d6c8 6825c0c 76bde47 6825c0c 4d6d6c8 6825c0c a76e03b ecb6084 a76e03b 5599c48 6825c0c 5599c48 6825c0c 5599c48 4d6d6c8 76bde47 4d6d6c8 3e8ea5d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 | import {
GIT_ARCHIVE_MAX_BUFFER_BYTES,
assertDeployMarkerMatches,
buildDeployMarker,
buildDeployMarkerRouteSource,
isSpaceDeployPath,
isHfUploadExistingSpacePolicyError,
isRetryableSpaceInfoReadError,
shouldUseGitPushForSpace,
buildUploadArgs,
extractUploadCommitSha,
findRemoteDeletePaths,
parseRepositorySlug,
rewriteSpaceReadmeImageSources,
waitForRunning
} from './deploy-hf-space.mjs';
import assert from 'node:assert/strict';
import { describe, it } from 'node:test';
describe('HF Space deploy script', () => {
it('recognizes the existing Docker Space create-policy error for the Git deployment fallback', () => {
const policyError = [
'Set HF_DEBUG=1 as environment variable for full traceback.',
"Error: Client error '402 Payment Required' for url 'https://huggingface.co/api/repos/create'",
'Static Spaces are free for everyone, but hosting Gradio and Docker Spaces on free cpu-basic requires a PRO subscription.'
].join('\n');
assert.equal(isHfUploadExistingSpacePolicyError(policyError), true);
assert.equal(
isHfUploadExistingSpacePolicyError(
"Client error '402 Payment Required' for url 'https://huggingface.co/api/models/create'"
),
false
);
assert.equal(
isHfUploadExistingSpacePolicyError(
"Client error '401 Unauthorized' for url 'https://huggingface.co/api/repos/create'"
),
false
);
});
it('uses Git directly for an existing Docker Space', () => {
assert.equal(shouldUseGitPushForSpace({ sdk: 'docker' }), true);
assert.equal(shouldUseGitPushForSpace({ sdk: 'gradio' }), false);
assert.equal(shouldUseGitPushForSpace({}), false);
});
it('extracts the Space commit SHA from hf upload JSON output', () => {
const sha = extractUploadCommitSha(
[
'Start hashing 189 files.',
'{"url":"https://huggingface.co/spaces/misonL/visual-journal/commit/32151b6aeaec0e59f14d1aaa87fba160ca8410df"}'
].join('\n')
);
assert.equal(sha, '32151b6aeaec0e59f14d1aaa87fba160ca8410df');
});
it('rejects upload output without a Space commit URL', () => {
assert.throws(
() => extractUploadCommitSha('{"url":"https://huggingface.co/spaces/misonL/demo"}'),
/commit SHA or commit URL/
);
});
it('extracts the Space commit SHA from direct hf upload JSON fields', () => {
assert.equal(
extractUploadCommitSha('{"sha":"cccccccccccccccccccccccccccccccccccccccc"}'),
'cccccccccccccccccccccccccccccccccccccccc'
);
assert.equal(
extractUploadCommitSha('{"commit_sha":"dddddddddddddddddddddddddddddddddddddddd"}'),
'dddddddddddddddddddddddddddddddddddddddd'
);
});
it('extracts the Space commit SHA when hf prints warning lines before JSON', () => {
const sha = extractUploadCommitSha(
[
'[WARN] retrying upload metadata',
'{"url":"https://huggingface.co/spaces/misonL/visual-journal/commit/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}',
'done'
].join('\n')
);
assert.equal(sha, 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa');
});
it('builds upload args without embedding newline characters in commit metadata', () => {
const args = buildUploadArgs({
sourceDir: '/tmp/source',
localSha: 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb',
repoSlug: 'MisonL/visual-journal'
});
assert.equal(args.includes('--json'), true);
assert.equal(args[args.indexOf('--commit-message') + 1], 'Deploy bbbbbbb to Docker Space');
assert.equal(
args[args.indexOf('--commit-description') + 1],
'Source: MisonL/visual-journal@bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'
);
assert.equal(
args.some((arg) => arg.includes('\n')),
false
);
});
it('uses a provided repository slug in upload commit metadata', () => {
const args = buildUploadArgs({
sourceDir: '/tmp/source',
localSha: 'eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee',
repoSlug: 'owner/repo'
});
assert.equal(
args[args.indexOf('--commit-description') + 1],
'Source: owner/repo@eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee'
);
});
it('parses GitHub repository slugs from common origin URL formats', () => {
assert.equal(
parseRepositorySlug('https://github.com/MisonL/visual-journal.git'),
'MisonL/visual-journal'
);
assert.equal(
parseRepositorySlug('git@github.com:MisonL/visual-journal.git'),
'MisonL/visual-journal'
);
assert.throws(() => parseRepositorySlug('not-a-github-url'), /Set REPO_SLUG=owner\/repo/);
});
it('requires an explicit repository slug for upload metadata', () => {
assert.throws(
() =>
buildUploadArgs({
sourceDir: '/tmp/source',
localSha: 'ffffffffffffffffffffffffffffffffffffffff'
}),
/REPO_SLUG is required/
);
});
it('keeps enough buffer for repository archives used by Space uploads', () => {
assert.ok(GIT_ARCHIVE_MAX_BUFFER_BYTES >= 128 * 1024 * 1024);
});
it('excludes README documentation binaries from the Space source tree', () => {
assert.equal(isSpaceDeployPath('README.md'), true);
assert.equal(isSpaceDeployPath('src/app/page.tsx'), true);
assert.equal(isSpaceDeployPath('readme-images/interface.jpg'), false);
assert.equal(isSpaceDeployPath('readme-images/nested/example.jpg'), false);
});
it('rewrites local README image sources to immutable GitHub sources for the Space', () => {
const localSha = '1111111111111111111111111111111111111111';
const readme = '<img src="./readme-images/interface.jpg?v=123" alt="Interface" />';
assert.equal(
rewriteSpaceReadmeImageSources(readme, 'owner/repo', localSha),
`<img src="https://raw.githubusercontent.com/owner/repo/${localSha}/readme-images/interface.jpg?v=123" alt="Interface" />`
);
});
it('adds delete args for remote files that are no longer in git HEAD', () => {
const args = buildUploadArgs({
sourceDir: '/tmp/source',
localSha: '1111111111111111111111111111111111111111',
repoSlug: 'owner/repo',
deletePaths: ['old-file.md', 'scripts/old-script.mjs']
});
assert.deepEqual(args.slice(-4), ['--delete', 'old-file.md', '--delete', 'scripts/old-script.mjs']);
});
it('rejects unsafe delete path values before invoking hf upload', () => {
assert.throws(
() =>
buildUploadArgs({
sourceDir: '/tmp/source',
localSha: '2222222222222222222222222222222222222222',
repoSlug: 'owner/repo',
deletePaths: ['bad\npath']
}),
/deletePaths must contain/
);
});
it('finds stale remote files deterministically', () => {
assert.deepEqual(
findRemoteDeletePaths(new Set(['README.md', 'src/app.ts']), ['old.md', 'src/app.ts', 'README.md']),
['old.md']
);
});
it('builds a non-secret deploy marker for service-side deployment verification', () => {
const marker = buildDeployMarker(
'3333333333333333333333333333333333333333',
new Date('2026-06-20T10:00:00.000Z'),
'deploy-333'
);
assert.deepEqual(marker, {
schema_version: 1,
local_sha: '3333333333333333333333333333333333333333',
created_at: '2026-06-20T10:00:00.000Z',
deploy_id: 'deploy-333'
});
assert.doesNotMatch(JSON.stringify(marker), /key|token|secret|password/i);
});
it('builds a unique deploy marker id when none is provided', () => {
const first = buildDeployMarker(
'3333333333333333333333333333333333333333',
new Date('2026-06-20T10:00:00.000Z')
);
const second = buildDeployMarker(
'3333333333333333333333333333333333333333',
new Date('2026-06-20T10:00:00.000Z')
);
assert.match(first.deploy_id, /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/);
assert.notEqual(first.deploy_id, second.deploy_id);
});
it('rejects short deploy marker commit shas before upload', () => {
assert.throws(() => buildDeployMarker('3333333'), /full git commit SHA/);
});
it('rejects unsafe deploy marker ids before upload', () => {
assert.throws(
() =>
buildDeployMarker(
'3333333333333333333333333333333333333333',
new Date('2026-06-20T10:00:00.000Z'),
'deploy\n333'
),
/deployId must be/
);
});
it('rejects stale deploy markers from an older same-commit deployment', () => {
const expected = buildDeployMarker(
'3333333333333333333333333333333333333333',
new Date('2026-06-20T10:00:00.000Z'),
'deploy-current'
);
const stale = buildDeployMarker(
'3333333333333333333333333333333333333333',
new Date('2026-06-20T09:00:00.000Z'),
'deploy-stale'
);
assert.throws(() => assertDeployMarkerMatches(stale, expected), /created_at mismatch/);
assert.deepEqual(assertDeployMarkerMatches(expected, expected), expected);
});
it('rejects deploy markers with a stale deploy id even when the source commit and timestamp match', () => {
const expected = buildDeployMarker(
'3333333333333333333333333333333333333333',
new Date('2026-06-20T10:00:00.000Z'),
'deploy-current'
);
const stale = {
...expected,
deploy_id: 'deploy-stale'
};
assert.throws(() => assertDeployMarkerMatches(stale, expected), /deploy_id mismatch/);
});
it('waits for the public service marker after management reports the new Space commit running', async () => {
const marker = buildDeployMarker(
'5555555555555555555555555555555555555555',
new Date('2026-06-20T12:00:00.000Z'),
'deploy-555'
);
let verifyCount = 0;
const runtime = await waitForRunning('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', marker, {
attempts: 2,
intervalMs: 0,
readInfo: () => ({
runtime: { stage: 'RUNNING' },
sha: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
}),
verifyMarker: async () => {
verifyCount += 1;
if (verifyCount === 1) throw new Error('deploy marker deploy_id mismatch');
return marker;
},
sleep: async () => {},
log: () => {}
});
assert.equal(verifyCount, 2);
assert.deepEqual(runtime, {
stage: 'RUNNING',
sha: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
service_marker_verified: true,
marker
});
});
it('retries a transient Hugging Face Space status transport error before verifying the new marker', async () => {
const marker = buildDeployMarker(
'6666666666666666666666666666666666666666',
new Date('2026-06-20T13:00:00.000Z'),
'deploy-666'
);
let readCount = 0;
let sleepCount = 0;
const runtime = await waitForRunning('bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb', marker, {
attempts: 2,
intervalMs: 0,
readInfo: () => {
readCount += 1;
if (readCount === 1) {
throw new Error(
'httpcore.ConnectError: [SSL: UNEXPECTED_EOF_WHILE_READING] EOF occurred in violation of protocol'
);
}
return {
runtime: { stage: 'RUNNING' },
sha: 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'
};
},
verifyMarker: async () => marker,
sleep: async () => {
sleepCount += 1;
},
log: () => {}
});
assert.equal(readCount, 2);
assert.equal(sleepCount, 1);
assert.deepEqual(runtime, {
stage: 'RUNNING',
sha: 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb',
service_marker_verified: true,
marker
});
});
it('does not sleep after the final transient management status error before verifying the marker', async () => {
const marker = buildDeployMarker(
'7777777777777777777777777777777777777777',
new Date('2026-06-20T14:00:00.000Z'),
'deploy-777'
);
let sleepCount = 0;
const runtime = await waitForRunning('cccccccccccccccccccccccccccccccccccccccc', marker, {
attempts: 1,
intervalMs: 10_000,
readInfo: () => {
throw new Error('httpx.ReadTimeout: timed out while reading Space status');
},
verifyMarker: async (expected) => assertDeployMarkerMatches(marker, expected),
sleep: async () => {
sleepCount += 1;
},
log: () => {}
});
assert.equal(sleepCount, 0);
assert.deepEqual(runtime, {
stage: 'unknown',
sha: 'unknown',
management_status: 'target_commit_not_confirmed',
verification_source: 'service_marker_after_management_timeout',
service_marker_verified: true,
warning:
'Hugging Face management status did not confirm RUNNING for cccccccccccccccccccccccccccccccccccccccc, but the public service returned the exact one-time deploy marker; last stage=unknown sha=unknown marker_error=unknown management_error=httpx.ReadTimeout: timed out while reading Space status',
marker
});
});
it('accepts an exact one-time service marker when management status remains stale', async () => {
const marker = buildDeployMarker(
'8888888888888888888888888888888888888888',
new Date('2026-06-20T15:00:00.000Z'),
'deploy-888'
);
let sleepCount = 0;
const runtime = await waitForRunning('dddddddddddddddddddddddddddddddddddddddd', marker, {
attempts: 1,
intervalMs: 0,
readInfo: () => ({ runtime: { stage: 'RUNNING' }, sha: 'stale-space-sha' }),
verifyMarker: async (expected) => assertDeployMarkerMatches(marker, expected),
sleep: async () => {
sleepCount += 1;
},
log: () => {}
});
assert.equal(sleepCount, 0);
assert.deepEqual(runtime, {
stage: 'RUNNING',
sha: 'stale-space-sha',
management_status: 'target_commit_not_confirmed',
verification_source: 'service_marker_after_management_timeout',
service_marker_verified: true,
warning:
'Hugging Face management status did not confirm RUNNING for dddddddddddddddddddddddddddddddddddddddd, but the public service returned the exact one-time deploy marker; last stage=RUNNING sha=stale-space-sha marker_error=unknown management_error=none',
marker
});
});
it('rejects a stale service marker after management status times out', async () => {
const marker = buildDeployMarker(
'9999999999999999999999999999999999999999',
new Date('2026-06-20T16:00:00.000Z'),
'deploy-999'
);
const staleMarker = { ...marker, deploy_id: 'deploy-stale' };
await assert.rejects(
waitForRunning('eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee', marker, {
attempts: 1,
intervalMs: 0,
readInfo: () => ({ runtime: { stage: 'RUNNING' }, sha: 'stale-space-sha' }),
verifyMarker: async (expected) => assertDeployMarkerMatches(staleMarker, expected),
sleep: async () => {},
log: () => {}
}),
/deploy_id mismatch/
);
});
it('only classifies transient Hugging Face management transport errors as retryable', () => {
assert.equal(
isRetryableSpaceInfoReadError(
new Error('httpcore.ConnectError: [SSL: UNEXPECTED_EOF_WHILE_READING] EOF occurred in violation of protocol')
),
true
);
assert.equal(isRetryableSpaceInfoReadError(new Error('hf spaces info failed with HTTP 401 Unauthorized')), false);
});
it('does not retry a non-transient Space status failure', async () => {
const marker = buildDeployMarker(
'7777777777777777777777777777777777777777',
new Date('2026-06-20T14:00:00.000Z'),
'deploy-777'
);
await assert.rejects(
waitForRunning('cccccccccccccccccccccccccccccccccccccccc', marker, {
attempts: 2,
intervalMs: 0,
readInfo: () => {
throw new Error('hf spaces info failed with HTTP 401 Unauthorized');
},
verifyMarker: async () => marker,
sleep: async () => {},
log: () => {}
}),
/HTTP 401 Unauthorized/
);
});
it('builds a no-store API route for service-side deploy marker verification', () => {
const marker = buildDeployMarker(
'4444444444444444444444444444444444444444',
new Date('2026-06-20T11:00:00.000Z'),
'deploy-444'
);
const source = buildDeployMarkerRouteSource(marker);
assert.match(source, /NextResponse\.json/);
assert.match(source, /Cache-Control/);
assert.match(source, /no-store/);
assert.match(source, /4444444444444444444444444444444444444444/);
assert.match(source, /deploy-444/);
assert.doesNotMatch(source, /key|token|secret|password/i);
});
it('keeps the generated deploy markers from being deleted on upload', () => {
assert.deepEqual(
findRemoteDeletePaths(
new Set(['README.md', 'public/hf-space-deploy-marker.json', 'src/app/api/deploy-marker/route.ts']),
['README.md', 'public/hf-space-deploy-marker.json', 'src/app/api/deploy-marker/route.ts', 'old.md']
),
['old.md']
);
});
});
|