feat: add job lifecycle controls abuse policies and retention operations

This commit is contained in:
Codex
2026-02-18 14:19:21 +00:00
parent e056d38ec7
commit 141d7b42a8
6 changed files with 1055 additions and 26 deletions

View File

@@ -31,7 +31,7 @@ test("returns not_article and does not charge caller", () => {
assert.equal(engine.getWalletBalance("u1"), 5);
});
test("charges credits and creates completed job for valid article", () => {
test("charges credits and creates charged job for valid article", () => {
const engine = createEngine();
engine.topUpCredits("u1", 5, "topup-2");
@@ -50,7 +50,7 @@ test("charges credits and creates completed job for valid article", () => {
});
assert.equal(result.ok, true);
assert.equal(result.job.status, "completed");
assert.equal(result.job.status, "charged");
assert.equal(result.job.creditsCharged, 1);
assert.equal(engine.getWalletBalance("u1"), 4);
@@ -150,11 +150,60 @@ test("lists jobs for user newest first and provides summary", () => {
const summary = engine.getUserSummary("u1");
assert.equal(summary.totalJobs, 2);
assert.equal(summary.completedJobs, 2);
assert.equal(summary.completedJobs, 0);
assert.equal(summary.totalCreditsSpent, 2);
assert.equal(summary.balance, 8);
});
test("job can transition through start and completion states", () => {
const engine = createEngine();
engine.topUpCredits("u1", 5, "topup-transition");
const created = engine.processMention({
mentionPostId: "m-transition",
callerUserId: "u1",
parentPost: {
id: "p-transition",
article: { id: "a-transition", title: "T", body: "hello world" },
},
});
const started = engine.startJob(created.job.id);
assert.equal(started.status, "synthesizing");
const completed = engine.completeJob(created.job.id, {
storageKey: "audio/final.mp3",
sizeBytes: 42,
});
assert.equal(completed.status, "completed");
assert.equal(engine.getAsset(created.job.assetId).storageKey, "audio/final.mp3");
assert.equal(engine.getAsset(created.job.assetId).sizeBytes, 42);
});
test("failed generation can refund caller credits once", () => {
const engine = createEngine();
engine.topUpCredits("u1", 5, "topup-fail-refund");
const created = engine.processMention({
mentionPostId: "m-fail-refund",
callerUserId: "u1",
parentPost: {
id: "p-fail-refund",
article: { id: "a-fail-refund", title: "T", body: "hello world" },
},
});
assert.equal(engine.getWalletBalance("u1"), 4);
engine.startJob(created.job.id);
const failed = engine.failJob(created.job.id, { error: "tts_down", refund: true });
assert.equal(failed.status, "failed_refunded");
assert.equal(engine.getWalletBalance("u1"), 5);
const second = engine.failJob(created.job.id, { error: "tts_down", refund: true });
assert.equal(second.status, "failed_refunded");
assert.equal(engine.getWalletBalance("u1"), 5);
});
test("round-trips state snapshot across engine restart", () => {
const engine1 = createEngine();
engine1.topUpCredits("u1", 5, "topup-snapshot");
@@ -202,3 +251,51 @@ test("updateAsset patches stored asset metadata", () => {
assert.equal(updated.storageKey, "audio/real-file.mp3");
assert.equal(updated.sizeBytes, 12345);
});
test("owner can takedown audio and hide it from access checks", () => {
const engine = createEngine();
engine.topUpCredits("owner", 5, "topup-takedown");
const created = engine.processMention({
mentionPostId: "m-takedown",
callerUserId: "owner",
parentPost: {
id: "p-takedown",
article: { id: "a-takedown", title: "T", body: "hello" },
},
});
engine.takedownAudio(created.job.assetId, "owner");
assert.equal(engine.getAsset(created.job.assetId), null);
assert.equal(engine.getAsset(created.job.assetId, { includeDeleted: true }).deletedAt !== null, true);
assert.equal(engine.checkAudioAccess(created.job.assetId, "owner").reason, "not_found");
});
test("retention prunes old article content and deletes stale assets", () => {
const engine = createEngine();
engine.topUpCredits("owner", 5, "topup-retention");
const created = engine.processMention({
mentionPostId: "m-retention",
callerUserId: "owner",
parentPost: {
id: "p-retention",
article: { id: "a-retention", title: "T", body: "hello retention" },
},
});
const job = engine.getJob(created.job.id);
const asset = engine.getAsset(created.job.assetId, { includeDeleted: true });
job.createdAt = "2020-01-01T00:00:00.000Z";
asset.createdAt = "2020-01-01T00:00:00.000Z";
const summary = engine.applyRetention({
rawArticleHours: 1,
audioDays: 1,
now: new Date("2020-01-03T00:00:00.000Z"),
});
assert.equal(summary.prunedArticleBodies, 1);
assert.equal(summary.deletedAssets, 1);
assert.equal(engine.getJob(created.job.id).article.content, "");
assert.equal(typeof engine.getJob(created.job.id).article.contentHash, "string");
assert.equal(engine.getAsset(created.job.assetId), null);
});