import test from 'ava';
import Cache from '../lib/Cache';

test.cb('infinite ranges should not be queried unnecessarily', t => {
	const cache = new Cache();

	cache.put({
		knowledge: { 'asdf:xyz!(:)': [ [ 12345, Infinity ] ] },
		indexes: { 'asdf:xyz!(:)': [] }
	});

	cache.onChange((changes) => {
		if (changes.queries && changes.queries['asdf:xyz!(:)']) {
			t.deepEqual(
				changes.queries['asdf:xyz!(:)'].arr,
				[ [ 12345, 20, 0 ] ]
			);
			t.end();
		}
	});

	cache.query('asdf:xyz!(:)', [ 12345, 20, 0 ], () => {});
});


test('should avoid duplicates', t => {
	const cache = new Cache();

	cache.put({
		knowledge: { 'asdf:xyz!(:)': [ [ 10, Infinity ] ] },
		indexes: { 'asdf:xyz!(:)': [
			{ id: 1, type: 'asdf', xyz: 10 },
			{ id: 2, type: 'asdf', xyz: 12 }
		] }
	});

	cache.put({
		knowledge: { 'asdf:xyz!(:)': [ [ 5, 10 ] ] },
		indexes: { 'asdf:xyz!(:)': [
			{ id: 5, type: 'asdf', xyz: 5 },
			{ id: 4, type: 'asdf', xyz: 7 },
			{ id: 1, type: 'asdf', xyz: 10 }
		] }
	});

	console.log(cache.indexes.arr);
	t.deepEqual(cache.indexes['asdf:xyz!(:)'].arr, [
		{ id: 5, type: 'asdf', xyz: 5 },
		{ id: 4, type: 'asdf', xyz: 7 },
		{ id: 1, type: 'asdf', xyz: 10 },
		{ id: 2, type: 'asdf', xyz: 12 }
	]);
});

@@ -995,41 +995,8 @@
 );%0A%0A
-%09console.log(cache.indexes.arr);%0A
 %09t.d

Update infibug.test.js