---@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( '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( '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 )