48 lines
2.2 KiB
TypeScript
48 lines
2.2 KiB
TypeScript
import { listSavedSqlConnections, removeSqlConnection, saveSqlConnection } from './saved-sql-connections';
|
|
|
|
describe('saved SQL connections', () => {
|
|
beforeEach(() => localStorage.clear());
|
|
|
|
it('never stores a password field', () => {
|
|
const saved = saveSqlConnection({ label: 'HIS · lector@his.local', engine: 'mssql', host: 'his.local', port: 1433, database: 'HIS', user: 'lector' });
|
|
expect(Object.keys(saved)).not.toContain('password');
|
|
expect(JSON.parse(localStorage.getItem('isssteleon.connect.savedSqlConnections')!)[0]).not.toHaveProperty('password');
|
|
});
|
|
|
|
it('upserts by connection shape instead of growing duplicates', () => {
|
|
const entry = { label: 'HIS', engine: 'mssql', host: 'his.local', port: 1433, database: 'HIS', user: 'lector' };
|
|
saveSqlConnection(entry);
|
|
saveSqlConnection({ ...entry, label: 'HIS (renamed)' });
|
|
const all = listSavedSqlConnections();
|
|
expect(all.length).toBe(1);
|
|
expect(all[0].label).toBe('HIS (renamed)');
|
|
});
|
|
|
|
it('keeps connections to different servers separate', () => {
|
|
saveSqlConnection({ label: 'A', engine: 'mssql', host: 'a.local', port: 1433, database: 'HIS', user: 'lector' });
|
|
saveSqlConnection({ label: 'B', engine: 'mysql', host: 'b.local', port: 3306, database: 'HIS', user: 'lector' });
|
|
expect(listSavedSqlConnections().length).toBe(2);
|
|
});
|
|
|
|
it('removes a saved connection by id', () => {
|
|
const saved = saveSqlConnection({ label: 'HIS', engine: 'mssql', host: 'his.local', port: 1433, database: 'HIS', user: 'lector' });
|
|
removeSqlConnection(saved.id);
|
|
expect(listSavedSqlConnections()).toEqual([]);
|
|
});
|
|
|
|
it('caps the list so it cannot grow without bound', () => {
|
|
for (let i = 0; i < 25; i++) {
|
|
saveSqlConnection({ label: `db${i}`, engine: 'mssql', host: `host${i}`, port: 1433, database: 'HIS', user: 'lector' });
|
|
}
|
|
expect(listSavedSqlConnections().length).toBeLessThanOrEqual(20);
|
|
});
|
|
|
|
it('degrades to an empty list rather than throwing when storage is unreadable', () => {
|
|
const spy = vi.spyOn(Storage.prototype, 'getItem').mockImplementation(() => {
|
|
throw new Error('blocked');
|
|
});
|
|
expect(listSavedSqlConnections()).toEqual([]);
|
|
spy.mockRestore();
|
|
});
|
|
});
|