Skip to content
← Public packages

@kentcdodds/x

X API v2 helpers for posts, search, DMs, and encrypted X Chat. Generic Activity CRC/HMAC utilities only — @kodykoala Pam wake lives in @kentcdodds/kodykoala-activity.

FollowFork

Files

script-recovery.test.mjs

90 lines · 2.8 KB · JavaScript
import { test } from 'node:test'
import assert from 'node:assert/strict'
import { buildModule } from '../scripts/upload-to-x-media.mjs'
const AsyncFunction = Object.getPrototypeOf(async () => {}).constructor
const base = {
	sourceHash: 'hash',
	account: null,
	size: 3,
	chunkBytes: 3,
	mediaType: 'video/mp4',
	mediaCategory: 'tweet_video',
	reportUrl: 'report',
	baseUrl: 'source',
	state: null,
}
function fixture(overrides = {}) {
	let init = 0,
		final = 0
	const reports = []
	const api = {
		getMediaAccountIdentity: async () => ({ accountId:'actor' }),
		initializeMediaUpload: async () => {
			init++
			return { mediaId: '123', integration: 'x' }
		},
		appendMediaUploadSegment: async () => {},
		finalizeMediaUpload: async () => {
			final++
			throw new Error('lost response')
		},
		getMediaUploadStatus: async () => ({
			state: 'succeeded',
			processingInfo: { state: 'succeeded' },
		}),
		waitForMediaProcessing: async () => {},
		setMediaMetadata: async () => {},
		fetch: async (u, i) => {
			if (u === 'report') {
				reports.push(JSON.parse(i.body))
				return new Response(null, { status: 204 })
			}
			return new Response(new Uint8Array(3), { status: 206 })
		},
		...overrides,
	}
	let code =
		buildModule('synthetic')
			.replace(/import[\s\S]*?from 'kody:[^']+'\n/g, '')
			.replace('export default async function main', 'async function main') + '\nreturn main(p)'
	let fn = new AsyncFunction('p', ...Object.keys(api), code)
	return { run: (p) => fn(p, ...Object.values(api)), reports, counts: () => ({ init, final }) }
}
test('lost FINALIZE saves intent; restart uses STATUS only', async () => {
	const f = fixture()
	await assert.rejects(f.run(base), /RECONCILIATION_REQUIRED/)
	const state = f.reports.findLast((x) => x.state)?.state
	assert.equal(state.phase, 'finalizing')
	assert.equal((await f.run({ ...base, state })).done, true)
	assert.deepEqual(f.counts(), { init: 1, final: 1 })
})
test('ambiguous STATUS without explicit processing evidence fails closed', async () => {
	const f = fixture({
		getMediaUploadStatus: async () => ({ state: 'succeeded', processingInfo: null }),
	})
	await assert.rejects(
		f.run({
			...base,
			state: {
				mediaId: '123',
				sourceHash: 'hash',
				accountId: 'actor',
				phase: 'finalizing',
				nextOffset: 3,
			},
		}),
		/unverified/,
	)
	assert.deepEqual(f.counts(), { init: 0, final: 0 })
})
test('wrong account/source and unknown initialization never create another session', async () => {
	for (const state of [
		{ mediaId: '123', sourceHash: 'other', accountId: 'actor' },
		{ mediaId: '123', sourceHash: 'hash', accountId: 'other' },
		{ phase: 'starting', sourceHash: 'hash', accountId: 'actor' },
	]) {
		const f = fixture()
		await assert.rejects(f.run({ ...base, state }), /RECONCILIATION_REQUIRED/)
		assert.equal(f.counts().init, 0)
	}
})