From ef5707e6342e90fe50e70134237d301840d94a89 Mon Sep 17 00:00:00 2001
From: Benoit Lavenier <benoit.lavenier@e-is.pro>
Date: Fri, 9 Jun 2023 10:12:58 +0200
Subject: [PATCH] fix(1447): Add a unit test to reproduce #1447 (Sindex not
 well trimmed)

---
 app/lib/dal/indexDAL/leveldb/LevelDBSindex.ts |  7 +++++++
 test/dal/triming-dal.ts                       | 14 ++++++++++++++
 2 files changed, 21 insertions(+)

diff --git a/app/lib/dal/indexDAL/leveldb/LevelDBSindex.ts b/app/lib/dal/indexDAL/leveldb/LevelDBSindex.ts
index c19ae8883..bdb046713 100644
--- a/app/lib/dal/indexDAL/leveldb/LevelDBSindex.ts
+++ b/app/lib/dal/indexDAL/leveldb/LevelDBSindex.ts
@@ -337,6 +337,13 @@ export class LevelDBSindex extends LevelDBTable<SindexEntry>
     }
   }
 
+  /**
+   * Allow to get internal index, for unit test (see test triming-dal.ts)
+   */
+  getInternalIndexes(): LevelDBTable<any>[] {
+    return [this.indexForTrimming, this.indexForConditions, this.indexForConditions, this.indexOfComplexeConditionForPubkeys];
+  }
+
   private async trimConditions(condition: string, id: string) {
     // Get all the condition's sources
     const existing = (await this.indexForConditions.getOrNull(condition)) || [];
diff --git a/test/dal/triming-dal.ts b/test/dal/triming-dal.ts
index be921161c..2de28ad0e 100644
--- a/test/dal/triming-dal.ts
+++ b/test/dal/triming-dal.ts
@@ -15,6 +15,7 @@ import {FileDAL} from "../../app/lib/dal/fileDAL"
 import {Directory} from "../../app/lib/system/directory"
 import {Indexer} from "../../app/lib/indexer"
 import {simpleNodeWith2Users} from "../integration/tools/toolbox"
+import {LevelDBSindex} from "../../app/lib/dal/indexDAL/leveldb/LevelDBSindex";
 
 const should = require('should');
 
@@ -135,6 +136,19 @@ describe("Triming", function(){
     (await dal.sindexDAL.getAvailableForConditions('COND(SOURCE_1)')).should.have.length(0);
     (await dal.sindexDAL.getAvailableForPubkey('PUB_1')).should.have.length(1);
     (await dal.sindexDAL.findByPos(4)).should.have.length(2);
+
+    // Consumed all, then trim again. => All sub indices should be empty
+    await dal.sindexDAL.insertBatch([
+      { op: 'UPDATE', identifier: 'SOURCE_2', pos: 4, written_on: '140-H', writtenOn: 140, written_time: 5000, consumed: true, conditions: 'COND(SOURCE_2)'},
+      { op: 'UPDATE', identifier: 'SOURCE_3', pos: 4, written_on: '140-H', writtenOn: 140, written_time: 5000, consumed: true, conditions: 'SIG(PUB_1)'}
+    ] as any);
+    await dal.trimIndexes(141);
+    if (dal.sindexDAL instanceof LevelDBSindex) {
+      for (let index of dal.sindexDAL.getInternalIndexes()) {
+        const res = await index.findAllKeys();
+        res.should.have.length(0, `index ${index['name']} should have been trimmed`);
+      }
+    }
   })
 
   it('should be able to trim the bindex', async () => {
-- 
GitLab