11// Sessions command tests cover listing, details, filtering, and transcript display behavior.
22import { afterEach , beforeEach , describe , expect , it , vi } from "vitest" ;
3+ import { ExpectedCliError } from "../cli/failure-output.js" ;
34import {
45 assignSessionOwner ,
56 recordSessionParticipant ,
@@ -16,9 +17,6 @@ import {
1617 writeStore ,
1718} from "./sessions.test-helpers.js" ;
1819
19- // Disable colors for deterministic snapshots.
20- process . env . FORCE_COLOR = "0" ;
21-
2220mockSessionsConfig ( ) ;
2321
2422import { sessionsCommand } from "./sessions.js" ;
@@ -31,6 +29,7 @@ describe("sessionsCommand", () => {
3129
3230 afterEach ( ( ) => {
3331 resetMockSessionsConfig ( ) ;
32+ vi . restoreAllMocks ( ) ;
3433 vi . useRealTimers ( ) ;
3534 } ) ;
3635
@@ -727,63 +726,45 @@ describe("sessionsCommand", () => {
727726 ] ) ;
728727 } ) ;
729728
730- it ( "rejects invalid --active values" , async ( ) => {
731- const store = await writeStore (
732- {
733- "agent:main:demo" : {
734- sessionId : "demo" ,
735- updatedAt : Date . now ( ) - 5 * 60_000 ,
736- } ,
737- } ,
738- "sessions-active-invalid" ,
729+ it . each ( [
730+ {
731+ name : "invalid active minutes" ,
732+ options : { active : "0" } ,
733+ message : "--active must be a positive number of minutes, for example --active 30." ,
734+ } ,
735+ {
736+ name : "partially numeric active minutes" ,
737+ options : { active : "10m" } ,
738+ message : "--active must be a positive number of minutes, for example --active 30." ,
739+ } ,
740+ {
741+ name : "an invalid limit" ,
742+ options : { limit : "0" } ,
743+ message : '--limit must be a positive integer or "all", for example --limit 25.' ,
744+ } ,
745+ {
746+ name : "active minutes before an invalid limit" ,
747+ options : { active : "0" , limit : "0" } ,
748+ message : "--active must be a positive number of minutes, for example --active 30." ,
749+ } ,
750+ ] ) ( "rejects $name before reading session stores" , async ( { options, message } ) => {
751+ const listSessionEntries = vi . spyOn (
752+ await import ( "../config/sessions/session-accessor.js" ) ,
753+ "listSessionEntriesReadOnly" ,
739754 ) ;
740- const { runtime, errors } = makeRuntime ( ) ;
741-
742- await expect ( sessionsCommand ( { store, active : "0" } , runtime ) ) . rejects . toThrow ( "exit 1" ) ;
743- expect ( errors ) . toStrictEqual ( [
744- "--active must be a positive number of minutes, for example --active 30." ,
745- ] ) ;
746-
747- cleanupStore ( store ) ;
748- } ) ;
749-
750- it ( "rejects partial --active values" , async ( ) => {
751- const store = await writeStore (
752- {
753- "agent:main:demo" : {
754- sessionId : "demo" ,
755- updatedAt : Date . now ( ) - 5 * 60_000 ,
756- } ,
757- } ,
758- "sessions-active-partial" ,
759- ) ;
760- const { runtime, errors } = makeRuntime ( ) ;
761-
762- await expect ( sessionsCommand ( { store, active : "10m" } , runtime ) ) . rejects . toThrow ( "exit 1" ) ;
763- expect ( errors ) . toStrictEqual ( [
764- "--active must be a positive number of minutes, for example --active 30." ,
765- ] ) ;
766-
767- cleanupStore ( store ) ;
768- } ) ;
769-
770- it ( "rejects invalid --limit values" , async ( ) => {
771- const store = await writeStore (
772- {
773- "agent:main:demo" : {
774- sessionId : "demo" ,
775- updatedAt : Date . now ( ) - 5 * 60_000 ,
776- } ,
777- } ,
778- "sessions-limit-invalid" ,
779- ) ;
780- const { runtime, errors } = makeRuntime ( ) ;
781-
782- await expect ( sessionsCommand ( { store, limit : "0" } , runtime ) ) . rejects . toThrow ( "exit 1" ) ;
783- expect ( errors ) . toStrictEqual ( [
784- '--limit must be a positive integer or "all", for example --limit 25.' ,
785- ] ) ;
786-
787- cleanupStore ( store ) ;
755+ const { runtime, logs, errors } = makeRuntime ( ) ;
756+ const runtimeExit = vi . spyOn ( runtime , "exit" ) ;
757+ const execution = sessionsCommand ( options , runtime ) ;
758+
759+ await expect ( execution ) . rejects . toBeInstanceOf ( ExpectedCliError ) ;
760+ await expect ( execution ) . rejects . toMatchObject ( {
761+ message,
762+ humanOutput : message ,
763+ machineOutput : message ,
764+ } ) ;
765+ expect ( logs ) . toEqual ( [ ] ) ;
766+ expect ( errors ) . toEqual ( [ ] ) ;
767+ expect ( runtimeExit ) . not . toHaveBeenCalled ( ) ;
768+ expect ( listSessionEntries ) . not . toHaveBeenCalled ( ) ;
788769 } ) ;
789770} ) ;
0 commit comments