59 lines
2.3 KiB
TypeScript
59 lines
2.3 KiB
TypeScript
import { describe, expect, it } from 'vitest';
|
|
|
|
import { AESKey } from 'src/crypto/aes-key.js';
|
|
|
|
import { EncryptedStorage } from 'src/storage/encrypted-storage.js';
|
|
import { StorageMemory } from 'src/storage/storage-memory.js';
|
|
import { StorageMemorySynced } from 'src/storage/storage-memory-synced.js';
|
|
|
|
describe('storage regressions', () => {
|
|
it('does not treat $eq as fully resolved when mixed with other operators', async () => {
|
|
const storage = StorageMemory.from<{ age: number }>(['age']);
|
|
await storage.insertMany([{ age: 25 }, { age: 35 }]);
|
|
|
|
const results = await storage.find({ age: { $eq: 25, $gt: 30 } });
|
|
expect(results).toEqual([]);
|
|
});
|
|
|
|
it('normalizes conflicting lower/upper bounds to strictest constraints', async () => {
|
|
const storage = StorageMemory.from<{ age: number }>(['age']);
|
|
await storage.insertMany([{ age: 30 }, { age: 31 }, { age: 32 }]);
|
|
|
|
const lower = await storage.find({ age: { $gt: 30, $gte: 30 } });
|
|
expect(lower.map((d) => d.age).sort((a, b) => a - b)).toEqual([31, 32]);
|
|
|
|
const upper = await storage.find({ age: { $lt: 32, $lte: 32 } });
|
|
expect(upper.map((d) => d.age).sort((a, b) => a - b)).toEqual([30, 31]);
|
|
});
|
|
|
|
it('formats plaintext operator values in encrypted filters', async () => {
|
|
type Doc = { createdAt: Date; name: string };
|
|
const key = await AESKey.fromSeed('storage-regression-test-key');
|
|
const base = StorageMemory.from<Record<string, any>>(['createdAt']);
|
|
const storage = EncryptedStorage.from<Doc>(base, key, { plaintextKeys: ['createdAt'] });
|
|
|
|
await storage.insertOne({
|
|
createdAt: new Date('2024-01-02T00:00:00.000Z'),
|
|
name: 'alice',
|
|
});
|
|
|
|
const results = await storage.find({
|
|
createdAt: { $gte: new Date('2024-01-01T00:00:00.000Z') },
|
|
});
|
|
expect(results).toHaveLength(1);
|
|
expect(results[0].name).toBe('alice');
|
|
});
|
|
|
|
it('primes derived child cache in StorageMemorySynced', async () => {
|
|
const store = StorageMemory.from<Record<string, any>>();
|
|
const child = store.deriveChild<{ value: number }>('child');
|
|
await child.insertOne({ value: 42 });
|
|
|
|
const synced = await StorageMemorySynced.create(store);
|
|
const syncedChild = synced.deriveChild<{ value: number }>('child');
|
|
const results = await syncedChild.find();
|
|
|
|
expect(results).toEqual([{ value: 42 }]);
|
|
});
|
|
});
|