Spaces:
Running
Running
File size: 10,366 Bytes
3e8ea5d 9d44811 6825c0c 4d6d6c8 5599c48 9d44811 6825c0c 9d44811 3e8ea5d 9d44811 4d6d6c8 6825c0c 4d6d6c8 6825c0c 4d6d6c8 6825c0c 4d6d6c8 6825c0c 5599c48 6825c0c 5599c48 6825c0c 5599c48 4d6d6c8 5599c48 4d6d6c8 5599c48 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 | import assert from 'node:assert/strict';
import { describe, it } from 'node:test';
import {
GIT_ARCHIVE_MAX_BUFFER_BYTES,
assertDeployMarkerMatches,
buildDeployMarker,
buildDeployMarkerRouteSource,
buildUploadArgs,
extractUploadCommitSha,
findRemoteDeletePaths,
parseRepositorySlug,
waitForRunning
} from './deploy-hf-space.mjs';
describe('HF Space deploy script', () => {
it('extracts the Space commit SHA from hf upload JSON output', () => {
const sha = extractUploadCommitSha(
[
'Start hashing 189 files.',
'{"url":"https://huggingface.co/spaces/misonL/gpt-image-playground-customer/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/gpt-image-playground-customer/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/gpt-image-playground-customer'
});
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/gpt-image-playground-customer@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/gpt-image-playground-customer.git'), 'MisonL/gpt-image-playground-customer');
assert.equal(parseRepositorySlug('git@github.com:MisonL/gpt-image-playground-customer.git'), 'MisonL/gpt-image-playground-customer');
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('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('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']
);
});
});
|