Merge remote-tracking branch 'gitea/main'
# Conflicts: # prompts/owner-common-paired-room.md # src/channels/discord.ts # src/codex-warmup.ts # src/db/bootstrap.test.ts # src/db/migrations/index.ts # src/paired-execution-context-reviewer.ts # src/usage-primer.test.ts # src/usage-primer.ts
This commit is contained in:
@@ -129,7 +129,11 @@ vi.mock('discord.js', () => {
|
||||
};
|
||||
});
|
||||
|
||||
import { DiscordChannel, DiscordChannelOpts } from './discord.js';
|
||||
import {
|
||||
DiscordChannel,
|
||||
DiscordChannelOpts,
|
||||
chunkForDiscord,
|
||||
} from './discord.js';
|
||||
import { logger } from '../logger.js';
|
||||
|
||||
// --- Test helpers ---
|
||||
@@ -1160,3 +1164,127 @@ describe('channel properties', () => {
|
||||
expect(channel.name).toBe('discord');
|
||||
});
|
||||
});
|
||||
|
||||
// --- chunkForDiscord ---
|
||||
|
||||
describe('chunkForDiscord', () => {
|
||||
it('returns the input unchanged when within the limit', () => {
|
||||
const text = 'short message';
|
||||
expect(chunkForDiscord(text, 2000)).toEqual([text]);
|
||||
});
|
||||
|
||||
it('returns empty array for empty input', () => {
|
||||
expect(chunkForDiscord('', 2000)).toEqual([]);
|
||||
});
|
||||
|
||||
it('splits on newline boundaries when possible', () => {
|
||||
const text = 'aaa\nbbb\nccc\nddd';
|
||||
const chunks = chunkForDiscord(text, 8);
|
||||
// Every chunk must be ≤ 8 chars and joining them must rebuild the text
|
||||
for (const c of chunks) expect(c.length).toBeLessThanOrEqual(8);
|
||||
expect(chunks.join('')).toBe(text);
|
||||
});
|
||||
|
||||
it('reopens a fenced code block across chunks (with language)', () => {
|
||||
const code = Array.from({ length: 200 }, (_, i) => `line ${i}`).join('\n');
|
||||
const text = 'intro paragraph.\n\n```ts\n' + code + '\n```\nafter';
|
||||
const chunks = chunkForDiscord(text, 500);
|
||||
expect(chunks.length).toBeGreaterThan(1);
|
||||
for (const c of chunks) expect(c.length).toBeLessThanOrEqual(500);
|
||||
// Every chunk must have a balanced number of ``` fences (so it renders
|
||||
// as valid markdown on its own).
|
||||
for (const c of chunks) {
|
||||
const fenceMatches = c.match(/^```/gm) ?? [];
|
||||
expect(fenceMatches.length % 2).toBe(0);
|
||||
}
|
||||
// The middle chunks must reopen the same language.
|
||||
for (let i = 1; i < chunks.length; i++) {
|
||||
// It should either start with the reopened fence, or never have been
|
||||
// inside the fence at this point.
|
||||
if (chunks[i].startsWith('```')) {
|
||||
expect(chunks[i].startsWith('```ts\n')).toBe(true);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
it('reopens a language-less fenced code block', () => {
|
||||
const code = Array.from({ length: 200 }, (_, i) => `payload-${i}`).join(
|
||||
'\n',
|
||||
);
|
||||
const text = '```\n' + code + '\n```';
|
||||
const chunks = chunkForDiscord(text, 400);
|
||||
expect(chunks.length).toBeGreaterThan(1);
|
||||
for (const c of chunks) {
|
||||
const fenceMatches = c.match(/^```/gm) ?? [];
|
||||
expect(fenceMatches.length % 2).toBe(0);
|
||||
}
|
||||
// Joining back should rebuild a string whose un-fenced content equals
|
||||
// the original code (allowing for extra fence pairs in the middle).
|
||||
const reconstructed = chunks
|
||||
.join('\n')
|
||||
.replace(/```\n```\n/g, '') // drop reopen-close pairs at chunk seams
|
||||
.replace(/```\n```$/g, '');
|
||||
expect(reconstructed.includes('payload-0')).toBe(true);
|
||||
expect(reconstructed.includes('payload-199')).toBe(true);
|
||||
});
|
||||
|
||||
it('handles multiple fenced blocks in the same document', () => {
|
||||
const blockA = Array.from({ length: 50 }, () => 'AAAA').join('\n');
|
||||
const blockB = Array.from({ length: 50 }, () => 'BBBB').join('\n');
|
||||
const text =
|
||||
'intro\n```js\n' + blockA + '\n```\nbetween\n```py\n' + blockB + '\n```';
|
||||
const chunks = chunkForDiscord(text, 200);
|
||||
for (const c of chunks) expect(c.length).toBeLessThanOrEqual(200);
|
||||
for (const c of chunks) {
|
||||
const fenceMatches = c.match(/^```/gm) ?? [];
|
||||
expect(fenceMatches.length % 2).toBe(0);
|
||||
}
|
||||
});
|
||||
|
||||
it('does not split a surrogate pair', () => {
|
||||
// Emoji 🦄 is a surrogate pair (length 2 in JS).
|
||||
const emoji = '🦄';
|
||||
const text = emoji.repeat(50);
|
||||
const chunks = chunkForDiscord(text, 9);
|
||||
for (const c of chunks) {
|
||||
// No chunk should start or end mid surrogate pair.
|
||||
expect(c.charCodeAt(0) >= 0xdc00 && c.charCodeAt(0) <= 0xdfff).toBe(
|
||||
false,
|
||||
);
|
||||
const last = c.charCodeAt(c.length - 1);
|
||||
expect(last >= 0xd800 && last <= 0xdbff).toBe(false);
|
||||
}
|
||||
expect(chunks.join('')).toBe(text);
|
||||
});
|
||||
|
||||
it('falls back gracefully when a single line exceeds maxLength', () => {
|
||||
const longLine = 'x'.repeat(5000);
|
||||
const chunks = chunkForDiscord(longLine, 2000);
|
||||
for (const c of chunks) expect(c.length).toBeLessThanOrEqual(2000);
|
||||
expect(chunks.join('')).toBe(longLine);
|
||||
});
|
||||
|
||||
it('keeps a closing fence with its block when there is room', () => {
|
||||
const text = '```ts\nshort code\n```';
|
||||
const chunks = chunkForDiscord(text, 2000);
|
||||
expect(chunks).toEqual([text]);
|
||||
});
|
||||
|
||||
it('preserves total content across chunk seams (modulo reopen overhead)', () => {
|
||||
const code = Array.from(
|
||||
{ length: 80 },
|
||||
(_, i) => `console.log(${i});`,
|
||||
).join('\n');
|
||||
const text = '```js\n' + code + '\n```';
|
||||
const chunks = chunkForDiscord(text, 300);
|
||||
// After removing the reopen-close fence pairs that the chunker inserts
|
||||
// between chunks, the result should match the original text.
|
||||
const stripped = chunks.join('\n').replace(/```\n```js\n/g, '');
|
||||
// The first chunk still has the original opener and the last still has
|
||||
// the original closer.
|
||||
expect(stripped.startsWith('```js\n')).toBe(true);
|
||||
expect(stripped.endsWith('```')).toBe(true);
|
||||
expect(stripped.includes('console.log(0);')).toBe(true);
|
||||
expect(stripped.includes('console.log(79);')).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user