Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
/**
* Pure-logic tests for the human-activity timeline (#2150). No DB — these cover
* the deterministic ingestion mappers, dedupe-key derivation, normalization, and
* the histogram/summary aggregates. The Postgres round-trip (recordEvents /
* listEvents / getDaySummary idempotency) is covered in humanActivity.db.test.js.
*/
import { describe, it, expect } from 'vitest';
import { localDayRangeUtc as canonicalLocalDayRangeUtc } from '../lib/timezone.js';
import {
shortSummary,
localDayKey,
localDayRangeUtc,
normalizeParticipant,
normalizeParticipants,
normalizeCandidate,
hourlyHistogram,
summarizeCounts,
messageActivityCandidates,
calendarActivityCandidates,
newlyLearnedAliases,
stripParticipantsForAccount,
} from './humanActivity.js';
describe('shortSummary', () => {
it('collapses whitespace to a single line', () => {
expect(shortSummary('hello world\n\tfoo')).toBe('hello world foo');
});
it('clamps to max with an ellipsis and never returns the full body', () => {
const body = 'x'.repeat(500);
const out = shortSummary(body, 160);
expect(out.length).toBeLessThanOrEqual(160);
expect(out.endsWith('…')).toBe(true);
});
it('returns empty for falsy input', () => {
expect(shortSummary('')).toBe('');
expect(shortSummary(null)).toBe('');
});
});
describe('localDayKey', () => {
it('derives the local day key in a given timezone', () => {
// 2026-07-04T02:00:00Z is still 2026-07-03 in America/Los_Angeles (-07:00).
expect(localDayKey('2026-07-04T02:00:00Z', 'America/Los_Angeles')).toBe('2026-07-03');
// …and the same instant is already 2026-07-04 in UTC.
expect(localDayKey('2026-07-04T02:00:00Z', 'UTC')).toBe('2026-07-04');
});
it('rejects a malformed date string', () => {
expect(localDayKey('not-a-date', 'UTC')).toBeNull();
});
});
it('keeps the localDayRangeUtc compatibility export wired to lib/timezone.js', () => {
expect(localDayRangeUtc).toBe(canonicalLocalDayRangeUtc);
});
// localDayRangeUtc itself (24h window, DST 23h/25h days, month/year rollover,
// malformed input) is now owned and tested by lib/timezone.js (#5572) — see
// timezone.test.js's own `localDayRangeUtc` describe block.
describe('participant normalization', () => {
it('lowercases email, trims, drops empties', () => {
expect(normalizeParticipant(' [email protected] ')).toEqual({ email: '[email protected]' });
expect(normalizeParticipant({ name: ' Jane ', email: '[email protected]', phone: ' +1 555 ' }))
.toEqual({ name: 'Jane', email: '[email protected]', phone: '+1 555' });
expect(normalizeParticipant({})).toBeNull();
expect(normalizeParticipant(null)).toBeNull();
});
it('normalizes and filters a list', () => {
expect(normalizeParticipants(['[email protected]', {}, null, { name: 'X' }])).toEqual([
{ email: '[email protected]' }, { name: 'X' },
]);
expect(normalizeParticipants('nope')).toEqual([]);
});
});
describe('normalizeCandidate', () => {
const base = {
source: 'gmail', kind: 'message.received', happenedAt: '2026-07-04T12:00:00Z',
dedupeKey: 'msg:acct:ext1', title: 'Hi', summary: 'body preview',
};
it('produces a DB row shape with a generated id when none supplied', () => {
const row = normalizeCandidate(base);
expect(row.id).toBeTruthy();
expect(row.source).toBe('gmail');
expect(row.kind).toBe('message.received');
expect(row.happenedAt).toBe('2026-07-04T12:00:00.000Z');
expect(row.participants).toEqual([]);
expect(row.metadata).toEqual({});
expect(row.durationS).toBeNull();
});
it('preserves a supplied id and coerces duration to an int', () => {
const row = normalizeCandidate({ ...base, id: 'fixed-id', durationS: 90.7 });
expect(row.id).toBe('fixed-id');
expect(row.durationS).toBe(91);
});
it('returns null when a required field is missing or the date is invalid', () => {
expect(normalizeCandidate({ ...base, source: '' })).toBeNull();
expect(normalizeCandidate({ ...base, kind: '' })).toBeNull();
expect(normalizeCandidate({ ...base, dedupeKey: '' })).toBeNull();
expect(normalizeCandidate({ ...base, happenedAt: 'garbage' })).toBeNull();
expect(normalizeCandidate(null)).toBeNull();
});
it('accepts snake_case aliases from callers', () => {
const row = normalizeCandidate({
source: 'calendar', kind: 'calendar.event', happened_at: '2026-07-04T12:00:00Z',
dedupe_key: 'cal:a:1', account_id: 'acct-9', duration_s: 3600,
});
expect(row.accountId).toBe('acct-9');
expect(row.durationS).toBe(3600);
});
});
describe('hourlyHistogram / summarizeCounts', () => {
const events = [
{ happenedAt: '2026-07-04T14:05:00Z', source: 'gmail', kind: 'message.received' },
{ happenedAt: '2026-07-04T14:40:00Z', source: 'gmail', kind: 'message.sent' },
{ happenedAt: '2026-07-04T22:00:00Z', source: 'calendar', kind: 'calendar.event' },
];
it('buckets events into 24 local-hour slots', () => {
const hist = hourlyHistogram(events, 'UTC');
expect(hist).toHaveLength(24);
expect(hist[14].count).toBe(2);
expect(hist[22].count).toBe(1);
expect(hist[0].count).toBe(0);
});
it('tallies by source and kind', () => {
const counts = summarizeCounts(events);
expect(counts.total).toBe(3);
expect(counts.bySource).toEqual({ gmail: 2, calendar: 1 });
expect(counts.byKind).toEqual({ 'message.received': 1, 'message.sent': 1, 'calendar.event': 1 });
});
});
describe('messageActivityCandidates', () => {
const account = { id: 'acct-1', type: 'gmail', email: '[email protected]' };
it('marks sent vs received by comparing the sender to the account owner', () => {
const [received, sent] = messageActivityCandidates(account, [
{ externalId: 'e1', date: '2026-07-04T10:00:00Z', from: '[email protected]', to: ['[email protected]'], subject: 'Hey' },
{ externalId: 'e2', date: '2026-07-04T11:00:00Z', from: { email: '[email protected]' }, to: ['[email protected]'], subject: 'Re: Hey' },
]);
expect(received.kind).toBe('message.received');
expect(sent.kind).toBe('message.sent');
});
it('excludes the account owner from participants and builds a stable dedupe key', () => {
const [c] = messageActivityCandidates(account, [
{ externalId: 'e1', date: '2026-07-04T10:00:00Z', from: '[email protected]', to: ['[email protected]', '[email protected]'], subject: 'Hey', bodyText: 'the full body\nwith newlines' },
]);
const emails = c.participants.map((p) => p.email);
expect(emails).toContain('[email protected]');
expect(emails).toContain('[email protected]');
expect(emails).not.toContain('[email protected]');
expect(c.dedupeKey).toBe('msg:acct-1:e1');
expect(c.metadata.externalId).toBe('e1');
// Privacy: summary is a collapsed preview, not the raw multi-line body.
expect(c.summary).toBe('the full body with newlines');
});
it('skips messages without a date or external id', () => {
expect(messageActivityCandidates(account, [{ subject: 'no date' }])).toEqual([]);
expect(messageActivityCandidates(account, [{ date: '2026-07-04T10:00:00Z' }])).toEqual([]);
});
it('sets metadata.handle to the sender email on received mail so the Tribe sender resolves (#2796)', () => {
// enrichActivityEvent derives the top-level personId from metadata.handle; without
// it, every email inbound is dropped and Tribe-outreach detection surfaces nothing.
const [received, sent] = messageActivityCandidates(account, [
{ externalId: 'e1', date: '2026-07-04T10:00:00Z', from: '[email protected]', to: ['[email protected]'], subject: 'Hey' },
{ externalId: 'e2', date: '2026-07-04T11:00:00Z', from: '[email protected]', to: ['[email protected]'], subject: 'Re: Hey' },
]);
expect(received.metadata.handle).toBe('[email protected]');
// Sent turns carry no counterpart handle (mirrors iMessage); email groups by threadId.
expect(sent.metadata.handle).toBeNull();
});
it('excludes send-as aliases from participants so a received 1:1-to-alias stays 1:1 (#2831)', () => {
// A 1:1 email delivered to the owner's alias (not account.email). Without alias
// filtering the alias survives as a second participant and the thread looks like a
// group, suppressing its outreach nudge.
const aliasAccount = { id: 'acct-1', type: 'gmail', email: '[email protected]', sendAsAliases: ['[email protected]'] };
const [c] = messageActivityCandidates(aliasAccount, [
{ externalId: 'e1', date: '2026-07-04T10:00:00Z', from: '[email protected]', to: ['[email protected]'], subject: 'Hey' },
]);
const emails = c.participants.map((p) => p.email);
expect(emails).toEqual(['[email protected]']); // exactly one counterpart — a true 1:1
expect(emails).not.toContain('[email protected]');
expect(c.metadata.handle).toBe('[email protected]');
});
it('still filters only the primary email when no aliases are stored (back-compat, #2831)', () => {
// Absent sendAsAliases → today's behavior: only account.email is dropped.
const [c] = messageActivityCandidates(account, [
{ externalId: 'e1', date: '2026-07-04T10:00:00Z', from: '[email protected]', to: ['[email protected]', '[email protected]'], subject: 'Hey' },
]);
const emails = c.participants.map((p) => p.email);
expect(emails).toContain('[email protected]');
expect(emails).toContain('[email protected]'); // not a known alias → kept
expect(emails).not.toContain('[email protected]');
});
it('classifies a reply as sent via the Gmail SENT label even from a send-as alias (#2796)', () => {
// From ([email protected]) != account owner ([email protected]), but the SENT label
// is authoritative — without it the reply is misclassified received and never
// cancels its inbound.
const [c] = messageActivityCandidates(account, [
{ externalId: 'e9', date: '2026-07-04T12:00:00Z', from: '[email protected]', to: ['[email protected]'], subject: 'Re: Hey', labels: ['SENT'] },
]);
expect(c.kind).toBe('message.sent');
expect(c.metadata.handle).toBeNull();
});
});
describe('calendarActivityCandidates', () => {
const account = { id: 'cal-1', type: 'google-calendar' };
const now = new Date('2026-07-04T23:00:00Z').getTime();
it('records finished, non-declined events with a duration', () => {
const [c] = calendarActivityCandidates(account, [
{
externalId: 'ev1', title: 'Standup', location: 'Zoom',
startTime: '2026-07-04T09:00:00Z', endTime: '2026-07-04T09:30:00Z',
organizer: '[email protected]', attendees: ['[email protected]'],
},
], now);
expect(c.source).toBe('calendar');
expect(c.kind).toBe('calendar.event');
expect(c.durationS).toBe(1800);
expect(c.dedupeKey).toBe('cal:cal-1:ev1');
expect(c.title).toBe('Standup');
});
it('skips cancelled, declined, and not-yet-finished events', () => {
expect(calendarActivityCandidates(account, [{ externalId: 'a', startTime: '2026-07-04T08:00:00Z', endTime: '2026-07-04T08:30:00Z', isCancelled: true }], now)).toEqual([]);
expect(calendarActivityCandidates(account, [{ externalId: 'b', startTime: '2026-07-04T08:00:00Z', endTime: '2026-07-04T08:30:00Z', myStatus: 'declined' }], now)).toEqual([]);
// ends in the future relative to `now`
expect(calendarActivityCandidates(account, [{ externalId: 'c', startTime: '2026-07-05T08:00:00Z', endTime: '2026-07-05T08:30:00Z' }], now)).toEqual([]);
});
it('anchors offset-less/all-day values in the user timezone, not the server OS zone', () => {
// Google all-day events normalize to "YYYY-MM-DDT00:00:00" (no offset). With
// the user in America/Los_Angeles, midnight July 4 local = 07:00Z — NOT
// whatever the Node process's OS timezone would parse it as (which could
// land the event inside July 3's local-day window for users west of UTC).
const nowLate = new Date('2026-07-06T00:00:00Z').getTime();
const [c] = calendarActivityCandidates(account, [
{ externalId: 'ad1', title: 'Independence Day', startTime: '2026-07-04T00:00:00', endTime: '2026-07-05T00:00:00', isAllDay: true },
], nowLate, 'America/Los_Angeles');
expect(c.happenedAt).toBe('2026-07-04T07:00:00.000Z');
expect(c.durationS).toBe(24 * 3600);
// Values WITH an explicit offset pass straight through untouched.
const [z] = calendarActivityCandidates(account, [
{ externalId: 'ad2', title: 'Zoomed', startTime: '2026-07-04T09:00:00Z', endTime: '2026-07-04T09:30:00Z' },
], nowLate, 'America/Los_Angeles');
expect(z.happenedAt).toBe('2026-07-04T09:00:00.000Z');
});
});
describe('newlyLearnedAliases (#2855)', () => {
it('returns only the aliases added since the previous set', () => {
expect(newlyLearnedAliases(['[email protected]'], ['[email protected]', '[email protected]'])).toEqual(['[email protected]']);
});
it('treats a first-ever fetch (no prior set) as all-new', () => {
expect(newlyLearnedAliases(undefined, ['[email protected]', '[email protected]'])).toEqual(['[email protected]', '[email protected]']);
expect(newlyLearnedAliases([], ['[email protected]'])).toEqual(['[email protected]']);
});
it('is empty for an unchanged set, so a routine sync does no repair work', () => {
expect(newlyLearnedAliases(['[email protected]', '[email protected]'], ['[email protected]', '[email protected]'])).toEqual([]);
});
it('ignores casing and whitespace so a cosmetic refresh is not a change', () => {
expect(newlyLearnedAliases(['[email protected]'], [' [email protected] '])).toEqual([]);
});
it('ignores removals — a dropped alias needs no backfill', () => {
expect(newlyLearnedAliases(['[email protected]', '[email protected]'], ['[email protected]'])).toEqual([]);
});
it('dedupes and drops empty entries', () => {
expect(newlyLearnedAliases([], ['[email protected]', '[email protected]', '', null])).toEqual(['[email protected]']);
});
it('returns [] for non-array input', () => {
expect(newlyLearnedAliases(null, null)).toEqual([]);
});
});
describe('stripParticipantsForAccount guards (#2855)', () => {
// The DB round-trip lives in humanActivity.db.test.js; these cases never reach
// the database at all — they assert the early return that keeps a pointless
// UPDATE (and a pool connection) off every routine sync.
const noDbClient = { query: () => { throw new Error('should not query'); } };
it('no-ops when the alias list is empty or normalizes to empty', async () => {
expect(await stripParticipantsForAccount('acct-1', 'gmail', [], { client: noDbClient })).toBe(0);
expect(await stripParticipantsForAccount('acct-1', 'gmail', ['', null], { client: noDbClient })).toBe(0);
expect(await stripParticipantsForAccount('acct-1', 'gmail', 'not-an-array', { client: noDbClient })).toBe(0);
});
it('no-ops when the account or source scope is missing', async () => {
expect(await stripParticipantsForAccount(null, 'gmail', ['[email protected]'], { client: noDbClient })).toBe(0);
expect(await stripParticipantsForAccount('acct-1', '', ['[email protected]'], { client: noDbClient })).toBe(0);
});
});