@julesl23/s5js
Version:
Enhanced TypeScript SDK for S5 decentralized storage with path-based API, media processing, and directory utilities
100 lines • 3.78 kB
JavaScript
import { describe, test, expect, beforeEach } from "vitest";
import { FS5 } from "../../src/fs/fs5.js";
import { JSCryptoImplementation } from "../../src/api/crypto/js.js";
// Mock S5 API interface for testing
class MockS5API {
crypto;
storage = new Map();
registryEntries = new Map();
constructor() {
this.crypto = new JSCryptoImplementation();
}
async uploadBlob(blob) {
const data = new Uint8Array(await blob.arrayBuffer());
const hash = await this.crypto.hashBlake3(data);
const key = Buffer.from(hash).toString('hex');
this.storage.set(key, data);
return { hash: new Uint8Array([0x1e, ...hash]), size: blob.size };
}
async downloadBlobAsBytes(hash) {
// If hash has multihash prefix, remove it
const actualHash = hash[0] === 0x1e ? hash.slice(1) : hash;
const key = Buffer.from(actualHash).toString('hex');
const data = this.storage.get(key);
if (!data)
throw new Error("Blob not found");
return data;
}
async registryGet(publicKey) {
const key = Buffer.from(publicKey).toString('hex');
return this.registryEntries.get(key);
}
async registrySet(entry) {
const key = Buffer.from(entry.pk).toString('hex');
this.registryEntries.set(key, entry);
}
}
// Mock identity for testing
class MockIdentity {
fsRootKey = new Uint8Array(32).fill(1);
}
describe("Path-Based API - Basic Test", () => {
let fs;
let api;
let identity;
beforeEach(async () => {
api = new MockS5API();
identity = new MockIdentity();
fs = new FS5(api, identity);
});
test("should handle basic operations without full S5 setup", async () => {
// First, let's test the existing uploadBlobWithoutEncryption
const testData = new TextEncoder().encode("Hello, world!");
const blob = new Blob([testData]);
const result = await fs.uploadBlobWithoutEncryption(blob);
expect(result.hash).toBeInstanceOf(Uint8Array);
expect(result.size).toBe(testData.length);
// Now test downloading
const downloaded = await api.downloadBlobAsBytes(new Uint8Array([0x1e, ...result.hash]));
expect(downloaded).toEqual(testData);
});
test("should load directory with mocked _loadDirectory", async () => {
// Create a simple directory structure
const testDir = {
magic: "S5.pro",
header: {},
dirs: new Map(),
files: new Map([
["test.txt", {
hash: new Uint8Array(32).fill(2),
size: 100,
media_type: "text/plain"
}]
])
};
// Mock the _loadDirectory method temporarily
const originalLoad = fs._loadDirectory;
fs._loadDirectory = async (path) => {
if (path === "" || path === "home") {
return testDir;
}
return undefined;
};
// Upload some test data first
const testContent = "Test file content";
const testBlob = new Blob([testContent]);
const uploaded = await api.uploadBlob(testBlob);
// Update the test directory with the correct hash (without prefix)
testDir.files.set("test.txt", {
hash: uploaded.hash.slice(1), // Remove multihash prefix
size: uploaded.size,
media_type: "text/plain"
});
// Test the get method
const result = await fs.get("test.txt");
expect(result).toBe(testContent);
// Restore original method
fs._loadDirectory = originalLoad;
});
});
//# sourceMappingURL=path-api-basic.test.js.map