File size: 8,236 Bytes
21dd449 |
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 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 |
import { expect, test, describe, vi, beforeEach } from "vitest";
import type { RepoDesignation, RepoId } from "../types/public";
import { dirname, join } from "node:path";
import { lstat, mkdir, stat, symlink, rename } from "node:fs/promises";
import { pathsInfo } from "./paths-info";
import { createWriteStream, type Stats } from "node:fs";
import { getHFHubCachePath, getRepoFolderName } from "./cache-management";
import { toRepoId } from "../utils/toRepoId";
import { downloadFileToCacheDir } from "./download-file-to-cache-dir";
import { createSymlink } from "../utils/symlink";
vi.mock("node:fs/promises", () => ({
rename: vi.fn(),
symlink: vi.fn(),
lstat: vi.fn(),
mkdir: vi.fn(),
stat: vi.fn(),
}));
vi.mock("node:fs", () => ({
createWriteStream: vi.fn(),
}));
vi.mock("./paths-info", () => ({
pathsInfo: vi.fn(),
}));
vi.mock("../utils/symlink", () => ({
createSymlink: vi.fn(),
}));
const DUMMY_REPO: RepoId = {
name: "hello-world",
type: "model",
};
const DUMMY_ETAG = "dummy-etag";
// utility test method to get blob file path
function _getBlobFile(params: {
repo: RepoDesignation;
etag: string;
cacheDir?: string; // default to {@link getHFHubCache}
}) {
return join(params.cacheDir ?? getHFHubCachePath(), getRepoFolderName(toRepoId(params.repo)), "blobs", params.etag);
}
// utility test method to get snapshot file path
function _getSnapshotFile(params: {
repo: RepoDesignation;
path: string;
revision: string;
cacheDir?: string; // default to {@link getHFHubCache}
}) {
return join(
params.cacheDir ?? getHFHubCachePath(),
getRepoFolderName(toRepoId(params.repo)),
"snapshots",
params.revision,
params.path
);
}
describe("downloadFileToCacheDir", () => {
const fetchMock: typeof fetch = vi.fn();
beforeEach(() => {
vi.resetAllMocks();
// mock 200 request
vi.mocked(fetchMock).mockResolvedValue(
new Response("dummy-body", {
status: 200,
headers: {
etag: DUMMY_ETAG,
"Content-Range": "bytes 0-54/55",
},
})
);
// prevent to use caching
vi.mocked(stat).mockRejectedValue(new Error("Do not exists"));
vi.mocked(lstat).mockRejectedValue(new Error("Do not exists"));
});
test("should throw an error if fileDownloadInfo return nothing", async () => {
await expect(async () => {
await downloadFileToCacheDir({
repo: DUMMY_REPO,
path: "/README.md",
fetch: fetchMock,
});
}).rejects.toThrowError("cannot get path info for /README.md");
expect(pathsInfo).toHaveBeenCalledWith(
expect.objectContaining({
repo: DUMMY_REPO,
paths: ["/README.md"],
fetch: fetchMock,
})
);
});
test("existing symlinked and blob should not re-download it", async () => {
// <cache>/<repo>/<revision>/snapshots/README.md
const expectPointer = _getSnapshotFile({
repo: DUMMY_REPO,
path: "/README.md",
revision: "dd4bc8b21efa05ec961e3efc4ee5e3832a3679c7",
});
// stat ensure a symlink and the pointed file exists
vi.mocked(stat).mockResolvedValue({} as Stats); // prevent default mocked reject
const output = await downloadFileToCacheDir({
repo: DUMMY_REPO,
path: "/README.md",
fetch: fetchMock,
revision: "dd4bc8b21efa05ec961e3efc4ee5e3832a3679c7",
});
expect(stat).toHaveBeenCalledOnce();
// Get call argument for stat
const starArg = vi.mocked(stat).mock.calls[0][0];
expect(starArg).toBe(expectPointer);
expect(fetchMock).not.toHaveBeenCalledWith();
expect(output).toBe(expectPointer);
});
test("existing symlinked and blob with default revision should not re-download it", async () => {
// <cache>/<repo>/<revision>/snapshots/README.md
const expectPointer = _getSnapshotFile({
repo: DUMMY_REPO,
path: "/README.md",
revision: "main",
});
// stat ensure a symlink and the pointed file exists
vi.mocked(stat).mockResolvedValue({} as Stats); // prevent default mocked reject
vi.mocked(lstat).mockResolvedValue({} as Stats);
vi.mocked(pathsInfo).mockResolvedValue([
{
oid: DUMMY_ETAG,
size: 55,
path: "README.md",
type: "file",
lastCommit: {
date: new Date(),
id: "main",
title: "Commit msg",
},
},
]);
const output = await downloadFileToCacheDir({
repo: DUMMY_REPO,
path: "/README.md",
fetch: fetchMock,
});
expect(stat).toHaveBeenCalledOnce();
expect(symlink).not.toHaveBeenCalledOnce();
// Get call argument for stat
const starArg = vi.mocked(stat).mock.calls[0][0];
expect(starArg).toBe(expectPointer);
expect(fetchMock).not.toHaveBeenCalledWith();
expect(output).toBe(expectPointer);
});
test("existing blob should only create the symlink", async () => {
// <cache>/<repo>/<revision>/snapshots/README.md
const expectPointer = _getSnapshotFile({
repo: DUMMY_REPO,
path: "/README.md",
revision: "dummy-commit-hash",
});
// <cache>/<repo>/blobs/<etag>
const expectedBlob = _getBlobFile({
repo: DUMMY_REPO,
etag: DUMMY_ETAG,
});
// mock existing blob only no symlink
vi.mocked(lstat).mockResolvedValue({} as Stats);
// mock pathsInfo resolve content
vi.mocked(pathsInfo).mockResolvedValue([
{
oid: DUMMY_ETAG,
size: 55,
path: "README.md",
type: "file",
lastCommit: {
date: new Date(),
id: "dummy-commit-hash",
title: "Commit msg",
},
},
]);
const output = await downloadFileToCacheDir({
repo: DUMMY_REPO,
path: "/README.md",
fetch: fetchMock,
});
// should have check for the blob
expect(lstat).toHaveBeenCalled();
expect(vi.mocked(lstat).mock.calls[0][0]).toBe(expectedBlob);
// symlink should have been created
expect(createSymlink).toHaveBeenCalledOnce();
// no download done
expect(fetchMock).not.toHaveBeenCalled();
expect(output).toBe(expectPointer);
});
test("expect resolve value to be the pointer path of downloaded file", async () => {
// <cache>/<repo>/<revision>/snapshots/README.md
const expectPointer = _getSnapshotFile({
repo: DUMMY_REPO,
path: "/README.md",
revision: "dummy-commit-hash",
});
// <cache>/<repo>/blobs/<etag>
const expectedBlob = _getBlobFile({
repo: DUMMY_REPO,
etag: DUMMY_ETAG,
});
vi.mocked(pathsInfo).mockResolvedValue([
{
oid: DUMMY_ETAG,
size: 55,
path: "README.md",
type: "file",
lastCommit: {
date: new Date(),
id: "dummy-commit-hash",
title: "Commit msg",
},
},
]);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
vi.mocked(createWriteStream).mockReturnValue(async function* () {} as any);
const output = await downloadFileToCacheDir({
repo: DUMMY_REPO,
path: "/README.md",
fetch: fetchMock,
});
// expect blobs and snapshots folder to have been mkdir
expect(vi.mocked(mkdir).mock.calls[0][0]).toBe(dirname(expectedBlob));
expect(vi.mocked(mkdir).mock.calls[1][0]).toBe(dirname(expectPointer));
expect(output).toBe(expectPointer);
});
test("should write fetch response to blob", async () => {
// <cache>/<repo>/<revision>/snapshots/README.md
const expectPointer = _getSnapshotFile({
repo: DUMMY_REPO,
path: "/README.md",
revision: "dummy-commit-hash",
});
// <cache>/<repo>/blobs/<etag>
const expectedBlob = _getBlobFile({
repo: DUMMY_REPO,
etag: DUMMY_ETAG,
});
// mock pathsInfo resolve content
vi.mocked(pathsInfo).mockResolvedValue([
{
oid: DUMMY_ETAG,
size: 55,
path: "README.md",
type: "file",
lastCommit: {
date: new Date(),
id: "dummy-commit-hash",
title: "Commit msg",
},
},
]);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
vi.mocked(createWriteStream).mockReturnValue(async function* () {} as any);
await downloadFileToCacheDir({
repo: DUMMY_REPO,
path: "/README.md",
fetch: fetchMock,
});
const incomplete = `${expectedBlob}.incomplete`;
// 1. should write fetch#response#body to incomplete file
expect(createWriteStream).toHaveBeenCalledWith(incomplete);
// 2. should rename the incomplete to the blob expected name
expect(rename).toHaveBeenCalledWith(incomplete, expectedBlob);
// 3. should create symlink pointing to blob
expect(createSymlink).toHaveBeenCalledWith({ sourcePath: expectedBlob, finalPath: expectPointer });
});
});
|