Refine journal search filtering

This commit is contained in:
Scott Idem
2026-05-04 16:58:48 -04:00
parent 5cbdec3b5c
commit 285ef84b7e
9 changed files with 379 additions and 242 deletions

View File

@@ -1,80 +1,110 @@
import { describe, it, expect } from 'vitest';
// Simulating the filter logic from ae_comp__journal_entry_obj_li.svelte
function filterEntries(list: any[], ae_loc: any) {
if (!list) return null;
return list.filter((item: any) => {
if (!item) return false;
import {
journal_entry_filter_list,
journal_entry_matches_search
} from '$lib/ae_journals/ae_journals_search_helpers';
const is_hidden = item.hide === true || item.hide === 1;
const is_disabled = item.enable === false || item.enable === 0;
// Standard Visibility: Filter out hidden/disabled if not in Edit Mode
if (!ae_loc.edit_mode) {
return !is_hidden && !is_disabled;
}
// Edit Mode Gating:
// - To see Hidden: Must have Trusted Access or higher
if (is_hidden && !ae_loc.trusted_access) return false;
// - To see Disabled: Must have Administrator Access or higher
if (is_disabled && !ae_loc.administrator_access) return false;
return true;
});
}
describe('Journal Entry Visibility Filtering', () => {
describe('Journal Entry Search Filtering', () => {
const mockEntries = [
{ id: '1', name: 'Normal Entry', hide: false, enable: true },
{ id: '2', name: 'Hidden Entry', hide: true, enable: true },
{ id: '3', name: 'Disabled Entry', hide: false, enable: false },
{ id: '4', name: 'Hidden & Disabled', hide: true, enable: false }
{
id: '4',
journal_entry_id: '4',
name: 'Hidden & Disabled',
hide: true,
enable: false,
tmp_sort_1: 'd'
},
{
id: '3',
journal_entry_id: '3',
name: 'Disabled Entry',
hide: false,
enable: false,
tmp_sort_1: 'c'
},
{
id: '2',
journal_entry_id: '2',
name: 'Hidden Entry',
hide: true,
enable: true,
tmp_sort_1: 'b'
},
{
id: '1',
journal_entry_id: '1',
name: 'Normal Entry',
summary: 'Alpha notes',
content: 'Beta details',
category_code: 'general',
hide: false,
enable: true,
tmp_sort_1: 'a'
}
];
it('should show only normal entries when Edit Mode is OFF (Manager)', () => {
const ae_loc = {
edit_mode: false,
trusted_access: true,
administrator_access: true
};
const result = filterEntries(mockEntries, ae_loc);
expect(result?.length).toBe(1);
expect(result?.[0].id).toBe('1');
});
it('should return all entries when filters are broad', () => {
const result = journal_entry_filter_list(mockEntries, {
str: '',
cat: '',
enabled: 'all',
hidden: 'all',
limit: 2
});
it('should show hidden entries to Trusted users when Edit Mode is ON', () => {
const ae_loc = {
edit_mode: true,
trusted_access: true,
administrator_access: false
};
const result = filterEntries(mockEntries, ae_loc);
// Should see Normal (1) and Hidden (2). Should NOT see Disabled (3, 4)
expect(result?.length).toBe(2);
expect(result?.map((r) => r.id)).toContain('1');
expect(result?.map((r) => r.id)).toContain('2');
});
it('should show everything to Administrators when Edit Mode is ON', () => {
const ae_loc = {
edit_mode: true,
trusted_access: true,
administrator_access: true
};
const result = filterEntries(mockEntries, ae_loc);
expect(result?.length).toBe(4);
expect(result?.map((entry) => entry.id)).toEqual([
'4',
'3',
'2',
'1'
]);
});
it('should hide everything sensitive to Public users even if Edit Mode is ON (Safety Check)', () => {
const ae_loc = {
edit_mode: true,
trusted_access: false,
administrator_access: false
};
const result = filterEntries(mockEntries, ae_loc);
it('should filter by enabled and hidden status', () => {
const result = journal_entry_filter_list(mockEntries, {
str: '',
cat: '',
enabled: 'enabled',
hidden: 'not_hidden',
limit: 50
});
expect(result?.length).toBe(1);
expect(result?.[0].id).toBe('1');
});
it('should match text across summary and content', () => {
expect(
journal_entry_matches_search(mockEntries[3], {
str: 'alpha',
cat: '',
enabled: 'all',
hidden: 'all'
})
).toBe(true);
expect(
journal_entry_matches_search(mockEntries[3], {
str: 'beta',
cat: '',
enabled: 'all',
hidden: 'all'
})
).toBe(true);
});
it('should limit text-filtered results after sorting', () => {
const result = journal_entry_filter_list(mockEntries, {
str: 'entry',
cat: '',
enabled: 'all',
hidden: 'all',
limit: 2
});
expect(result?.length).toBe(2);
expect(result?.map((entry) => entry.id)).toEqual(['3', '2']);
});
});