|
1 | 1 | import { ConsoleLoggerAdapter, HmacSha256Signer } from "@milaboratories/ts-helpers"; |
2 | | -import { LsDriver } from "./ls"; |
| 2 | +import { LsDriver, type LsEntryWithFileStats } from "./ls"; |
3 | 3 | import { TestHelpers } from "@milaboratories/pl-client"; |
4 | 4 | import * as path from "node:path"; |
5 | | -import { test, expect } from "vitest"; |
| 5 | +import { test, expect, describe } from "vitest"; |
6 | 6 | import { isImportFileHandleIndex, isImportFileHandleUpload } from "@milaboratories/pl-model-common"; |
| 7 | +import type { StorageHandle } from "@milaboratories/pl-model-common"; |
7 | 8 | import * as env from "../test_env"; |
| 9 | +import { parseIndexHandle } from "./helpers/ls_remote_import_handle"; |
| 10 | +import { createRemoteStorageHandle } from "./helpers/ls_storage_entry"; |
8 | 11 |
|
9 | 12 | const assetsPath = path.resolve("../../../assets"); |
10 | 13 |
|
@@ -141,3 +144,135 @@ test("should ok when get file using local dialog, and read its content", async ( |
141 | 144 | expect(multiResult.files![0]).toStrictEqual(result.file); |
142 | 145 | }); |
143 | 146 | }); |
| 147 | + |
| 148 | +// Unit tests: verify that LsDriver.listFiles and listRemoteFilesWithFileStats correctly |
| 149 | +// thread additionalInfo from gRPC list items into the index:// handle. |
| 150 | +describe("LsDriver additionalInfo threading", () => { |
| 151 | + const envelope = { uid: "u1", sid: "s1", sig: "sigval", exp: "9999999999", kid: "k1", v: "1" }; |
| 152 | + |
| 153 | + const storageInfo = { |
| 154 | + storageId: "test-storage", |
| 155 | + storageName: "Test Storage", |
| 156 | + resourceId: "res-id" as any, |
| 157 | + resourceType: { name: "LS/test-storage", version: "1" }, |
| 158 | + }; |
| 159 | + |
| 160 | + // Builds a minimal LsDriver instance with an injected mock lsClient via private-constructor bypass. |
| 161 | + function makeMockDriver(listResponse: { items: any[]; delimiter: string }): LsDriver { |
| 162 | + const mockLsClient = { |
| 163 | + list: async () => listResponse, |
| 164 | + close: () => {}, |
| 165 | + }; |
| 166 | + const mockUserResources = { |
| 167 | + getDataLibraries: async () => new Map([[storageInfo.storageId, storageInfo]]), |
| 168 | + }; |
| 169 | + const signer = new HmacSha256Signer("test"); |
| 170 | + // Bypass private constructor for unit testing only. |
| 171 | + return new (LsDriver as any)( |
| 172 | + new ConsoleLoggerAdapter(), |
| 173 | + mockLsClient, |
| 174 | + mockUserResources, |
| 175 | + signer, |
| 176 | + new Map(), |
| 177 | + new Map(), |
| 178 | + () => Promise.resolve(undefined), |
| 179 | + ) as LsDriver; |
| 180 | + } |
| 181 | + |
| 182 | + function makeRemoteHandle(): StorageHandle { |
| 183 | + return createRemoteStorageHandle(storageInfo) as StorageHandle; |
| 184 | + } |
| 185 | + |
| 186 | + test("listFiles: handle carries additionalInfo envelope from gRPC item", async () => { |
| 187 | + const driver = makeMockDriver({ |
| 188 | + delimiter: "/", |
| 189 | + items: [ |
| 190 | + { |
| 191 | + name: "file.txt", |
| 192 | + size: 100n, |
| 193 | + isDir: false, |
| 194 | + additionalInfo: envelope, |
| 195 | + fullName: "dir/file.txt", |
| 196 | + directory: "dir/", |
| 197 | + version: "v1", |
| 198 | + }, |
| 199 | + ], |
| 200 | + }); |
| 201 | + |
| 202 | + const result = await driver.listFiles(makeRemoteHandle(), "dir/"); |
| 203 | + expect(result.entries).toHaveLength(1); |
| 204 | + |
| 205 | + const parsed = parseIndexHandle(result.entries[0].handle as any); |
| 206 | + expect(parsed.additionalInfo).toEqual(envelope); |
| 207 | + }); |
| 208 | + |
| 209 | + test("listFiles: handle has no additionalInfo when item has empty envelope", async () => { |
| 210 | + const driver = makeMockDriver({ |
| 211 | + delimiter: "/", |
| 212 | + items: [ |
| 213 | + { |
| 214 | + name: "plain.txt", |
| 215 | + size: 50n, |
| 216 | + isDir: false, |
| 217 | + additionalInfo: {}, |
| 218 | + fullName: "plain.txt", |
| 219 | + directory: "", |
| 220 | + version: "v1", |
| 221 | + }, |
| 222 | + ], |
| 223 | + }); |
| 224 | + |
| 225 | + const result = await driver.listFiles(makeRemoteHandle(), ""); |
| 226 | + expect(result.entries).toHaveLength(1); |
| 227 | + |
| 228 | + const parsed = parseIndexHandle(result.entries[0].handle as any); |
| 229 | + expect(parsed.additionalInfo).toBeUndefined(); |
| 230 | + }); |
| 231 | + |
| 232 | + test("listRemoteFilesWithFileStats: handle carries additionalInfo envelope", async () => { |
| 233 | + const driver = makeMockDriver({ |
| 234 | + delimiter: "/", |
| 235 | + items: [ |
| 236 | + { |
| 237 | + name: "data.csv", |
| 238 | + size: 200n, |
| 239 | + isDir: false, |
| 240 | + additionalInfo: envelope, |
| 241 | + fullName: "data.csv", |
| 242 | + directory: "", |
| 243 | + version: "v2", |
| 244 | + }, |
| 245 | + ], |
| 246 | + }); |
| 247 | + |
| 248 | + const result = await driver.listRemoteFilesWithFileStats(makeRemoteHandle(), ""); |
| 249 | + expect(result.entries).toHaveLength(1); |
| 250 | + expect((result.entries[0] as LsEntryWithFileStats).size).toBe(200); |
| 251 | + |
| 252 | + const parsed = parseIndexHandle(result.entries[0].handle as any); |
| 253 | + expect(parsed.additionalInfo).toEqual(envelope); |
| 254 | + }); |
| 255 | + |
| 256 | + test("listRemoteFilesWithFileStats: no additionalInfo when absent in item", async () => { |
| 257 | + const driver = makeMockDriver({ |
| 258 | + delimiter: "/", |
| 259 | + items: [ |
| 260 | + { |
| 261 | + name: "plain.csv", |
| 262 | + size: 10n, |
| 263 | + isDir: false, |
| 264 | + additionalInfo: {}, |
| 265 | + fullName: "plain.csv", |
| 266 | + directory: "", |
| 267 | + version: "v1", |
| 268 | + }, |
| 269 | + ], |
| 270 | + }); |
| 271 | + |
| 272 | + const result = await driver.listRemoteFilesWithFileStats(makeRemoteHandle(), ""); |
| 273 | + expect(result.entries).toHaveLength(1); |
| 274 | + |
| 275 | + const parsed = parseIndexHandle(result.entries[0].handle as any); |
| 276 | + expect(parsed.additionalInfo).toBeUndefined(); |
| 277 | + }); |
| 278 | +}); |
0 commit comments