Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

12.2 KB
Raw Blame
// @vitest-environment node
import { describe, expect, it } from 'vitest';

import {
  consumeSendCredits,
  drainPendingPushes,
  enqueuePendingPush,
  peekWindow,
  pendingPushSendCount,
  recordInboundToken,
  WECHAT_WINDOW_MAX_SENDS,
  wechatLegacyTokenKey,
  type WechatPendingPush,
  wechatPendingPushKey,
  wechatWindowKey,
  type WechatWindowRedis,
} from './contextWindow';

/** Minimal in-memory Redis covering the commands the window store uses. */
class FakeRedis implements WechatWindowRedis {
  hashes = new Map<string, Record<string, string>>();
  lists = new Map<string, string[]>();
  strings = new Map<string, string>();
  ttls = new Map<string, number>();

  async del(...keys: string[]) {
    for (const key of keys) {
      this.hashes.delete(key);
      this.lists.delete(key);
      this.strings.delete(key);
      this.ttls.delete(key);
    }
    return keys.length;
  }

  async expire(key: string, seconds: number) {
    this.ttls.set(key, seconds * 1000);
    return 1;
  }

  async get(key: string) {
    return this.strings.get(key) ?? null;
  }

  async hgetall(key: string) {
    return this.hashes.get(key) ?? {};
  }

  async hincrby(key: string, field: string, increment: number) {
    const hash = this.hashes.get(key) ?? {};
    const next = (Number(hash[field]) || 0) + increment;
    hash[field] = String(next);
    this.hashes.set(key, hash);
    return next;
  }

  async hset(key: string, data: Record<string, string | number>) {
    const hash = this.hashes.get(key) ?? {};
    for (const [field, value] of Object.entries(data)) hash[field] = String(value);
    this.hashes.set(key, hash);
    return Object.keys(data).length;
  }

  async llen(key: string) {
    return this.lists.get(key)?.length ?? 0;
  }

  async lpop(key: string) {
    return this.lists.get(key)?.shift() ?? null;
  }

  async lpush(key: string, value: string) {
    const list = this.lists.get(key) ?? [];
    list.unshift(value);
    this.lists.set(key, list);
    return list.length;
  }

  async ltrim(key: string, start: number, stop: number) {
    const list = this.lists.get(key) ?? [];
    const normalizedStop = stop < 0 ? list.length + stop : stop;
    this.lists.set(key, list.slice(start, normalizedStop + 1));
    return 'OK';
  }

  async pttl(key: string) {
    return this.ttls.get(key) ?? -1;
  }

  async rpush(key: string, value: string) {
    const list = this.lists.get(key) ?? [];
    list.push(value);
    this.lists.set(key, list);
    return list.length;
  }

  async set(key: string, value: string, ...args: (string | number)[]) {
    const nx = args.includes('NX');
    if (nx && this.strings.has(key)) return null;
    this.strings.set(key, value);
    const exIndex = args.indexOf('EX');
    if (exIndex !== -1) this.ttls.set(key, Number(args[exIndex + 1]) * 1000);
    return 'OK';
  }

  async ttl(key: string) {
    const ms = this.ttls.get(key);
    return ms === undefined ? -1 : Math.ceil(ms / 1000);
  }
}

const APP = '[email protected]';
const USER = '[email protected]';

describe('recordInboundToken', () => {
  it('resets the window with a full quota and mirrors the legacy key', async () => {
    const redis = new FakeRedis();
    await redis.hset(wechatWindowKey(APP, USER), { remaining: 1, token: 'old' });

    await recordInboundToken(redis, APP, USER, 'fresh-token');

    const sendWindow = await peekWindow(redis, APP, USER);
    expect(sendWindow).toMatchObject({ remaining: WECHAT_WINDOW_MAX_SENDS, token: 'fresh-token' });
    expect(await redis.get(wechatLegacyTokenKey(APP, USER))).toBe('fresh-token');
  });
});

describe('peekWindow', () => {
  it('returns null when neither window nor legacy token exists', async () => {
    expect(await peekWindow(new FakeRedis(), APP, USER)).toBeNull();
  });

  it('seeds the window from the legacy token key with a full quota', async () => {
    const redis = new FakeRedis();
    await redis.set(wechatLegacyTokenKey(APP, USER), 'legacy-token', 'EX', 3600);

    const sendWindow = await peekWindow(redis, APP, USER);

    expect(sendWindow).toMatchObject({ remaining: WECHAT_WINDOW_MAX_SENDS, token: 'legacy-token' });
    // Seeded window must not outlive the legacy token.
    expect(await redis.pttl(wechatWindowKey(APP, USER))).toBe(3600 * 1000);
  });
});

