[READ-ONLY] Mirror of https://github.com/andrioid/n8n-nodes-atproto. atproto node for n8n
2.3 kB
73 lines
1/**
2 * Behaviour tests for user-facing error messages.
3 *
4 * The message must name *which* PDS failed — reads route to the target
5 * repo's PDS, so a rate limit may come from a server other than the user's
6 * own. Rate-limit timing is read from the standard atproto headers.
7 */
8
9import { describe, it, expect } from 'vitest';
10import { friendlyError } from '../nodes/Atproto/Atproto.node';
11function xrpcLikeError(
12 message: string,
13 extra: Record<string, unknown> = {},
14): Error {
15 return Object.assign(new Error(message), extra);
16}
17
18describe('friendlyError', () => {
19 it('names the PDS host and computes the reset on a rate limit', () => {
20 const resetEpoch = Math.floor(Date.now() / 1000) + 42;
21 const msg = friendlyError(
22 xrpcLikeError('Rate Limit Exceeded', {
23 status: 429,
24 headers: { 'ratelimit-reset': String(resetEpoch) },
25 pdsHost: 'auriporia.us-west.host.bsky.network',
26 }),
27 );
28
29 expect(msg).toContain(
30 'Rate limited by auriporia.us-west.host.bsky.network',
31 );
32 expect(msg).toMatch(/retry after (41|42)s/);
33 expect(msg).toContain('resets at');
34 });
35
36 it('falls back to retry-after when no reset header is present', () => {
37 const msg = friendlyError(
38 xrpcLikeError('Rate Limit Exceeded', {
39 status: 429,
40 headers: { 'retry-after': '30' },
41 pdsHost: 'pds.example',
42 }),
43 );
44
45 expect(msg).toContain('Rate limited by pds.example — retry after 30s');
46 });
47
48 it('reports a rate limit without timing when headers are missing', () => {
49 const msg = friendlyError(
50 xrpcLikeError('Rate Limit Exceeded', { status: 429 }),
51 );
52 expect(msg).toBe('Rate limited — try again later');
53 });
54
55 it('names the host that could not be reached', () => {
56 const msg = friendlyError(
57 xrpcLikeError('fetch failed', { pdsHost: 'pds.example' }),
58 );
59 expect(msg).toBe('Could not reach pds.example');
60 });
61
62 it('appends the host to otherwise-unmapped errors', () => {
63 const msg = friendlyError(
64 xrpcLikeError('Could not find repo: did:plc:abc', {
65 status: 400,
66 pdsHost: 'shard.host.example',
67 }),
68 );
69 expect(msg).toBe(
70 'Could not find repo: did:plc:abc (via shard.host.example)',
71 );
72 });
73});