Every analytics tool wants a subscription. And your data. I built Twiligent instead. YouTube stats. Instagram stats. One dashboard. Runs locally on your machine.
feat(youtube): upload videos in resumable chunks from scheduled runs
spacesdrive26e1df05bdf1
diff --git a/backend/services/cloudinary/cloudinaryApi.ts b/backend/services/cloudinary/cloudinaryApi.tsindex c15c0f9..52fc9bc 100644--- a/backend/services/cloudinary/cloudinaryApi.ts+++ b/backend/services/cloudinary/cloudinaryApi.ts@@ -54,3 +54,47 @@ export async function destroyFile(config: CloudinaryAdminConfig, publicId: strinconst answer = await post<{ result?: string }>(`${CLOUDINARY_API}/${config.cloudName}/${resourceType}/destroy`, body, signal);if (answer.result !== 'ok' && answer.result !== 'not found') throw new UpstreamError(`Cloudinary did not delete the file (${answer.result ?? 'no answer'})`);}++// the runner reads only the instance's own Cloudinary files, so a changed row can never point the Worker elsewhere+function isCloudinaryFile(url: URL): boolean {+ return url.protocol === 'https:' && (url.hostname === 'res.cloudinary.com' || url.hostname.endsWith('.cloudinary.com'));+}++// one part of a stored file, exactly first..last; anything else is an error, since a short part would corrupt the upload+export async function fetchFileRange(fileUrl: string, first: number, last: number, signal?: AbortSignal): Promise<ArrayBuffer> {+ const url = new URL(fileUrl);+ if (!isCloudinaryFile(url)) throw new UpstreamError('The video is not stored on Cloudinary, so it cannot be read in parts.');+ let response: Response;+ try {+ response = await fetch(url, { headers: { Range: `bytes=${first}-${last}` }, signal });+ } catch (err) {+ if (isAbortError(err)) throw new UpstreamTimeoutError('Cloudinary', err);+ throw new UpstreamError(`Cloudinary could not be reached (${(err as Error).message})`, { reason: 'unreachable', cause: err });+ }+ if (response.status === 404) throw new UpstreamError('The video is no longer on Cloudinary. Upload it again.', { status: 404 });+ if (response.status !== 206 && !(response.status === 200 && first === 0)) {+ throw new UpstreamError(`Cloudinary did not return the part of the video asked for (status ${response.status})`, { reason: 'unreachable', status: response.status });+ }+ const part = await response.arrayBuffer();+ const wanted = last - first + 1;+ // a 200 for the first part holds the whole file; anything else must be exactly the range+ if (part.byteLength < wanted) throw new UpstreamError('Cloudinary returned less of the video than asked for', { reason: 'unreachable' });+ return part.byteLength === wanted ? part : part.slice(0, wanted);+}++// a small stored file read whole, such as a thumbnail, with the same Cloudinary-only rule as fetchFileRange+export async function fetchWholeFile(fileUrl: string, maxBytes: number, signal?: AbortSignal): Promise<ArrayBuffer> {+ const url = new URL(fileUrl);+ if (!isCloudinaryFile(url)) throw new UpstreamError('The file is not stored on Cloudinary.');+ let response: Response;+ try {+ response = await fetch(url, { signal });+ } catch (err) {+ if (isAbortError(err)) throw new UpstreamTimeoutError('Cloudinary', err);+ throw new UpstreamError(`Cloudinary could not be reached (${(err as Error).message})`, { reason: 'unreachable', cause: err });+ }+ if (!response.ok) throw new UpstreamError(`Cloudinary could not return the file (status ${response.status})`, { status: response.status });+ const file = await response.arrayBuffer();+ if (file.byteLength > maxBytes) throw new UpstreamError(`The file is larger than ${Math.round(maxBytes / 1024 / 1024)} MB.`);+ return file;+}diff --git a/backend/services/youtubeUploadRunner.ts b/backend/services/youtubeUploadRunner.tsnew file mode 100644index 0000000..5a30340--- /dev/null+++ b/backend/services/youtubeUploadRunner.ts@@ -0,0 +1,234 @@+import { fetchFileRange, fetchWholeFile } from './cloudinary/cloudinaryApi.ts';+import { GoogleApiError, querySession, sendChunk, setThumbnail, startUploadSession, videoStatus } from './youtube/googleApi.ts';+import {+ DEFAULT_UNITS_PER_DAY, MAX_UPLOAD_ATTEMPTS, UNITS, UPLOAD_LEASE_SECONDS, UPLOADS_PER_DAY,+ classifyGoogleFailure, confirmedFromRange, nextChunk, nextPacificMidnight, retryDelayMs,+} from '../lib/youtubeUpload.ts';+import { pacificDay } from '../lib/youtubeQuota.ts';+import { DatabaseError, UpstreamError, UpstreamTimeoutError } from '../lib/errors.ts';+import { errorFields } from '../lib/logger.ts';+import type { YouTubeConnectService } from './youtubeConnectService.ts';+import type { StoredYouTubeUpload, UploadProgress, YouTubeUploadRepository } from '../repositories/youtubeUploadRepository.ts';+import type { YouTubeConnectionRepository } from '../repositories/youtubeConnectionRepository.ts';+import type { QuotaBucket, YouTubeQuotaLedgerRepository } from '../repositories/youtubeQuotaLedgerRepository.ts';+import type { RequestContext } from '../types/context.ts';+import type { YouTubeUploadState } from '../../shared/types/youtubePublishing.ts';++export interface YouTubeRunnerDeps {+ uploads: YouTubeUploadRepository;+ connections: YouTubeConnectionRepository;+ connect: YouTubeConnectService;+ ledger: YouTubeQuotaLedgerRepository;+ context: RequestContext;+}++export interface YouTubeRunBudget {+ // subrequests this run may still make, after what its invocation already spent+ subrequests: number;+ timeBudgetMs: number;+}++export interface YouTubeRunOutcome {+ jobId: string | null;+ state: YouTubeUploadState | null;+ chunks: number;+ subrequests: number;+}++export interface YouTubeUploadRunner {+ run(budget: YouTubeRunBudget): Promise<YouTubeRunOutcome>;+}++// processing a video takes minutes; checking more often only spends quota+const PROCESSING_CHECK_MS = 3 * 60 * 1000;+// one chunk is a Cloudinary read, a Google write and a progress save+const SUBREQUESTS_PER_CHUNK = 3;+// a chunk is not started without this much time left, so the run never stops halfway through a write+const CHUNK_TIME_MS = 60 * 1000;+// YouTube takes custom thumbnails up to 2 MB+const THUMBNAIL_MAX_BYTES = 2 * 1024 * 1024;+const LOST_ANSWER = 'YouTube received the whole video, but its answer was lost and the upload address no longer works. Check your channel before trying again, so the video is not uploaded twice.';++// stopped the run for a reason that is not an error, with the job left as it is+class LeaseLost extends Error {}++// one job per run, moved as far as the run's subrequests and time allow; every write checks this run still holds the job+export function createYouTubeUploadRunner({ uploads, connections, connect, ledger, context }: YouTubeRunnerDeps): YouTubeUploadRunner {+ const { signal, log, requestId: owner } = context;++ return {+ async run({ subrequests, timeBudgetMs }) {+ const started = Date.now();+ let spent = 1;+ const claimed = await uploads.claim(owner, UPLOAD_LEASE_SECONDS);+ if (!claimed) return { jobId: null, state: null, chunks: 0, subrequests: spent };+ const job: StoredYouTubeUpload = claimed;++ let chunks = 0;+ let state: YouTubeUploadState = job.state;+ let session = job.sessionUri;+ let confirmed = job.bytesConfirmed;+ let videoId = job.videoId;++ const canAfford = (calls: number) => spent + calls <= subrequests - 1;+ const timeLeft = () => timeBudgetMs - (Date.now() - started);+ async function save(progress: UploadProgress): Promise<void> {+ spent++;+ if (!(await uploads.saveLeased(job.id, owner, progress))) throw new LeaseLost();+ }+ async function release(progress: UploadProgress): Promise<void> {+ spent++;+ await uploads.releaseLeased(job.id, owner, progress);+ state = progress.state ?? state;+ }+ async function reserve(bucket: QuotaBucket, units: number): Promise<boolean> {+ spent++;+ const ok = await ledger.reserve(pacificDay(), bucket, units, bucket === 'uploads' ? UPLOADS_PER_DAY : DEFAULT_UNITS_PER_DAY);+ if (!ok) await release({ state: 'waiting_quota', nextAttemptAt: nextPacificMidnight().toISOString(), lastError: 'The YouTube allowance for today is used up; the upload continues after midnight Pacific time.' });+ return ok;+ }++ try {+ spent++;+ const connection = await connections.findById(job.connectionId);+ if (!connection) {+ await release({ state: 'failed', lastError: 'The YouTube connection for this upload was removed.' });+ return { jobId: job.id, state, chunks, subrequests: spent };+ }+ spent += 2;+ const token = await connect.accessTokenFor(connection);+ if (!token) {+ await release({ state: 'needs_reconnect', lastError: 'Google no longer accepts this channel\'s sign-in. Connect YouTube again on the Accounts page.' });+ return { jobId: job.id, state, chunks, subrequests: spent };+ }++ // a job that waited for the quota goes back to where it was+ if (state === 'waiting_quota') state = job.videoId ? 'processing' : session ? 'uploading' : 'queued';++ if (state === 'queued') {+ if (!(await reserve('uploads', 1))) return { jobId: job.id, state, chunks, subrequests: spent };+ spent++;+ session = await startUploadSession(token, job, job.sourceBytes, job.contentType, signal);+ // saved before any byte is sent, so a run that dies here resumes this session instead of starting a second video+ await save({ sessionUri: session, state: 'uploading', bytesConfirmed: 0 });+ state = 'uploading';+ confirmed = 0;+ }++ if (state === 'uploading' && session) {+ spent++;+ const status = await querySession(token, session, job.sourceBytes, signal);+ if (status.kind === 'done') {+ videoId = status.videoId;+ state = 'processing';+ await save({ state, videoId, bytesConfirmed: job.sourceBytes });+ } else {+ confirmed = confirmedFromRange(status.range);+ }+ while (state === 'uploading' && canAfford(SUBREQUESTS_PER_CHUNK) && timeLeft() > CHUNK_TIME_MS) {+ const range = nextChunk(confirmed, job.sourceBytes);+ if (!range) break;+ spent++;+ const part = await fetchFileRange(job.sourceUrl, range.first, range.last, signal);+ spent++;+ const result = await sendChunk(token, session, part, range.first, job.sourceBytes, signal);+ chunks++;+ if (result.kind === 'done') {+ videoId = result.videoId;+ state = 'processing';+ await save({ state, videoId, bytesConfirmed: job.sourceBytes, attempts: 0, lastError: null });+ } else {+ confirmed = confirmedFromRange(result.range);+ await save({ bytesConfirmed: confirmed, attempts: 0, lastError: null });+ }+ }+ if (state === 'uploading') {+ await release({ state, nextAttemptAt: new Date().toISOString() });+ return { jobId: job.id, state, chunks, subrequests: spent };+ }+ // processing has only just started; the next run checks on it+ await release({ state: 'processing', nextAttemptAt: new Date(Date.now() + PROCESSING_CHECK_MS).toISOString() });+ return { jobId: job.id, state, chunks, subrequests: spent };+ }++ if (state === 'processing' && videoId) {+ if (!(await reserve('default', UNITS.videosList))) return { jobId: job.id, state, chunks, subrequests: spent };+ spent++;+ const status = await videoStatus(token, videoId, signal);+ if (!status) {+ await release({ state: 'failed', lastError: 'YouTube no longer has this video. It may have been deleted on the channel.' });+ } else if (['failed', 'rejected', 'deleted'].includes(status.uploadStatus)) {+ await release({ state: 'failed', lastError: `YouTube did not accept the video (${status.failureReason ?? status.uploadStatus}).` });+ } else if (status.uploadStatus === 'processed' || status.processingStatus === 'succeeded') {+ state = job.thumbnailUrl ? 'finalizing' : 'done';+ if (state === 'done') await release({ state, lastError: null });+ } else {+ await release({ state: 'processing', nextAttemptAt: new Date(Date.now() + PROCESSING_CHECK_MS).toISOString() });+ }+ if (state !== 'finalizing') return { jobId: job.id, state, chunks, subrequests: spent };+ }++ if (state === 'finalizing' && videoId && job.thumbnailUrl) {+ if (!(await reserve('default', UNITS.thumbnailSet))) return { jobId: job.id, state, chunks, subrequests: spent };+ spent += 2;+ try {+ const image = await fetchWholeFile(job.thumbnailUrl, THUMBNAIL_MAX_BYTES, signal);+ await setThumbnail(token, videoId, image, job.thumbnailUrl.endsWith('.png') ? 'image/png' : 'image/jpeg', signal);+ await release({ state: 'done', lastError: null });+ } catch (err) {+ // the video is up; a thumbnail YouTube refuses (an unverified channel, say) never undoes that+ if (err instanceof GoogleApiError && classifyGoogleFailure(err.httpStatus, err.googleReason) === 'permanent') {+ await release({ state: 'done', lastError: `The video is published, but YouTube did not take the thumbnail: ${err.message}` });+ } else {+ throw err;+ }+ }+ return { jobId: job.id, state, chunks, subrequests: spent };+ }++ await release({ state: 'failed', lastError: 'The upload was in a state the runner could not continue from.' });+ return { jobId: job.id, state, chunks, subrequests: spent };+ } catch (err) {+ if (err instanceof LeaseLost) {+ log.warn('youtube upload lease lost', { jobId: job.id });+ return { jobId: job.id, state, chunks, subrequests: spent };+ }+ await settleFailure(job, err, { state, confirmed });+ return { jobId: job.id, state, chunks, subrequests: spent };+ }++ // decides what happens to the job after an error; never throws, so the cron run always finishes+ async function settleFailure(failed: StoredYouTubeUpload, err: unknown, at: { state: YouTubeUploadState; confirmed: number }): Promise<void> {+ const message = (err as Error).message ?? String(err);+ const kind = err instanceof GoogleApiError+ ? classifyGoogleFailure(err.httpStatus, err.googleReason)+ : err instanceof UpstreamTimeoutError || err instanceof DatabaseError || (err instanceof UpstreamError && err.reason === 'unreachable')+ ? 'transient'+ : 'permanent';+ log.warn('youtube upload step failed', { jobId: failed.id, kind, ...errorFields(err) });+ try {+ if (kind === 'quota') {+ await release({ state: 'waiting_quota', nextAttemptAt: nextPacificMidnight().toISOString(), lastError: 'YouTube\'s daily allowance is used up; the upload continues after midnight Pacific time.' });+ } else if (kind === 'reconnect') {+ await connections.markNeedsReconnectById(failed.connectionId);+ await release({ state: 'needs_reconnect', lastError: 'Google no longer accepts this channel\'s sign-in. Connect YouTube again on the Accounts page.' });+ } else if (kind === 'session_gone') {+ // with every byte sent the video may exist; a person checks before a second upload+ if (at.confirmed >= failed.sourceBytes) await release({ state: 'needs_review', lastError: LOST_ANSWER });+ else await release({ state: 'queued', sessionUri: null, bytesConfirmed: 0, nextAttemptAt: new Date().toISOString(), lastError: 'The upload address expired; the upload starts again.' });+ } else if (kind === 'transient') {+ const attempts = failed.attempts + 1;+ if (attempts >= MAX_UPLOAD_ATTEMPTS) await release({ state: 'failed', attempts, lastError: `Stopped after ${attempts} tries: ${message}` });+ else await release({ state: at.state === 'queued' ? 'queued' : at.state, attempts, nextAttemptAt: new Date(Date.now() + retryDelayMs(attempts)).toISOString(), lastError: message });+ } else {+ await release({ state: 'failed', attempts: failed.attempts + 1, lastError: message });+ }+ } catch (saveErr) {+ // the lease runs out and the next run picks the job up again+ log.error('youtube upload outcome could not be saved', { jobId: failed.id, ...errorFields(saveErr) });+ }+ }+ },+ };+}+diff --git a/backend/tests/services/youtubeUploadRunner.test.ts b/backend/tests/services/youtubeUploadRunner.test.tsnew file mode 100644index 0000000..7e6e347--- /dev/null+++ b/backend/tests/services/youtubeUploadRunner.test.ts@@ -0,0 +1,176 @@+import { test, mock } from 'node:test';+import assert from 'node:assert/strict';+import { createYouTubeUploadRunner } from '../../services/youtubeUploadRunner.ts';+import { CHUNK_BYTES, MAX_UPLOAD_ATTEMPTS } from '../../lib/youtubeUpload.ts';+import { createLogger } from '../../lib/logger.ts';+import type { StoredYouTubeUpload, UploadProgress, YouTubeUploadRepository } from '../../repositories/youtubeUploadRepository.ts';+import type { StoredYouTubeConnection, YouTubeConnectionRepository } from '../../repositories/youtubeConnectionRepository.ts';+import type { YouTubeQuotaLedgerRepository } from '../../repositories/youtubeQuotaLedgerRepository.ts';+import type { YouTubeConnectService } from '../../services/youtubeConnectService.ts';++const SESSION = 'https://www.googleapis.com/upload/youtube/v3/videos?upload_id=abc';+const SOURCE = 'https://res.cloudinary.com/demo/video/upload/v1/clip.mp4';++function job(extra: Partial<StoredYouTubeUpload> = {}): StoredYouTubeUpload {+ return {+ id: 'job-1', userId: 'user-1', connectionId: 'conn-1', title: 'Launch', description: '', tags: [], categoryId: '22',+ privacy: 'private', publishAt: null, madeForKids: false, syntheticMedia: false, notifySubscribers: true, isShort: false,+ hasThumbnail: false, state: 'queued', bytesConfirmed: 0, sourceBytes: 10, videoId: null, attempts: 0,+ nextAttemptAt: '2026-11-05T00:00:00Z', lastError: null, createdAt: '2026-11-05T00:00:00Z', updatedAt: '2026-11-05T00:00:00Z',+ sourceUrl: SOURCE, contentType: 'video/mp4', thumbnailUrl: null, sessionUri: null, ...extra,+ };+}++function harness(start: StoredYouTubeUpload | null, options: { token?: string | null; quota?: boolean; leaseHeld?: boolean } = {}) {+ const saves: UploadProgress[] = [];+ let released: UploadProgress | null = null;+ let reconnectMarked = false;+ const uploads = {+ claim: async () => start,+ saveLeased: async (_id: string, _owner: string, progress: UploadProgress) => { saves.push(progress); return options.leaseHeld ?? true; },+ releaseLeased: async (_id: string, _owner: string, progress: UploadProgress) => { released = progress; },+ } as unknown as YouTubeUploadRepository;+ const connection = { id: 'conn-1', status: 'active' } as StoredYouTubeConnection;+ const connections = {+ findById: async () => connection,+ markNeedsReconnectById: async () => { reconnectMarked = true; },+ } as unknown as YouTubeConnectionRepository;+ const connect = { accessTokenFor: async () => (options.token === undefined ? 'ACCESS' : options.token) } as unknown as YouTubeConnectService;+ const ledger: YouTubeQuotaLedgerRepository = { reserve: async () => options.quota ?? true };+ const context = { requestId: 'run-1', signal: new AbortController().signal, log: createLogger() };+ const runner = createYouTubeUploadRunner({ uploads, connections, connect, ledger, context });+ return { runner, saves, released: () => released as UploadProgress | null, reconnectMarked: () => reconnectMarked };+}++type Answer = (url: string, init: RequestInit) => Response;++function network(t: { after(fn: () => void): void }, answer: Answer) {+ const calls: { url: string; method: string; headers: Record<string, string> }[] = [];+ const fetchMock = mock.method(globalThis, 'fetch', async (input: string | URL, init: RequestInit = {}) => {+ const url = String(input);+ calls.push({ url, method: init.method ?? 'GET', headers: Object.fromEntries(new Headers(init.headers).entries()) });+ return answer(url, init);+ });+ t.after(() => fetchMock.mock.restore());+ return calls;+}++const quiet = (t: { after(fn: () => void): void }) => {+ const warn = mock.method(console, 'warn', () => undefined);+ t.after(() => warn.mock.restore());+};++test('a queued job opens a session, saves it before sending bytes, uploads the video and waits for processing', async t => {+ const calls = network(t, (url, init) => {+ if (url.includes('uploadType=resumable')) return new Response(null, { status: 200, headers: { Location: SESSION } });+ if (url === SOURCE) return new Response(new Uint8Array(10), { status: 206 });+ if (url === SESSION && new Headers(init.headers).get('Content-Range') === 'bytes */10') return new Response(null, { status: 308 });+ if (url === SESSION) return Response.json({ id: 'vid-1' }, { status: 201 });+ return new Response('unexpected', { status: 500 });+ });+ const { runner, saves, released } = harness(job());+ const outcome = await runner.run({ subrequests: 50, timeBudgetMs: 600_000 });+ assert.deepEqual(saves[0], { sessionUri: SESSION, state: 'uploading', bytesConfirmed: 0 });+ const sessionStart = calls.findIndex(call => call.url.includes('uploadType=resumable'));+ const firstByte = calls.findIndex(call => call.url === SOURCE);+ assert.ok(sessionStart < firstByte);+ assert.equal(calls.find(call => call.url === SESSION && call.headers['content-range'] === 'bytes 0-9/10')?.method, 'PUT');+ assert.equal(outcome.chunks, 1);+ assert.equal(released()?.state, 'processing');+ assert.deepEqual(saves.at(-1), { state: 'processing', videoId: 'vid-1', bytesConfirmed: 10, attempts: 0, lastError: null });+});++test('an upload resumes where Google says it stopped and stops at the run\'s budget, ready for the next run', async t => {+ const total = CHUNK_BYTES * 3;+ let sent = 0;+ network(t, (url, init) => {+ const range = new Headers(init.headers).get('Content-Range') ?? '';+ if (url === SOURCE) return new Response(new Uint8Array(CHUNK_BYTES), { status: 206 });+ if (range === `bytes */${total}`) return new Response(null, { status: 308, headers: { Range: `bytes=0-${CHUNK_BYTES - 1}` } });+ sent++;+ return new Response(null, { status: 308, headers: { Range: `bytes=0-${CHUNK_BYTES * (1 + sent) - 1}` } });+ });+ const { runner, saves, released } = harness(job({ state: 'uploading', sessionUri: SESSION, sourceBytes: total, bytesConfirmed: 0 }));+ // claim, connection, token (2) and the session check leave room for one chunk of three before the reserve of one+ const outcome = await runner.run({ subrequests: 10, timeBudgetMs: 600_000 });+ assert.equal(outcome.chunks, 1);+ assert.equal(saves[0].bytesConfirmed, CHUNK_BYTES * 2);+ assert.equal(released()?.state, 'uploading');+});++test('a used-up daily allowance makes the job wait until after midnight Pacific time', async t => {+ network(t, () => new Response('should not be called', { status: 500 }));+ const { runner, released } = harness(job(), { quota: false });+ await runner.run({ subrequests: 50, timeBudgetMs: 600_000 });+ assert.equal(released()?.state, 'waiting_quota');+ assert.ok(Date.parse(released()?.nextAttemptAt ?? '') > Date.now());+});++test('a connection Google stopped accepting parks the job without spending an attempt', async t => {+ network(t, () => new Response('should not be called', { status: 500 }));+ const { runner, released } = harness(job(), { token: null });+ await runner.run({ subrequests: 50, timeBudgetMs: 600_000 });+ assert.equal(released()?.state, 'needs_reconnect');+ assert.equal(released()?.attempts, undefined);+});++test('a session that vanished after every byte was sent goes to a person, never to a second upload', async t => {+ quiet(t);+ network(t, () => Response.json({ error: { code: 404, message: 'Not found', errors: [{ reason: 'notFound' }] } }, { status: 404 }));+ const finished = harness(job({ state: 'uploading', sessionUri: SESSION, bytesConfirmed: 10 }));+ await finished.runner.run({ subrequests: 50, timeBudgetMs: 600_000 });+ assert.equal(finished.released()?.state, 'needs_review');+ const partial = harness(job({ state: 'uploading', sessionUri: SESSION, bytesConfirmed: 0, sourceBytes: 20 }));+ await partial.runner.run({ subrequests: 50, timeBudgetMs: 600_000 });+ assert.deepEqual({ state: partial.released()?.state, sessionUri: partial.released()?.sessionUri, bytes: partial.released()?.bytesConfirmed }, { state: 'queued', sessionUri: null, bytes: 0 });+});++test('a passing error backs off and counts an attempt; the last attempt fails the job; a refused upload fails at once', async t => {+ quiet(t);+ network(t, () => Response.json({ error: { code: 503, message: 'Backend Error' } }, { status: 503 }));+ const first = harness(job());+ await first.runner.run({ subrequests: 50, timeBudgetMs: 600_000 });+ assert.equal(first.released()?.state, 'queued');+ assert.equal(first.released()?.attempts, 1);+ const last = harness(job({ attempts: MAX_UPLOAD_ATTEMPTS - 1 }));+ await last.runner.run({ subrequests: 50, timeBudgetMs: 600_000 });+ assert.equal(last.released()?.state, 'failed');+ mock.restoreAll();+ quiet(t);+ network(t, () => Response.json({ error: { code: 400, message: 'Invalid title', errors: [{ reason: 'invalidTitle' }] } }, { status: 400 }));+ const refused = harness(job());+ await refused.runner.run({ subrequests: 50, timeBudgetMs: 600_000 });+ assert.equal(refused.released()?.state, 'failed');+ assert.match(refused.released()?.lastError ?? '', /Invalid title/);+});++test('a processed video is done, or moves on to its thumbnail; a thumbnail YouTube refuses never undoes the upload', async t => {+ network(t, url => {+ if (url.includes('/videos?part=status')) return Response.json({ items: [{ id: 'vid-1', status: { uploadStatus: 'processed' }, processingDetails: { processingStatus: 'succeeded' } }] });+ if (url.endsWith('thumb.jpg')) return new Response(new Uint8Array(100), { status: 200 });+ if (url.includes('/thumbnails/set')) return Response.json({ error: { code: 403, message: 'The channel cannot set custom thumbnails', errors: [{ reason: 'forbidden' }] } }, { status: 403 });+ return new Response('unexpected', { status: 500 });+ });+ const plain = harness(job({ state: 'processing', videoId: 'vid-1', bytesConfirmed: 10 }));+ await plain.runner.run({ subrequests: 50, timeBudgetMs: 600_000 });+ assert.equal(plain.released()?.state, 'done');+ const withThumbnail = harness(job({ state: 'processing', videoId: 'vid-1', bytesConfirmed: 10, thumbnailUrl: 'https://res.cloudinary.com/demo/image/upload/thumb.jpg', hasThumbnail: true }));+ await withThumbnail.runner.run({ subrequests: 50, timeBudgetMs: 600_000 });+ assert.equal(withThumbnail.released()?.state, 'done');+ assert.match(withThumbnail.released()?.lastError ?? '', /did not take the thumbnail/);+});++test('a run that lost its lease stops without writing the job again', async t => {+ network(t, (url, init) => {+ if (url.includes('uploadType=resumable')) return new Response(null, { status: 200, headers: { Location: SESSION } });+ return new Response(null, { status: init.method === 'PUT' ? 308 : 500 });+ });+ const { runner, released } = harness(job(), { leaseHeld: false });+ await runner.run({ subrequests: 50, timeBudgetMs: 600_000 });+ assert.equal(released(), null);+});++test('with no job due, the run only makes the claim', async () => {+ const { runner } = harness(null);+ assert.deepEqual(await runner.run({ subrequests: 50, timeBudgetMs: 600_000 }), { jobId: null, state: null, chunks: 0, subrequests: 1 });+});