fix: clamp getAllSessions pagination params, add cleanupAliases success field, add 10 tests

- session-manager: clamp offset/limit to safe non-negative integers to
  prevent negative offset counting from end and NaN returning empty results
- session-aliases: add success field to cleanupAliases return value for
  API contract consistency with setAlias/deleteAlias/renameAlias
This commit is contained in:
Affaan Mustafa
2026-02-13 02:16:22 -08:00
parent 2dbba8877b
commit f33ed4c49e
4 changed files with 97 additions and 2 deletions

View File

@@ -446,9 +446,17 @@ function cleanupAliases(sessionExists) {
if (removed.length > 0 && !saveAliases(data)) {
log('[Aliases] Failed to save after cleanup');
return {
success: false,
totalChecked: Object.keys(data.aliases).length + removed.length,
removed: removed.length,
removedAliases: removed,
error: 'Failed to save after cleanup'
};
}
return {
success: true,
totalChecked: Object.keys(data.aliases).length + removed.length,
removed: removed.length,
removedAliases: removed

View File

@@ -189,12 +189,21 @@ function getSessionStats(sessionPathOrContent) {
*/
function getAllSessions(options = {}) {
const {
limit = 50,
offset = 0,
limit: rawLimit = 50,
offset: rawOffset = 0,
date = null,
search = null
} = options;
// Clamp offset and limit to safe non-negative integers.
// Without this, negative offset causes slice() to count from the end,
// and NaN values cause slice() to return empty or unexpected results.
// Note: cannot use `|| default` because 0 is falsy — use isNaN instead.
const offsetNum = Number(rawOffset);
const offset = Number.isNaN(offsetNum) ? 0 : Math.max(0, Math.floor(offsetNum));
const limitNum = Number(rawLimit);
const limit = Number.isNaN(limitNum) ? 50 : Math.max(1, Math.floor(limitNum));
const sessionsDir = getSessionsDir();
if (!fs.existsSync(sessionsDir)) {