function lastRun (cache) { return fs.readFileAsync( path.join(cache, '_lastverified'), 'utf8' ).then(data => new Date(+data)) }
test('writes a file with last verification time', t => { return verify(CACHE).then(() => { return BB.join( verify.lastRun(CACHE), fs.readFileAsync( path.join(CACHE, '_lastverified'), 'utf8' ).then(data => { return new Date(parseInt(data)) }), (fromLastRun, fromFile) => { t.equal(+fromLastRun, +fromFile, 'last verified was writen') } ) }) })
function assertRuleForFiles(files, rules) { return Promise.mapSeries( Object.keys(files).map(file => () => processRulesOnFile(file, rules) .then(res => { expect(res.file).toBeDefined('Result of processing rules does not contain file name'); expect(res.file).toBe(file, 'File name in result of processing rules should be ' + file); expect(res.matches).toBeDefined('Result of processing rules does not contain a "matches" array'); expect(res.matches.length).toBe(files[file].match ? 1 : 0, 'Number of matches for this file should be ' + (files[file].match ? '1' : '0')); if (files[file].match) { expect(res.matches[0]).toBeDefined('As file matches rule, first match should be the current rule (' + file + ')'); if (res.matches[0]) expect(res.matches[0].id).toBe(rules[0].id, 'ID of matched rule does not match rule being tested'); expect(fs.readFileAsync).toHaveBeenCalledWith(file); } return res; }) ), thunk => thunk() ); }
test('removes corrupted index entries from buckets', t => { return mockCache().then(() => { return fs.readFileAsync(BUCKET, 'utf8').then(BUCKETDATA => { // traaaaash return fs.appendFileAsync(BUCKET, '\n234uhhh').then(() => { return verify(CACHE) }).then(stats => { t.equal(stats.missingContent, 0, 'content valid because of good entry') t.equal(stats.totalEntries, 1, 'only one entry counted') return fs.readFileAsync(BUCKET, 'utf8') }).then(bucketData => { const bucketEntry = JSON.parse(bucketData.split('\t')[1]) const targetEntry = JSON.parse(BUCKETDATA.split('\t')[1]) targetEntry.time = bucketEntry.time // different timestamps t.deepEqual( bucketEntry, targetEntry, 'bucket only contains good entry') }) }) }) })
test('removes shadowed index entries from buckets', t => { return mockCache().then(() => { return index.insert(CACHE, KEY, INTEGRITY, { metadata: 'meh' }).then(newEntry => { return verify(CACHE).then(stats => { t.equal(stats.missingContent, 0, 'content valid because of good entry') t.equal(stats.totalEntries, 1, 'only one entry counted') return fs.readFileAsync(BUCKET, 'utf8') }).then(bucketData => { const stringified = JSON.stringify({ key: newEntry.key, integrity: newEntry.integrity.toString(), time: +(bucketData.match(/"time":([0-9]+)/)[1]), metadata: newEntry.metadata }) t.equal( bucketData, `\n${index._hashEntry(stringified)}\t${stringified}`, 'only the most recent entry is still in the bucket' ) }) }) }) })