Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
import { err, ok } from 'neverthrow';
import { describe, expect, it } from 'vitest';
import { cellValue, missingOptionLabels, mutationOp } from './cell-ops';
import type { DatabaseViewColumn } from './database-view';
import { UNAVAILABLE_OPTION } from './grid-cells';
function column(
dataType: DatabaseViewColumn['dataType'],
options: {
multi?: boolean;
entityType?: 'USER' | 'DOCUMENT';
relation?: boolean;
} = {}
): DatabaseViewColumn {
return {
id: 'column',
name: 'Column',
dataType,
isMultiSelect: options.multi ?? false,
specificEntityType: options.entityType ?? null,
options: [],
writable: true,
...(options.relation
? { relation: { databaseId: 'db', tableId: 'related' } }
: {}),
};
}
describe('grid values as cell values', () => {
it('names options by label, one for a single select and each for a multi select', () => {
expect(cellValue(column('SELECT_STRING'), 'Going')).toEqual(
ok({
type: 'options',
value: [{ label: 'Going' }],
})
);
expect(
cellValue(column('TAG', { multi: true }), '["Urgent","Backend"]')
).toEqual(
ok({
type: 'options',
value: [{ label: 'Urgent' }, { label: 'Backend' }],
})
);
expect(cellValue(column('SELECT_NUMBER', { multi: true }), '[]')).toEqual(
ok({
type: 'clear',
})
);
});
it('writes references with the kind the column points at, and relations as rows', () => {
expect(
cellValue(column('ENTITY', { entityType: 'DOCUMENT' }), 'document-1')
).toEqual(
ok({
type: 'entities',
value: [{ entityType: 'DOCUMENT', entityId: 'document-1' }],
})
);
expect(
cellValue(
column('ENTITY', { relation: true, multi: true }),
'["row-1","row-2"]'
)
).toEqual(ok({ type: 'rows', value: ['row-1', 'row-2'] }));
expect(cellValue(column('ENTITY', { relation: true }), '[]')).toEqual(
ok({
type: 'clear',
})
);
});
it('keeps empty text but clears any other empty cell', () => {
expect(cellValue(column('STRING'), '')).toEqual(
ok({
type: 'text',
value: '',
})
);
expect(cellValue(column('LINK'), '')).toEqual(ok({ type: 'clear' }));
expect(cellValue(column('NUMBER'), '')).toEqual(ok({ type: 'clear' }));
expect(cellValue(column('DATE'), null)).toEqual(ok({ type: 'clear' }));
});
it('writes checkboxes, numbers, links and dates as the values they are', () => {
expect(cellValue(column('BOOLEAN'), 1)).toEqual(
ok({
type: 'boolean',
value: true,
})
);
expect(cellValue(column('BOOLEAN'), 'false')).toEqual(
ok({
type: 'boolean',
value: false,
})
);
expect(cellValue(column('NUMBER'), '12.5')).toEqual(
ok({
type: 'number',
value: 12.5,
})
);
expect(cellValue(column('LINK'), 'https://macro.com')).toEqual(
ok({
type: 'link',
value: ['https://macro.com'],
})
);
expect(cellValue(column('DATE'), '2026-09-30')).toEqual(
ok({
type: 'date',
value: '2026-09-30T00:00:00Z',
})
);
expect(cellValue(column('NUMBER'), 'lots')).toEqual(
err({ kind: 'not-a-number' })
);
});
it('refuses numbers the column inference would not read as one', () => {
expect(cellValue(column('NUMBER'), ' ')).toEqual(
err({ kind: 'not-a-number' })
);
expect(cellValue(column('NUMBER'), '0x10')).toEqual(
err({ kind: 'not-a-number' })
);
expect(cellValue(column('NUMBER'), ' 42 ')).toEqual(
ok({ type: 'number', value: 42 })
);
});
});
describe('grid values as time ranges', () => {
it('writes an interval, or one day as a one-day range', () => {
expect(
cellValue(
column('DATE_RANGE'),
'2026-03-01T00:00:00.000Z/2026-03-05T00:00:00.000Z'
)
).toEqual(
ok({
type: 'date_range',
value: {
start: '2026-03-01T00:00:00.000Z',
end: '2026-03-05T00:00:00.000Z',
},
})
);
expect(cellValue(column('DATE_RANGE'), '2026-03-01 / 2026-03-05')).toEqual(
ok({
type: 'date_range',
value: {
start: '2026-03-01T00:00:00.000Z',
end: '2026-03-05T00:00:00.000Z',
},
})
);
expect(cellValue(column('DATE_RANGE'), '2026-03-01')).toEqual(
ok({
type: 'date_range',
value: {
start: '2026-03-01T00:00:00.000Z',
end: '2026-03-01T00:00:00.000Z',
},
})
);
expect(cellValue(column('DATE_RANGE'), '')).toEqual(ok({ type: 'clear' }));
});
it('writes a timed interval as its two instants in UTC', () => {
expect(
cellValue(
column('DATE_RANGE'),
'2026-03-01T14:00:00.000Z/2026-03-01T22:30:00.000Z'
)
).toEqual(
ok({
type: 'date_range',
value: {
start: '2026-03-01T14:00:00.000Z',
end: '2026-03-01T22:30:00.000Z',
},
})
);
expect(
cellValue(
column('DATE_RANGE'),
'2026-03-01T09:00:00-05:00/2026-03-01T17:00:00-05:00'
)
).toEqual(
ok({
type: 'date_range',
value: {
start: '2026-03-01T14:00:00.000Z',
end: '2026-03-01T22:00:00.000Z',
},
})
);
expect(
cellValue(
column('DATE_RANGE'),
'2026-03-01T17:00:00Z/2026-03-01T09:00:00Z'
)
).toEqual(err({ kind: 'not-a-date-range' }));
});
it('refuses a range that ends before it starts, or is not dates', () => {
expect(cellValue(column('DATE_RANGE'), '2026-03-05/2026-03-01')).toEqual(
err({ kind: 'not-a-date-range' })
);
expect(cellValue(column('DATE_RANGE'), 'next week')).toEqual(
err({ kind: 'not-a-date-range' })
);
expect(
cellValue(column('DATE_RANGE'), '2026-03-01/2026-03-02/2026-03-03')
).toEqual(err({ kind: 'not-a-date-range' }));
});
});
describe('grid values a column cannot take', () => {
it('refuses references to rows outside a relation column', () => {
const rows: DatabaseViewColumn = {
...column('ENTITY'),
specificEntityType: 'DATABASE_ROW',
};
expect(cellValue(rows, 'row-1')).toEqual(
err({ kind: 'relation-as-entity' })
);
});
it('refuses a multi-valued cell that is not a JSON array instead of clearing it', () => {
expect(cellValue(column('TAG', { multi: true }), 'Urgent')).toEqual(
err({ kind: 'malformed-list' })
);
expect(cellValue(column('TAG', { multi: true }), '{"a":1}')).toEqual(
err({ kind: 'malformed-list' })
);
expect(cellValue(column('TAG', { multi: true }), '[null]')).toEqual(
err({ kind: 'malformed-list' })
);
expect(cellValue(column('ENTITY', { relation: true }), 'row-1')).toEqual(
err({ kind: 'malformed-list' })
);
});
it('refuses a bare id for an entity column that names no kind', () => {
expect(cellValue(column('ENTITY'), 'someone')).toEqual(
err({ kind: 'untyped-entity' })
);
});
it('refuses an option the grid could not name', () => {
expect(
cellValue(
column('TAG', { multi: true }),
JSON.stringify(['Urgent', UNAVAILABLE_OPTION])
)
).toEqual(err({ kind: 'unavailable-option' }));
});
it('refuses the whole op when one of its cells is refused', () => {
expect(
mutationOp(
'table',
{ kind: 'create', values: { name: 'Ada', age: 'old' } },
(columnId) =>
columnId === 'name'
? ok(column('STRING'))
: err({ kind: 'read-only-column' })
)
).toEqual(err({ kind: 'read-only-column' }));
});
});
describe('grid edits as ops', () => {
it('deletes a record as one rows delete op', () => {
expect(
mutationOp('table', { kind: 'delete', rowId: 'row-1' }, () =>
ok(column('STRING'))
)
).toEqual(
ok({
kind: 'rows',
table: 'table',
change: { kind: 'delete', rows: ['row-1'] },
})
);
});
it('creates a record with every value it starts with, naming options by label', () => {
expect(
mutationOp(
'table',
{ kind: 'create', values: { status: 'New lane' } },
() => ok(column('SELECT_STRING'))
)
).toEqual(
ok({
kind: 'rows',
table: 'table',
change: {
kind: 'insert',
rows: [
[
{
column: 'status',
value: { type: 'options', value: [{ label: 'New lane' }] },
},
],
],
},
})
);
});
it('lists the labels a write names that its columns lack, once each', () => {
expect(
missingOptionLabels(
{
kind: 'rows',
table: 'table',
change: {
kind: 'update',
changes: {
kind: 'per_row',
rows: [
{
row: 'row-1',
cells: [
{
column: 'tags',
value: {
type: 'options',
value: [
{ label: 'urgent' },
{ label: 'Blocked' },
{ label: 'blocked' },
{ id: 'option-1' },
],
},
},
{ column: 'name', value: { type: 'text', value: 'Ada' } },
],
},
],
},
},
},
(columnId) => (columnId === 'tags' ? ['Urgent'] : [])
)
).toEqual([{ column: 'tags', labels: ['Blocked'] }]);
});
});
it('clears a rectangle in one uniform row operation', () => {
const result = mutationOp(
'table',
{ kind: 'clear', rowIds: ['one', 'two'], columnIds: ['name', 'count'] },
() => ok(column('STRING'))
);
expect(result._unsafeUnwrap()).toEqual({
kind: 'rows',
table: 'table',
change: {
kind: 'update',
changes: {
kind: 'uniform',
rows: ['one', 'two'],
cells: [
{ column: 'name', value: { type: 'clear' } },
{ column: 'count', value: { type: 'clear' } },
],
},
},
});
});