Spaces:
Configuration error

File size: 8,342 Bytes
023064b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import assert from 'node:assert/strict';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import test from 'node:test';
import { runBatch, readManifestSafe, summarize, type BatchJobInput, type BatchJobResult } from './batch-queue';
import { makeWorkspaceTempDir, resolveWorkspaceTempPath } from '../../shared/runtime/paths.js';

function makeInputs(count: number): BatchJobInput[] {
    return Array.from({ length: count }, (_, index) => ({
        id: `job_${index}`,
        index,
        title: `Job ${index}`,
    }));
}

function tmpManifestPath(): string {
    return path.join(makeWorkspaceTempDir('avg-batch-'), 'batch-manifest.json');
}

test('caps active jobs at N (concurrency)', async () => {
    const maxActive = 2;
    let active = 0;
    let peak = 0;

    const manifest = await runBatch(
        makeInputs(6),
        {
            concurrency: maxActive,
            maxRetries: 0,
            executeJob: async () => {
                active += 1;
                peak = Math.max(peak, active);
                await new Promise((r) => setTimeout(r, 10));
                active -= 1;
                return { outcome: 'completed' as const };
            },
        },
        tmpManifestPath(),
    );

    assert.equal(peak, maxActive, 'peak active concurrency never exceeds the cap');
    assert.equal(manifest.jobs.length, 6);
    assert.ok(manifest.jobs.every((j) => j.outcome === 'completed'));
});

test('honors MAX_CONCURRENT_JOBS default when no concurrency passed', async () => {
    const prev = process.env.MAX_CONCURRENT_JOBS;
    process.env.MAX_CONCURRENT_JOBS = '3';
    try {
        let active = 0;
        let peak = 0;
        await runBatch(
            makeInputs(5),
            {
                maxRetries: 0,
                executeJob: async () => {
                    active += 1;
                    peak = Math.max(peak, active);
                    await new Promise((r) => setTimeout(r, 5));
                    active -= 1;
                    return { outcome: 'completed' as const };
                },
            },
            tmpManifestPath(),
        );
        assert.equal(peak, 3, 'defaults to MAX_CONCURRENT_JOBS');
    } finally {
        if (prev === undefined) {
            delete process.env.MAX_CONCURRENT_JOBS;
        } else {
            process.env.MAX_CONCURRENT_JOBS = prev;
        }
    }
});

test('retries transient failures up to AVG_BATCH_MAX_RETRIES then marks failed', async () => {
    process.env.AVG_BATCH_MAX_RETRIES = '2';
    try {
        const attemptsByJob = new Map<string, number>();
        const manifest = await runBatch(
            makeInputs(1),
            {
                concurrency: 1,
                executeJob: async (job) => {
                    const n = (attemptsByJob.get(job.id) ?? 0) + 1;
                    attemptsByJob.set(job.id, n);
                    return { outcome: 'failed' as const, error: 'transient network error' };
                },
            },
            tmpManifestPath(),
        );

        assert.equal(manifest.jobs[0].attempts, 3, '1 initial + 2 retries');
        assert.equal(manifest.jobs[0].outcome, 'failed');
        assert.match(manifest.jobs[0].error ?? '', /transient/i);
    } finally {
        delete process.env.AVG_BATCH_MAX_RETRIES;
    }
});

test('marks permanent (schema) failures without retrying', async () => {
    process.env.AVG_BATCH_MAX_RETRIES = '3';
    try {
        const attemptsByJob = new Map<string, number>();
        const manifest = await runBatch(
            makeInputs(1),
            {
                concurrency: 1,
                executeJob: async (job) => {
                    const n = (attemptsByJob.get(job.id) ?? 0) + 1;
                    attemptsByJob.set(job.id, n);
                    return { outcome: 'failed' as const, error: 'Schema validation failed' };
                },
            },
            tmpManifestPath(),
        );

        assert.equal(manifest.jobs[0].attempts, 1, 'permanent failures are not retried');
        assert.equal(manifest.jobs[0].outcome, 'failed');
    } finally {
        delete process.env.AVG_BATCH_MAX_RETRIES;
    }
});