describe('consumeSendCredits', () => {
  it('decrements the quota and returns the token', async () => {
    const redis = new FakeRedis();
    await recordInboundToken(redis, APP, USER, 'token-1');

    const result = await consumeSendCredits(redis, APP, USER, 3);

    expect(result).toEqual({
      remaining: WECHAT_WINDOW_MAX_SENDS - 3,
      status: 'ok',
      token: 'token-1',
    });
  });

  it('reports missing when there is no window at all', async () => {
    expect(await consumeSendCredits(new FakeRedis(), APP, USER, 1)).toEqual({ status: 'missing' });
  });

  it('refuses to overdraw in strict mode and leaves the counter untouched', async () => {
    const redis = new FakeRedis();
    await recordInboundToken(redis, APP, USER, 'token-1');
    await consumeSendCredits(redis, APP, USER, WECHAT_WINDOW_MAX_SENDS);

    const result = await consumeSendCredits(redis, APP, USER, 1);

    expect(result).toEqual({ remaining: 0, status: 'exhausted', token: 'token-1' });
    expect((await peekWindow(redis, APP, USER))?.remaining).toBe(0);
  });

  it('allows the counter to go negative with allowOverdraft', async () => {
    const redis = new FakeRedis();
    await recordInboundToken(redis, APP, USER, 'token-1');
    await consumeSendCredits(redis, APP, USER, WECHAT_WINDOW_MAX_SENDS);

    const result = await consumeSendCredits(redis, APP, USER, 2, { allowOverdraft: true });

    expect(result).toEqual({ remaining: -2, status: 'ok', token: 'token-1' });
  });
});

describe('pendingPushSendCount', () => {
  it('counts text and attachments as separate sendmessage calls', () => {
    expect(pendingPushSendCount({ content: 'hi' })).toBe(1);
    expect(pendingPushSendCount({ content: 'a'.repeat(4500) })).toBe(3);
    expect(pendingPushSendCount({ attachments: [{ type: 'image' }, { type: 'file' }] })).toBe(2);
    expect(pendingPushSendCount({ attachments: [{ type: 'image' }], content: 'hi' })).toBe(2);
    // A degenerate empty payload still costs one call.
    expect(pendingPushSendCount({ content: '  ' })).toBe(1);
  });
});

