Add automatic Threads link replies and sunrise preview card
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
import { test } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { mkdtemp, writeFile, readFile, rm } from 'node:fs/promises';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { join } from 'node:path';
|
||||
import { replyToPublished } from '../server/threads/reply.ts';
|
||||
|
||||
test('confirmed reply is recorded separately and skipped on restart', async () => {
|
||||
const dir = await mkdtemp(join(tmpdir(), 'threads-reply-'));
|
||||
const path = join(dir, 'record.json');
|
||||
let calls = 0;
|
||||
try {
|
||||
const original = JSON.stringify({ status: 'published', postId: 'parent', imagesDeletedAt: 'now',
|
||||
replyText: 'Link', replyUrl: 'https://example.com' });
|
||||
await writeFile(path, original);
|
||||
const publish = async (_token: string, post: string, text: string, url: string, claim: (id: string) => Promise<void>) => {
|
||||
calls++;
|
||||
assert.equal(post, 'parent');
|
||||
assert.equal(text, 'Link');
|
||||
assert.equal(url, 'https://example.com');
|
||||
await claim('container');
|
||||
return 'reply-id';
|
||||
};
|
||||
await replyToPublished(path, async () => 'token', publish);
|
||||
await replyToPublished(path, async () => 'token', publish);
|
||||
assert.equal(calls, 1);
|
||||
assert.equal(await readFile(path, 'utf8'), original);
|
||||
assert.equal(JSON.parse(await readFile(`${path}.reply.json`, 'utf8')).replyId, 'reply-id');
|
||||
} finally { await rm(dir, { recursive: true, force: true }); }
|
||||
});
|
||||
|
||||
test('reply waits for image cleanup, and a lost publish response is never sent twice', async () => {
|
||||
const dir = await mkdtemp(join(tmpdir(), 'threads-reply-'));
|
||||
const path = join(dir, 'record.json');
|
||||
const record = { status: 'published', postId: 'parent', replyText: 'Link', replyUrl: 'https://example.com' };
|
||||
let calls = 0;
|
||||
const publish = async (_token: string, _post: string, _text: string, _url: string, claim: (id: string) => Promise<void>) => {
|
||||
calls++;
|
||||
await claim('container');
|
||||
throw new Error('Connection lost');
|
||||
};
|
||||
try {
|
||||
await writeFile(path, JSON.stringify(record));
|
||||
await replyToPublished(path, async () => 'token', publish);
|
||||
assert.equal(calls, 0);
|
||||
await writeFile(path, JSON.stringify({ ...record, imagesDeletedAt: 'now' }));
|
||||
await assert.rejects(replyToPublished(path, async () => 'token', publish), /Connection lost/);
|
||||
await replyToPublished(path, async () => 'token', publish);
|
||||
assert.equal(calls, 1);
|
||||
assert.equal(JSON.parse(await readFile(`${path}.reply.json`, 'utf8')).status, 'publishing');
|
||||
} finally { await rm(dir, { recursive: true, force: true }); }
|
||||
});
|
||||
Reference in New Issue
Block a user