aboutsummaryrefslogtreecommitdiffstats
path: root/packages/e2e_tests/tests/assetdb/local-filesystem-store.test.ts
blob: 36ff837fab5b98419e360211178605cd1f243dba (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import * as fs from "fs";
import * as path from "path";
import { afterEach, beforeEach, describe, expect, it } from "vitest";

import { LocalFileSystemAssetStore } from "@karakeep/shared/assetdb";

import {
  assertAssetNotExists,
  cleanupTempDirectory,
  createLocalFileSystemStore,
  createTempDirectory,
  createTestAssetData,
} from "./assetdb-utils";

describe("LocalFileSystemAssetStore - Filesystem-Specific Behaviors", () => {
  let tempDir: string;
  let store: LocalFileSystemAssetStore;

  beforeEach(async () => {
    tempDir = await createTempDirectory();
    store = createLocalFileSystemStore(tempDir);
  });

  afterEach(async () => {
    await cleanupTempDirectory(tempDir);
  });

  describe("File System Structure", () => {
    it("should create correct directory structure and files", async () => {
      const testData = createTestAssetData();

      await store.saveAsset({
        userId: testData.userId,
        assetId: testData.assetId,
        asset: testData.content,
        metadata: testData.metadata,
      });

      // Verify directory structure
      const assetDir = path.join(tempDir, testData.userId, testData.assetId);
      expect(
        await fs.promises
          .access(assetDir)
          .then(() => true)
          .catch(() => false),
      ).toBe(true);

      // Verify asset.bin file
      const assetFile = path.join(assetDir, "asset.bin");
      expect(
        await fs.promises
          .access(assetFile)
          .then(() => true)
          .catch(() => false),
      ).toBe(true);

      // Verify metadata.json file
      const metadataFile = path.join(assetDir, "metadata.json");
      expect(
        await fs.promises
          .access(metadataFile)
          .then(() => true)
          .catch(() => false),
      ).toBe(true);

      // Verify file contents
      const savedContent = await fs.promises.readFile(assetFile);
      expect(savedContent).toEqual(testData.content);

      const savedMetadata = JSON.parse(
        await fs.promises.readFile(metadataFile, "utf8"),
      );
      expect(savedMetadata).toEqual(testData.metadata);
    });

    it("should create nested directory structure for user/asset hierarchy", async () => {
      const userId = "user123";
      const assetId = "asset456";
      const testData = createTestAssetData({ userId, assetId });

      await store.saveAsset({
        userId: testData.userId,
        assetId: testData.assetId,
        asset: testData.content,
        metadata: testData.metadata,
      });

      // Verify the exact directory structure
      const userDir = path.join(tempDir, userId);
      const assetDir = path.join(userDir, assetId);

      expect(
        await fs.promises
          .access(userDir)
          .then(() => true)
          .catch(() => false),
      ).toBe(true);

      expect(
        await fs.promises
          .access(assetDir)
          .then(() => true)
          .catch(() => false),
      ).toBe(true);

      // Verify files exist in the correct location
      expect(
        await fs.promises
          .access(path.join(assetDir, "asset.bin"))
          .then(() => true)
          .catch(() => false),
      ).toBe(true);

      expect(
        await fs.promises
          .access(path.join(assetDir, "metadata.json"))
          .then(() => true)
          .catch(() => false),
      ).toBe(true);
    });
  });

  describe("Directory Cleanup", () => {
    it("should remove entire asset directory when deleting asset", async () => {
      const testData = createTestAssetData();

      await store.saveAsset({
        userId: testData.userId,
        assetId: testData.assetId,
        asset: testData.content,
        metadata: testData.metadata,
      });

      const assetDir = path.join(tempDir, testData.userId, testData.assetId);

      // Verify directory exists
      expect(
        await fs.promises
          .access(assetDir)
          .then(() => true)
          .catch(() => false),
      ).toBe(true);

      await store.deleteAsset({
        userId: testData.userId,
        assetId: testData.assetId,
      });

      // Verify entire directory was removed
      expect(
        await fs.promises
          .access(assetDir)
          .then(() => true)
          .catch(() => false),
      ).toBe(false);

      await assertAssetNotExists(store, testData.userId, testData.assetId);
    });

    it("should remove entire user directory when deleting all user assets", async () => {
      const userId = "test-user";
      const testData1 = createTestAssetData({ userId });
      const testData2 = createTestAssetData({ userId });

      await Promise.all([
        store.saveAsset({
          userId: testData1.userId,
          assetId: testData1.assetId,
          asset: testData1.content,
          metadata: testData1.metadata,
        }),
        store.saveAsset({
          userId: testData2.userId,
          assetId: testData2.assetId,
          asset: testData2.content,
          metadata: testData2.metadata,
        }),
      ]);

      const userDir = path.join(tempDir, userId);

      // Verify user directory exists
      expect(
        await fs.promises
          .access(userDir)
          .then(() => true)
          .catch(() => false),
      ).toBe(true);

      await store.deleteUserAssets({ userId });

      // Verify entire user directory was removed
      expect(
        await fs.promises
          .access(userDir)
          .then(() => true)
          .catch(() => false),
      ).toBe(false);
    });
  });

  describe("File System Permissions", () => {
    it("should create directories with appropriate permissions", async () => {
      const testData = createTestAssetData();

      await store.saveAsset({
        userId: testData.userId,
        assetId: testData.assetId,
        asset: testData.content,
        metadata: testData.metadata,
      });

      const userDir = path.join(tempDir, testData.userId);
      const assetDir = path.join(userDir, testData.assetId);

      // Verify directories are readable and writable
      const userStats = await fs.promises.stat(userDir);
      const assetStats = await fs.promises.stat(assetDir);

      expect(userStats.isDirectory()).toBe(true);
      expect(assetStats.isDirectory()).toBe(true);

      // Verify we can read and write to the directories
      await fs.promises.access(userDir, fs.constants.R_OK | fs.constants.W_OK);
      await fs.promises.access(assetDir, fs.constants.R_OK | fs.constants.W_OK);
    });
  });
});