feat: add polar x tts and storage integration adapters with tests

This commit is contained in:
Codex
2026-02-18 13:35:03 +00:00
parent de2ddf258f
commit d68ccc70bf
10 changed files with 678 additions and 0 deletions

View File

@@ -0,0 +1,59 @@
"use strict";
const test = require("node:test");
const assert = require("node:assert/strict");
const { createAudioGenerationService } = require("../src/services/audio-generation");
test("enqueueJob generates audio and uploads it", async () => {
const uploaded = [];
const service = createAudioGenerationService({
tts: {
async synthesize() {
return Buffer.from("bytes");
},
},
storage: {
async uploadAudio(payload) {
uploaded.push(payload);
},
},
logger: { error() {} },
});
const result = await service.enqueueJob({
assetId: "a1",
text: "hello",
});
assert.equal(result.storageKey, "audio/a1.mp3");
assert.equal(uploaded.length, 1);
assert.equal(uploaded[0].key, "audio/a1.mp3");
});
test("enqueueJob invokes onFailed on error", async () => {
let failed = false;
const service = createAudioGenerationService({
tts: {
async synthesize() {
throw new Error("tts_fail");
},
},
storage: {
async uploadAudio() {},
},
logger: { error() {} },
});
await assert.rejects(
() => service.enqueueJob({
assetId: "a2",
text: "hello",
onFailed() {
failed = true;
},
}),
/tts_fail/,
);
assert.equal(failed, true);
});

View File

@@ -0,0 +1,71 @@
"use strict";
const test = require("node:test");
const assert = require("node:assert/strict");
const {
createPolarAdapter,
normalizeTopUpPayload,
hasStandardWebhookHeaders,
} = require("../src/integrations/polar");
test("detects standard polar webhook headers", () => {
const ok = hasStandardWebhookHeaders({
"webhook-id": "a",
"webhook-timestamp": "b",
"webhook-signature": "c",
});
assert.equal(ok, true);
});
test("normalizeTopUpPayload supports legacy simple webhook shape", () => {
const parsed = normalizeTopUpPayload({ userId: "u1", credits: "12", eventId: "evt1" });
assert.equal(parsed.userId, "u1");
assert.equal(parsed.credits, 12);
assert.equal(parsed.eventId, "evt1");
});
test("normalizeTopUpPayload supports metadata-based order event shape", () => {
const parsed = normalizeTopUpPayload({
type: "order.paid",
data: {
id: "ord_1",
metadata: {
xartaudio_user_id: "u2",
xartaudio_credits: "20",
},
},
});
assert.equal(parsed.userId, "u2");
assert.equal(parsed.credits, 20);
assert.equal(parsed.eventId, "ord_1");
});
test("createCheckoutSession calls polar sdk with configured products", async () => {
const calls = [];
const adapter = createPolarAdapter({
productIds: ["prod_1"],
sdk: {
checkouts: {
async create(payload) {
calls.push(payload);
return { id: "chk_1", url: "https://polar.sh/checkout/chk_1" };
},
},
},
});
const checkout = await adapter.createCheckoutSession({
userId: "u1",
successUrl: "https://app/success",
returnUrl: "https://app/return",
metadata: { xartaudio_user_id: "u1", xartaudio_credits: "50" },
});
assert.equal(checkout.id, "chk_1");
assert.equal(checkout.url, "https://polar.sh/checkout/chk_1");
assert.equal(calls.length, 1);
assert.deepEqual(calls[0].products, ["prod_1"]);
assert.equal(calls[0].externalCustomerId, "u1");
});

View File

@@ -0,0 +1,39 @@
"use strict";
const test = require("node:test");
const assert = require("node:assert/strict");
const { createStorageAdapter } = require("../src/integrations/storage-client");
test("uploadAudio sends put command", async () => {
const sent = [];
const adapter = createStorageAdapter({
bucket: "b1",
client: {
async send(command) {
sent.push(command.input);
},
},
signedUrlFactory: async () => "https://signed.example",
});
const res = await adapter.uploadAudio({
key: "audio/1.mp3",
body: Buffer.from("abc"),
});
assert.equal(res.bucket, "b1");
assert.equal(sent.length, 1);
assert.equal(sent[0].Bucket, "b1");
assert.equal(sent[0].Key, "audio/1.mp3");
});
test("getSignedDownloadUrl uses provided signer", async () => {
const adapter = createStorageAdapter({
bucket: "b1",
client: { async send() {} },
signedUrlFactory: async (_client, command, options) => `signed:${command.input.Key}:${options.expiresIn}`,
});
const url = await adapter.getSignedDownloadUrl("audio/2.mp3", 120);
assert.equal(url, "signed:audio/2.mp3:120");
});

View File

@@ -0,0 +1,32 @@
"use strict";
const test = require("node:test");
const assert = require("node:assert/strict");
const { createTTSAdapter } = require("../src/integrations/tts-client");
test("synthesize returns buffer from openai response", async () => {
const adapter = createTTSAdapter({
client: {
audio: {
speech: {
async create() {
return {
async arrayBuffer() {
return Uint8Array.from([1, 2, 3]).buffer;
},
};
},
},
},
},
});
const bytes = await adapter.synthesize("hello");
assert.equal(Buffer.isBuffer(bytes), true);
assert.equal(bytes.length, 3);
});
test("throws when tts adapter is not configured", async () => {
const adapter = createTTSAdapter({});
await assert.rejects(() => adapter.synthesize("hello"), /tts_not_configured/);
});

View File

@@ -0,0 +1,80 @@
"use strict";
const test = require("node:test");
const assert = require("node:assert/strict");
const { createXAdapter, findParentReplyId } = require("../src/integrations/x-client");
test("findParentReplyId returns replied_to reference id", () => {
const id = findParentReplyId({
referenced_tweets: [
{ type: "quoted", id: "q1" },
{ type: "replied_to", id: "p1" },
],
});
assert.equal(id, "p1");
});
test("listMentions fetches mention timeline", async () => {
const calls = [];
const adapter = createXAdapter({
botUserId: "bot-1",
client: {
v2: {
async userMentionTimeline(userId, opts) {
calls.push({ userId, opts });
return { data: [{ id: "m1" }] };
},
},
},
});
const mentions = await adapter.listMentions({ sinceId: "100" });
assert.equal(mentions.length, 1);
assert.equal(calls[0].userId, "bot-1");
assert.equal(calls[0].opts.since_id, "100");
});
test("fetchParentPostFromMention resolves parent tweet", async () => {
const adapter = createXAdapter({
botUserId: "bot-1",
client: {
v2: {
async singleTweet(id) {
if (id === "mention-1") {
return {
data: {
id,
referenced_tweets: [{ type: "replied_to", id: "parent-1" }],
},
};
}
return { data: { id, article: { title: "A", body: "B" } } };
},
},
},
});
const parent = await adapter.fetchParentPostFromMention("mention-1");
assert.equal(parent.id, "parent-1");
assert.equal(parent.article.title, "A");
});
test("replyToMention posts a reply", async () => {
const calls = [];
const adapter = createXAdapter({
botUserId: "bot-1",
client: {
v2: {
async reply(text, mentionId) {
calls.push({ text, mentionId });
return { data: { id: "reply-1" } };
},
},
},
});
const result = await adapter.replyToMention({ mentionTweetId: "m1", text: "done" });
assert.equal(result.data.id, "reply-1");
assert.equal(calls[0].mentionId, "m1");
});