Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion backend/__tests__/unit/routes/integrations.linkedUserId.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ jest.mock('../../../models/DiscordIntegration', () => function DiscordIntegratio
});
jest.mock('../../../services/discordService', () => jest.fn());
jest.mock('../../../models/Integration', () => {
function Integration(data) { Object.assign(this, data); }
function Integration(data) {
Object.assign(this, data);
this._id = this._id || 'integration-new';
this.save = jest.fn().mockResolvedValue(this);
}
Integration.findById = jest.fn();
Integration.findByIdAndUpdate = jest.fn();
Integration.aggregate = jest.fn().mockResolvedValue([]);
Expand Down Expand Up @@ -89,3 +93,34 @@ describe('PATCH /api/integrations/:id — linkedUserId guard', () => {
expect(update.config.linkedUserId).toBeUndefined();
});
});

describe('POST /api/integrations — telegram first-run defaults', () => {
beforeEach(() => {
jest.clearAllMocks();
const User = require('../../../models/User');
User.findById.mockResolvedValue({ _id: 'user-1', role: 'member' });
});

// A fresh connector must never default into the silence trap: attention
// mode with no lead configured relays nothing, so mirror is the first-run
// default and the Connected message teaches /mode attention.
it('defaults a new telegram connector to liveRelay + mirror', async () => {
const Integration = require('../../../models/Integration');
const res = await request(app)
.post('/api/integrations')
.send({ podId: 'pod-1', type: 'telegram', config: {} });
expect(res.status).toBe(201);
const created = res.body.integration;
expect(created.config.relayAllAgentMessages).toBe(true);
expect(created.config.liveRelay).toBe(true);
expect(created.config.connectCode).toBeTruthy();
});

it('respects an explicit attention-mode choice at create', async () => {
const res = await request(app)
.post('/api/integrations')
.send({ podId: 'pod-1', type: 'telegram', config: { relayAllAgentMessages: false } });
expect(res.status).toBe(201);
expect(res.body.integration.config.relayAllAgentMessages).toBe(false);
});
});
8 changes: 8 additions & 0 deletions backend/routes/integrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,14 @@ router.post('/', auth, async (req: AuthReq, res: Res) => {
if (!manifest) return res.status(400).json({ message: 'Unsupported integration type' });
const nextConfig = { ...config };
if (type === 'telegram' && !nextConfig.connectCode) nextConfig.connectCode = crypto.randomBytes(3).toString('hex');
// First-run default: mirror. A fresh connector has no leadAgentUsername
// and its agents use no escalation markers, so attention mode relays
// NOTHING — a new user's first experience of the bridge would be silence.
// Mirror shows everything; the Connected message teaches /mode attention.
if (type === 'telegram' && nextConfig.relayAllAgentMessages === undefined) {
nextConfig.relayAllAgentMessages = true;
if (nextConfig.liveRelay === undefined) nextConfig.liveRelay = true;
}
const missingRequired = getMissingRequiredFields(type, nextConfig);
if (type === 'discord' && missingRequired.length) return res.status(400).json({ message: `Missing required fields: ${missingRequired.join(', ')}`, missing: missingRequired });
if (missingRequired.length && (req.body as { status?: string })?.status === 'connected') return res.status(400).json({ message: `Missing required fields: ${missingRequired.join(', ')}`, missing: missingRequired });
Expand Down
4 changes: 3 additions & 1 deletion backend/routes/webhooks/telegram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,9 @@ const handleEnableCommand = async (chat: any, code: any) => {
await telegramService.sendMessage(
botToken,
chatId,
`✅ Connected this chat to <b>${podName}</b> in Commonly.`,
`✅ Connected this chat to <b>${podName}</b> in Commonly.\n`
+ 'Agent messages from the pod will appear here. Too chatty? Send '
+ '/mode attention to only get what needs you. /help lists the rest.',
);
};

Expand Down
Loading