test('--resume reprocesses only failed/pending, skipping completed', async () => {
    const manifestPath = tmpManifestPath();

    // First run: job_0 completes, job_1 fails.
    const first = await runBatch(
        makeInputs(2),
        {
            concurrency: 1,
            maxRetries: 0,
            executeJob: async (job) =>
                job.id === 'job_0'
                    ? ({ outcome: 'completed' as const, outputPath: '/out/0.mp4' } as BatchJobResult)
                    : ({ outcome: 'failed' as const, error: 'boom' } as BatchJobResult),
        },
        manifestPath,
    );
    assert.equal(first.jobs.find((j) => j.id === 'job_0')!.outcome, 'completed');
    assert.equal(first.jobs.find((j) => j.id === 'job_1')!.outcome, 'failed');

    let job0Executions = 0;
    let job1Executions = 0;
    const second = await runBatch(
        makeInputs(2),
        {
            concurrency: 1,
            maxRetries: 0,
            resume: true,
            executeJob: async (job) => {
                if (job.id === 'job_0') {
                    job0Executions += 1;
                    return { outcome: 'completed' as const, outputPath: '/out/0.mp4' };
                }
                job1Executions += 1;
                return { outcome: 'completed' as const, outputPath: '/out/1.mp4' };
            },
        },
        manifestPath,
    );

    assert.equal(job0Executions, 0, 'completed job_0 is skipped on resume');
    assert.equal(job1Executions, 1, 'failed job_1 is re-run on resume');
    assert.equal(second.jobs.find((j) => j.id === 'job_0')!.outcome, 'completed');
    assert.equal(second.jobs.find((j) => j.id === 'job_1')!.outcome, 'completed');
});

test('persists an accurate machine-readable manifest + summary', async () => {
    const manifestPath = tmpManifestPath();
    const manifest = await runBatch(
        makeInputs(3),
        {
            concurrency: 2,
            maxRetries: 0,
            executeJob: async (job) =>
                job.index === 2
                    ? ({ outcome: 'failed' as const, error: 'nope' } as BatchJobResult)
                    : ({ outcome: 'completed' as const, outputPath: `/out/${job.index}.mp4` } as BatchJobResult),
        },
        manifestPath,
    );

    // Re-read from disk.
    const fromDisk = readManifestSafe(manifestPath);
    assert.ok(fromDisk, 'manifest written to disk');
    assert.equal(fromDisk!.jobs.length, 3);
    const summary = summarize(fromDisk!);
    assert.equal(summary.completed, 2);
    assert.equal(summary.failed, 1);
    assert.equal(summary.pending, 0);
    assert.equal(summary.total, 3);

    const failedEntry = fromDisk!.jobs.find((j) => j.id === 'job_2')!;
    assert.equal(failedEntry.attempts, 1);
    assert.equal(failedEntry.error, 'nope');
    assert.ok(failedEntry.startedAt && failedEntry.finishedAt);
});

test('--only restricts the batch to named ids', async () => {
    const manifestPath = tmpManifestPath();
    const seen = new Set<string>();
    await runBatch(
        makeInputs(3),
        {
            concurrency: 1,
            maxRetries: 0,
            onlyIds: ['job_1'],
            executeJob: async (job) => {
                seen.add(job.id);
                return { outcome: 'completed' as const };
            },
        },
        manifestPath,
    );
    assert.deepEqual([...seen], ['job_1']);
});

test('--fail-fast stops the batch after the first failure', async () => {
    const manifestPath = tmpManifestPath();
    let executions = 0;
    await runBatch(
        makeInputs(4),
        {
            concurrency: 2,
            maxRetries: 0,
            failFast: true,
            executeJob: async (job) => {
                executions += 1;
                await new Promise((r) => setTimeout(r, 5));
                return job.id === 'job_1'
                    ? ({ outcome: 'failed' as const, error: 'stop' } as BatchJobResult)
                    : ({ outcome: 'completed' as const } as BatchJobResult);
            },
        },
        manifestPath,
    );
    assert.ok(executions < 4, 'not every job ran because fail-fast aborted the queue');
});