---@diagnostic disable: duplicate-set-field, need-check-nil, param-type-mismatch, assign-type-mismatch local store = require 'bugabinga.pivi.store' --- Writes a session file whose header names a cwd, mirroring Pi's layout. local write_session = function( dir, bucket, name, cwd ) local bucket_dir = dir .. '/' .. bucket vim.fn.mkdir( bucket_dir, 'p' ) local header = vim.json.encode { type = 'session', id = name:match '_(.+)%.jsonl$', timestamp = name:match '^(.+)_', cwd = cwd, } vim.fn.writefile( { header, '{"type":"model_change"}', }, bucket_dir .. '/' .. name ) end describe( 'pivi store', function() local root before_each( function() root = vim.fs.normalize( vim.fn.tempname() ) vim.fn.mkdir( root, 'p' ) end ) after_each( function() vim.fn.delete( root, 'rf' ) end ) it( 'resolves the session directory from the environment first', function() local original = vim.env.PI_CODING_AGENT_SESSION_DIR vim.env.PI_CODING_AGENT_SESSION_DIR = '/tmp/custom/pi/sessions' assert.equal( '/tmp/custom/pi/sessions', store.directory() ) vim.env.PI_CODING_AGENT_SESSION_DIR = original end ) it( 'uses the Pi agent directory rather than XDG state for default storage', function() local original, explicit = vim.env.PI_CODING_AGENT_DIR, vim.env.PI_CODING_AGENT_SESSION_DIR vim.env.PI_CODING_AGENT_SESSION_DIR = nil vim.env.PI_CODING_AGENT_DIR = root assert.equal( root .. '/sessions', store.directory() ) vim.env.PI_CODING_AGENT_DIR, vim.env.PI_CODING_AGENT_SESSION_DIR = original, explicit end ) it( 'lists explicit flat session directories and filters each header', function() local mine = '/project' write_session( root, '.', '2026-01-01T00-00-00-000Z_old.jsonl', mine ) write_session( root, '.', '2026-02-01T00-00-00-000Z_other.jsonl', '/other' ) local found = store.sessions( mine, root ) assert.equal( 1, #found ) assert.equal( 'old', found[1].id ) end ) it( 'validates arbitrary session paths against their authoritative header identity', function() local path = root .. '/arbitrary.jsonl' vim.fn.writefile( { vim.json.encode { type = 'session', id = 'saved', cwd = root, }, }, path ) assert.is_true( store.valid_file( path, 'saved' ) ) assert.is_false( store.valid_file( path, 'other' ) ) assert.is_false( store.valid_file( root .. '/missing.jsonl', 'saved' ) ) assert.is_false( store.valid_file( nil, 'saved' ) ) vim.fn.writefile( { 'invalid JSON', }, path ) assert.is_false( store.valid_file( path, 'saved' ) ) vim.fn.writefile( { '{"type":"message","id":"saved"}', }, path ) assert.is_false( store.valid_file( path, 'saved' ) ) end ) it( 'parses id and timestamp from a session filename', function() local parsed = store.parse_name '2026-04-05T22-31-02-368Z_433fbbc8-0d7d-4006.jsonl' assert.equal( '433fbbc8-0d7d-4006', parsed.id ) assert.equal( '2026-04-05T22-31-02-368Z', parsed.timestamp ) end ) it( 'keeps underscores in native custom session ids', function() local parsed = store.parse_name '2026-04-05T22-31-02-368Z_task_build.jsonl' assert.equal( 'task_build', parsed.id ) assert.equal( '2026-04-05T22-31-02-368Z', parsed.timestamp ) end ) it( 'lists only the sessions whose header names the cwd, newest first', function() local mine = '/home/me/Workspace/project' write_session( root, '--home-me-Workspace-project--', '2026-01-01T00-00-00-000Z_old.jsonl', mine ) write_session( root, '--home-me-Workspace-project--', '2026-02-02T00-00-00-000Z_new.jsonl', mine ) write_session( root, '--home-me-other--', '2026-03-03T00-00-00-000Z_other.jsonl', '/home/me/other' ) local found = store.sessions( mine, root ) assert.equal( 2, #found ) assert.equal( 'new', found[1].id ) assert.equal( 'old', found[2].id ) end ) it( 'returns nothing for a cwd with no recorded session', function() write_session( root, '--home-me-a--', '2026-01-01T00-00-00-000Z_a.jsonl', '/home/me/a' ) assert.same( {}, store.sessions( '/home/me/absent', root ) ) end ) it( 'ignores a blank cwd', function() assert.same( {}, store.sessions( '', root ) ) end ) end )