garage44.eu/jeroen/midas

logclone: git clone https://garage44.eu/jeroen/midas.git

.. /web/src/lib/memory.test.ts

raw

import {expect, test} from 'bun:test'

import {notes, parse, search} from '@/lib/memory'

const note = (text: string, ts = 1) => ({text, line: '', ts, by: 't', deleted: false})

test('the frontmatter gives the name, description and type, and the rest is the body', () => {
    const p = parse('---\nname: terse\ndescription: "short \\"answers\\""\nmetadata:\n  type: feedback\n---\n\nBe terse.\n')
    expect(p).toEqual({name: 'terse', description: 'short "answers"', type: 'feedback', body: 'Be terse.'})
    expect(parse('no frontmatter').body).toBe('no frontmatter')
})

test('removed notes are left out, and the newest come first', () => {
    const all = notes({
        workspace: {'workspace--who.md': note('---\nname: who\n---\nSam', 5)},
        'acme/cartopolis': {'old.md': note('old', 1), 'gone.md': {...note('x', 9), deleted: true}},
    })
    expect(all.map((n) => [n.set, n.file, n.name])).toEqual([
        ['workspace', 'workspace--who.md', 'who'],
        ['acme/cartopolis', 'old.md', 'old'],
    ])
})

test('a search needs every word, in any case, and puts name matches first', () => {
    const all = notes({
        r: {
            'a.md': note('---\nname: a\ndescription: about builds\n---\nslow cargo here', 3),
            'cargo-lock.md': note('---\nname: cargo-lock\ndescription: locks\n---\nnothing slow', 2),
            'c.md': note('---\nname: c\n---\nunrelated', 1),
        },
    })
    expect(search(all, 'Cargo SLOW').map((n) => n.file)).toEqual(['cargo-lock.md', 'a.md'])
    expect(search(all, '  ').length).toBe(3)
    expect(search(all, 'cargo unrelated')).toEqual([])
})