Skip to content

Built for people who want to own their automations. Join the waitlist for an invite.

Package listing

@kody/stash

src/lib/kinds.test.ts

43 lines · 1.5 KB · TypeScript
import assert from 'node:assert/strict'
import test from 'node:test'
import {
	extractEntities,
	extractTags,
	inferKind,
	scoreHaystack,
	summarize,
} from './kinds.ts'

test('inferKind maps reminders, contacts, instructions, and facts', () => {
	assert.equal(inferKind('Remind me to call the dentist'), 'reminder')
	assert.equal(inferKind('Need to follow up tomorrow'), 'reminder')
	assert.equal(inferKind("Sam's phone is 555-0100"), 'contact')
	assert.equal(inferKind('How to reset the garage keypad'), 'instruction')
	assert.equal(inferKind('Projector bulb is 250W'), 'fact')
	assert.equal(inferKind('Grocery list: milk'), 'note')
})

test('extractTags keeps hashtags, explicit tags, and inferred kind tags', () => {
	assert.deepEqual(
		extractTags('Garage code 1234 #home', ['house'], 'note'),
		['home', 'house', 'note'],
	)
})

test('extractEntities finds emails and title phrases', () => {
	const entities = extractEntities('Email Sam Smith at sam@example.com')
	assert.ok(entities.includes('sam@example.com'))
	assert.ok(entities.some((entity) => entity.includes('sam')))
})

test('summarize uses the first line and truncates long text', () => {
	assert.equal(summarize('First line\nSecond'), 'First line')
	assert.equal(summarize('x'.repeat(120)).endsWith('...'), true)
})

test('scoreHaystack ranks exact phrase matches above unrelated text', () => {
	assert.ok(
		scoreHaystack('garage door code 1234', 'garage code') >
			scoreHaystack('unrelated recipe', 'garage code'),
	)
})