describe('pending push queue', () => {
  const payload = (content: string): WechatPendingPush => ({ content, enqueuedAt: 1 });

  it('enqueues FIFO and drains in order', async () => {
    const redis = new FakeRedis();
    await enqueuePendingPush(redis, APP, USER, payload('first'));
    await enqueuePendingPush(redis, APP, USER, payload('second'));

    const drained: string[] = [];
    const { sent } = await drainPendingPushes(redis, APP, USER, async (p) => {
      drained.push(p.content!);
      return 'sent';
    });

    expect(sent).toBe(2);
    expect(drained).toEqual(['first', 'second']);
    expect(redis.lists.get(wechatPendingPushKey(APP, USER))).toEqual([]);
  });

  it('pushes a stopped payload back to the queue head', async () => {
    const redis = new FakeRedis();
    await enqueuePendingPush(redis, APP, USER, payload('first'));
    await enqueuePendingPush(redis, APP, USER, payload('second'));

    let calls = 0;
    const { sent } = await drainPendingPushes(redis, APP, USER, async () => {
      calls++;
      return calls === 1 ? 'sent' : 'stop';
    });

    expect(sent).toBe(1);
    const remaining = redis.lists.get(wechatPendingPushKey(APP, USER))!;
    expect(remaining).toHaveLength(1);
    expect(JSON.parse(remaining[0]).content).toBe('second');
  });

  it('skips draining while another flush holds the lock', async () => {
    const redis = new FakeRedis();
    await enqueuePendingPush(redis, APP, USER, payload('first'));
    await redis.set(`wechat:pending-flush:${APP}:${USER}`, '1', 'EX', 30);

    const { sent } = await drainPendingPushes(redis, APP, USER, async () => 'sent');

    expect(sent).toBe(0);
    expect(redis.lists.get(wechatPendingPushKey(APP, USER))).toHaveLength(1);
  });

  it('reports the lock it could not take, so callers do not read an empty queue as drained', async () => {
    // Regression: the in-flight drain has already LPOP'd its current item, so
    // the list reads empty from outside while an older message is still being
    // delivered. `drained: false` is what stops a caller trusting that.
    const redis = new FakeRedis();
    await enqueuePendingPush(redis, APP, USER, payload('first'));
    await redis.set(`wechat:pending-flush:${APP}:${USER}`, '1', 'EX', 30);

    const result = await drainPendingPushes(redis, APP, USER, async () => 'sent');

    expect(result).toEqual({ drained: false, remaining: 0, sent: 0 });
  });

  it('does not delete a lock a new owner took after the lease expired', async () => {
    // Regression: one delivery can outlive the 30s TTL (probe + download +
    // upload is not bounded by it). An unconditional DEL on the way out would
    // drop the lock the NEXT drainer is holding and admit a third one.
    const redis = new FakeRedis();
    await enqueuePendingPush(redis, APP, USER, payload('first'));
    const lockKey = `wechat:pending-flush:${APP}:${USER}`;

    await drainPendingPushes(redis, APP, USER, async () => {
      // Simulate the lease expiring mid-delivery and someone else claiming it.
      await redis.set(lockKey, 'someone-elses-token', 'EX', 30);
      return 'sent';
    });

    expect(await redis.get(lockKey)).toBe('someone-elses-token');
  });

  it('stops draining when the lease is lost mid-delivery', async () => {
    // Regression: a send that outlasts the lease lets a second drainer in. The
    // first must not keep popping items the second is also popping, and must
    // not renew the lease that now belongs to the new owner.
    const redis = new FakeRedis();
    await enqueuePendingPush(redis, APP, USER, payload('first'));
    await enqueuePendingPush(redis, APP, USER, payload('second'));
    const lockKey = `wechat:pending-flush:${APP}:${USER}`;

    const delivered: string[] = [];
    const result = await drainPendingPushes(redis, APP, USER, async (p) => {
      delivered.push(p.content!);
      // The lease lapsed while this delivery ran and another drainer took it.
      await redis.set(lockKey, 'new-owner-token', 'EX', 30);
      return 'sent';
    });

    expect(delivered).toEqual(['first']);
    expect(result.sent).toBe(1);
    // The newer item is left for the drainer that now owns the lease.
    expect(redis.lists.get(wechatPendingPushKey(APP, USER))).toHaveLength(1);
    expect(await redis.get(lockKey)).toBe('new-owner-token');
  });

  it('renews its own lease across deliveries and releases it at the end', async () => {
    const redis = new FakeRedis();
    await enqueuePendingPush(redis, APP, USER, payload('first'));
    const lockKey = `wechat:pending-flush:${APP}:${USER}`;

    await drainPendingPushes(redis, APP, USER, async () => 'sent');

    // Its own lease is cleaned up, so the next drain is not blocked for 30s.
    expect(await redis.get(lockKey)).toBeNull();
  });

  it('reports what is still queued when the drain stops early', async () => {
    const redis = new FakeRedis();
    await enqueuePendingPush(redis, APP, USER, payload('first'));
    await enqueuePendingPush(redis, APP, USER, payload('second'));

    const result = await drainPendingPushes(redis, APP, USER, async () => 'stop');

    expect(result).toEqual({ drained: true, remaining: 2, sent: 0 });
  });

  it('drops malformed payloads instead of blocking the queue', async () => {
    const redis = new FakeRedis();
    await redis.rpush(wechatPendingPushKey(APP, USER), 'not-json');
    await enqueuePendingPush(redis, APP, USER, payload('valid'));

    const drained: string[] = [];
    const { sent } = await drainPendingPushes(redis, APP, USER, async (p) => {
      drained.push(p.content!);
      return 'sent';
    });

    expect(sent).toBe(1);
    expect(drained).toEqual(['valid']);
  });

  it('caps the queue by dropping the oldest entries', async () => {
    const redis = new FakeRedis();
    for (let i = 0; i < 25; i++) {
      await enqueuePendingPush(redis, APP, USER, payload(`msg-${i}`));
    }

    const list = redis.lists.get(wechatPendingPushKey(APP, USER))!;
    expect(list).toHaveLength(20);
    expect(JSON.parse(list[0]).content).toBe('msg-5');
    expect(JSON.parse(list.at(-1)!).content).toBe('msg-24');
  });
});