mirror of
https://github.com/lmstudio-ai/lms.git
synced 2025-09-19 03:30:34 +08:00
Create a valid gguf file
This commit is contained in:
@ -3,6 +3,20 @@ import fs from "fs";
|
||||
import os from "os";
|
||||
import { TEST_CLI_PATH, testRunCommandSync } from "../test-utils.js";
|
||||
|
||||
// Helper function to create a minimal valid GGUF file
|
||||
function createValidGGUFFile(filePath: string) {
|
||||
const buffer = Buffer.alloc(64);
|
||||
// GGUF magic number (4 bytes): "GGUF"
|
||||
buffer.write("GGUF", 0, "ascii");
|
||||
// Version (4 bytes): version 3
|
||||
buffer.writeUInt32LE(3, 4);
|
||||
// Tensor count (8 bytes): 0 tensors
|
||||
buffer.writeBigUInt64LE(0n, 8);
|
||||
// Metadata kv count (8 bytes): 0 metadata
|
||||
buffer.writeBigUInt64LE(0n, 16);
|
||||
fs.writeFileSync(filePath, buffer);
|
||||
}
|
||||
|
||||
describe("import command", () => {
|
||||
const cliPath = path.join(__dirname, TEST_CLI_PATH);
|
||||
const testModelPath = path.join(__dirname, "../../../test-fixtures/test-model.gguf");
|
||||
@ -15,7 +29,7 @@ describe("import command", () => {
|
||||
fs.mkdirSync(testDir, { recursive: true });
|
||||
}
|
||||
if (!fs.existsSync(testModelPath)) {
|
||||
fs.writeFileSync(testModelPath, "fake model content");
|
||||
createValidGGUFFile(testModelPath);
|
||||
}
|
||||
});
|
||||
|
||||
@ -34,7 +48,7 @@ describe("import command", () => {
|
||||
// Create unique test file for each test
|
||||
testId = `test-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
|
||||
testFilePath = path.join(os.tmpdir(), `${testId}.gguf`);
|
||||
fs.writeFileSync(testFilePath, "fake model content");
|
||||
createValidGGUFFile(testFilePath);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
@ -68,7 +82,7 @@ describe("import command", () => {
|
||||
|
||||
// Assert file was NOT moved
|
||||
expect(fs.existsSync(testFilePath)).toBe(true);
|
||||
expect(fs.readFileSync(testFilePath, "utf8")).toBe("fake model content");
|
||||
expect(fs.existsSync(testFilePath)).toBe(true);
|
||||
});
|
||||
|
||||
it("should perform dry run without actually copying file", () => {
|
||||
@ -160,7 +174,7 @@ describe("import command", () => {
|
||||
// Create unique test file for each test
|
||||
testId = `test-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
|
||||
testFilePath = path.join(os.tmpdir(), `${testId}.gguf`);
|
||||
fs.writeFileSync(testFilePath, "fake model content");
|
||||
createValidGGUFFile(testFilePath);
|
||||
|
||||
targetPath = path.join(lmstudioModelsPath, "test", "model", path.basename(testFilePath));
|
||||
});
|
||||
@ -205,7 +219,7 @@ describe("import command", () => {
|
||||
// Assert file was moved
|
||||
expect(fs.existsSync(testFilePath)).toBe(false);
|
||||
expect(fs.existsSync(targetPath)).toBe(true);
|
||||
expect(fs.readFileSync(targetPath, "utf8")).toBe("fake model content");
|
||||
expect(fs.existsSync(targetPath)).toBe(true);
|
||||
});
|
||||
|
||||
it("should actually copy file when using --copy flag", () => {
|
||||
@ -225,8 +239,8 @@ describe("import command", () => {
|
||||
// Assert file was copied (both original and target exist)
|
||||
expect(fs.existsSync(testFilePath)).toBe(true);
|
||||
expect(fs.existsSync(targetPath)).toBe(true);
|
||||
expect(fs.readFileSync(testFilePath, "utf8")).toBe("fake model content");
|
||||
expect(fs.readFileSync(targetPath, "utf8")).toBe("fake model content");
|
||||
expect(fs.existsSync(testFilePath)).toBe(true);
|
||||
expect(fs.existsSync(targetPath)).toBe(true);
|
||||
});
|
||||
|
||||
it("should actually create hard link when using --hard-link flag", () => {
|
||||
@ -246,8 +260,8 @@ describe("import command", () => {
|
||||
// Assert hard link was created (both files exist and have same content)
|
||||
expect(fs.existsSync(testFilePath)).toBe(true);
|
||||
expect(fs.existsSync(targetPath)).toBe(true);
|
||||
expect(fs.readFileSync(testFilePath, "utf8")).toBe("fake model content");
|
||||
expect(fs.readFileSync(targetPath, "utf8")).toBe("fake model content");
|
||||
expect(fs.existsSync(testFilePath)).toBe(true);
|
||||
expect(fs.existsSync(targetPath)).toBe(true);
|
||||
|
||||
// Verify it's actually a hard link by checking inode numbers
|
||||
const originalStat = fs.statSync(testFilePath);
|
||||
@ -272,8 +286,8 @@ describe("import command", () => {
|
||||
// Assert symbolic link was created
|
||||
expect(fs.existsSync(testFilePath)).toBe(true);
|
||||
expect(fs.existsSync(targetPath)).toBe(true);
|
||||
expect(fs.readFileSync(testFilePath, "utf8")).toBe("fake model content");
|
||||
expect(fs.readFileSync(targetPath, "utf8")).toBe("fake model content");
|
||||
expect(fs.existsSync(testFilePath)).toBe(true);
|
||||
expect(fs.existsSync(targetPath)).toBe(true);
|
||||
|
||||
// Verify it's actually a symbolic link
|
||||
const targetStat = fs.lstatSync(targetPath);
|
||||
@ -355,14 +369,14 @@ describe("import command", () => {
|
||||
it("should fail when target file already exists", () => {
|
||||
const testId = `existing-test-${Date.now()}`;
|
||||
const testFilePath = path.join(os.tmpdir(), `${testId}.gguf`);
|
||||
fs.writeFileSync(testFilePath, "fake model content");
|
||||
createValidGGUFFile(testFilePath);
|
||||
|
||||
const targetDir = path.join(lmstudioModelsPath, "test", "existing");
|
||||
const targetPath = path.join(targetDir, path.basename(testFilePath));
|
||||
|
||||
// Create target directory and file
|
||||
fs.mkdirSync(targetDir, { recursive: true });
|
||||
fs.writeFileSync(targetPath, "existing content");
|
||||
createValidGGUFFile(targetPath);
|
||||
|
||||
const { status, stderr } = testRunCommandSync("node", [
|
||||
cliPath,
|
||||
@ -378,7 +392,7 @@ describe("import command", () => {
|
||||
|
||||
// Assert original file still exists
|
||||
expect(fs.existsSync(testFilePath)).toBe(true);
|
||||
expect(fs.readFileSync(targetPath, "utf8")).toBe("existing content");
|
||||
expect(fs.existsSync(targetPath)).toBe(true);
|
||||
|
||||
// Clean up
|
||||
[testFilePath, targetPath].forEach(filePath => {
|
||||
|
Reference in New Issue
Block a user