Files
CatoandClaude 234d424707 Files on a message: an attachment policy, read receipts that ignore an assistant, whole-word mentions (#54)
A messenger's costs live in its files, so what may be attached is a rule
this package can answer: checkAttachment (allowlist, a ceiling per kind,
a count per message, a quota per person), the lifetime predicates, and
the Attachment type a Message can carry. Nothing is stored or signed
here.

readState answers none/some/all over the humans a message was for. In
OrangeCat an assistant that had joined a conversation never read
anything, so every later message stayed at delivered.

mentionsAny/mentionMatcher export the whole-word matcher whenMentioned
used, now aware of letters outside ASCII.

mergeMessages, visibleMessages and unreadMessages are generic over the
caller's message type.

Released as 0.1.2: additive, inside every consumer's ^0.1.1 range.


Claude-Session: https://claude.ai/code/session_019MDtmsmMXvM981mcszxEA6

Co-authored-by: Claude <noreply@anthropic.com>
2026-11-12 07:13:19 +01:00

121 lines
3.8 KiB
JavaScript

import { test, before } from 'node:test';
import assert from 'node:assert/strict';
import { execFileSync } from 'node:child_process';
import { mkdtempSync, mkdirSync, writeFileSync, readdirSync, existsSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
/**
* Consume the package the way npm does.
*
* Every other test in this repo imports `../dist/index.js` by relative path,
* which never touches the `exports` map or the `files` allowlist — the two
* things a real consumer resolves through. A subpath missing from `exports`, a
* `types` path pointing at a file that was never built, or a `files` list that
* forgets `dist` all ship green under a relative import. The first witness
* would be whoever runs `npm install`.
*
* So: pack the real tarball, unpack it as `node_modules/threadkit`, and import
* it by bare specifier from outside the repo.
*/
const PUBLIC_API = [
'DEFAULT_ATTACHMENT_POLICY',
'afterEveryMessage',
'attachmentExpiresAt',
'attachmentKind',
'canRead',
'canSeeMessage',
'canWrite',
'checkAttachment',
'compareMessages',
'defaultRespondPolicy',
'findParticipant',
'isAbandonedUpload',
'isAttachmentExpired',
'isPending',
'mentionMatcher',
'mentionsAny',
'mergeMessages',
'readState',
'readersOf',
'runAiTurn',
'storedBytes',
'unreadCount',
'unreadMessages',
'unreadThreadCount',
'visibilityWindow',
'visibleMessages',
'whenMentioned',
];
let workspace;
let installed;
let probe;
before(() => {
workspace = mkdtempSync(join(tmpdir(), 'threadkit-pack-'));
installed = join(workspace, 'node_modules', '@bitbaum/threadkit');
mkdirSync(installed, { recursive: true });
execFileSync('npm', ['pack', '--silent', '--pack-destination', workspace], {
cwd: process.cwd(),
stdio: ['ignore', 'ignore', 'inherit'],
});
const tarball = readdirSync(workspace).find((f) => f.endsWith('.tgz'));
assert.ok(tarball, 'npm pack produced no tarball');
execFileSync('tar', ['-xzf', join(workspace, tarball), '-C', installed, '--strip-components=1']);
// Each probe records failure as a *value*, never a throw. A broken exports map
// that crashes this hook would fail every assertion in the file at once and
// bury which entry point actually broke.
writeFileSync(
join(workspace, 'probe.mjs'),
[
'const out = {};',
'try { out.resolved = import.meta.resolve("@bitbaum/threadkit"); } catch { out.resolved = null; }',
'try { out.exports = Object.keys(await import("@bitbaum/threadkit")).sort(); } catch { out.exports = null; }',
'console.log(JSON.stringify(out));',
].join('\n'),
);
probe = JSON.parse(
execFileSync('node', [join(workspace, 'probe.mjs')], {
cwd: workspace,
encoding: 'utf8',
}),
);
});
test('the package resolves from a consumer install', () => {
assert.ok(probe.resolved, '"@bitbaum/threadkit" did not resolve through its own exports map');
});
test('the entry point exposes its whole public API', () => {
assert.ok(probe.exports, 'importing "@bitbaum/threadkit" from a consumer install threw');
for (const name of PUBLIC_API) {
assert.ok(probe.exports.includes(name), `"${name}" is missing from the published entry point`);
}
});
test('the type declarations it advertises are actually in the tarball', () => {
const pkg = JSON.parse(
execFileSync('node', ['-p', 'JSON.stringify(require("./package.json"))'], {
cwd: installed,
encoding: 'utf8',
}),
);
const types = pkg.exports['.'].types;
assert.ok(
existsSync(join(installed, types)),
`the package advertises types at ${types}, which is not in the tarball`,
);
});
test('the tarball carries the documentation npm will render', () => {
for (const file of ['README.md', 'LICENSE']) {
assert.ok(existsSync(join(installed, file)), `${file} is missing from the tarball`);
}
});