|
1 | 1 | 'use strict'; |
2 | 2 |
|
3 | 3 | const assert = require('node:assert/strict'); |
| 4 | +const fs = require('node:fs'); |
| 5 | +const os = require('node:os'); |
| 6 | +const path = require('node:path'); |
4 | 7 | const sinon = require('sinon'); |
5 | 8 | const rewire = require('rewire'); |
6 | 9 |
|
@@ -35,9 +38,10 @@ describe('storageReclamation module', function () { |
35 | 38 | }); |
36 | 39 |
|
37 | 40 | afterEach(function () { |
38 | | - // Reset the space ratio getter |
| 41 | + // Reset the space ratio getter and quota size |
39 | 42 | if (storageReclamation) { |
40 | 43 | storageReclamation.setAvailableSpaceRatioGetter(null); |
| 44 | + storageReclamation.setQuotaSizeBytes(undefined); |
41 | 45 | } |
42 | 46 |
|
43 | 47 | // Clear any timers |
@@ -396,26 +400,155 @@ describe('storageReclamation module', function () { |
396 | 400 |
|
397 | 401 | describe('quota mode', function () { |
398 | 402 | const QUOTA_100GB = 100 * 1024 * 1024 * 1024; |
| 403 | + let tmpDir; |
| 404 | + let quotaStatusPath; |
| 405 | + |
| 406 | + beforeEach(function () { |
| 407 | + tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'harper-quota-test-')); |
| 408 | + quotaStatusPath = path.join(tmpDir, 'quota-status.json'); |
| 409 | + }); |
| 410 | + |
| 411 | + afterEach(function () { |
| 412 | + try { |
| 413 | + fs.rmSync(tmpDir, { recursive: true }); |
| 414 | + } catch {} |
| 415 | + }); |
399 | 416 |
|
400 | 417 | it('getFreeSpaceBasis returns filesystem when no quota configured', function () { |
401 | 418 | assert.equal(storageReclamation.getFreeSpaceBasis(), 'filesystem'); |
402 | 419 | }); |
403 | 420 |
|
404 | 421 | it('getFreeSpaceBasis returns quota when QUOTA_SIZE_BYTES is set', function () { |
405 | | - storageReclamation.__set__('QUOTA_SIZE_BYTES', QUOTA_100GB); |
| 422 | + storageReclamation.setQuotaSizeBytes(QUOTA_100GB); |
406 | 423 | assert.equal(storageReclamation.getFreeSpaceBasis(), 'quota'); |
407 | | - storageReclamation.__set__('QUOTA_SIZE_BYTES', undefined); |
408 | 424 | }); |
409 | 425 |
|
410 | 426 | it('getQuotaInfo returns undefined when no quota configured', function () { |
411 | 427 | assert.equal(storageReclamation.getQuotaInfo(), undefined); |
412 | 428 | }); |
413 | 429 |
|
414 | 430 | it('getQuotaInfo returns quota size when QUOTA_SIZE_BYTES is set', function () { |
415 | | - storageReclamation.__set__('QUOTA_SIZE_BYTES', QUOTA_100GB); |
416 | | - const info = storageReclamation.getQuotaInfo(); |
417 | | - assert.deepEqual(info, { quotaSizeBytes: QUOTA_100GB }); |
418 | | - storageReclamation.__set__('QUOTA_SIZE_BYTES', undefined); |
| 431 | + storageReclamation.setQuotaSizeBytes(QUOTA_100GB); |
| 432 | + assert.deepEqual(storageReclamation.getQuotaInfo(), { quotaSizeBytes: QUOTA_100GB }); |
| 433 | + }); |
| 434 | + |
| 435 | + describe('getQuotaStatus', function () { |
| 436 | + let originalRootPath; |
| 437 | + |
| 438 | + beforeEach(function () { |
| 439 | + originalRootPath = env.get('rootPath'); |
| 440 | + env.setProperty('rootPath', tmpDir); |
| 441 | + }); |
| 442 | + |
| 443 | + afterEach(function () { |
| 444 | + env.setProperty('rootPath', originalRootPath); |
| 445 | + }); |
| 446 | + |
| 447 | + it('returns parsed object when file is present and valid', async function () { |
| 448 | + const data = { usedBytes: 50_000_000_000, quotaBytes: QUOTA_100GB, updatedAt: Date.now() }; |
| 449 | + fs.writeFileSync(quotaStatusPath, JSON.stringify(data)); |
| 450 | + assert.deepEqual(await storageReclamation.getQuotaStatus(), data); |
| 451 | + }); |
| 452 | + |
| 453 | + it('returns undefined when file is absent', async function () { |
| 454 | + assert.equal(await storageReclamation.getQuotaStatus(), undefined); |
| 455 | + }); |
| 456 | + |
| 457 | + it('returns undefined when file contains malformed JSON', async function () { |
| 458 | + fs.writeFileSync(quotaStatusPath, 'not-valid-json{'); |
| 459 | + assert.equal(await storageReclamation.getQuotaStatus(), undefined); |
| 460 | + }); |
| 461 | + }); |
| 462 | + |
| 463 | + describe('getDirectoryUsageBytes', function () { |
| 464 | + it('returns a non-negative integer for a real directory', async function () { |
| 465 | + const bytes = await storageReclamation.getDirectoryUsageBytes(tmpDir); |
| 466 | + assert.ok(Number.isInteger(bytes)); |
| 467 | + assert.ok(bytes >= 0); |
| 468 | + }); |
| 469 | + }); |
| 470 | + |
| 471 | + describe('defaultGetAvailableSpaceRatio', function () { |
| 472 | + let originalRootPath; |
| 473 | + |
| 474 | + beforeEach(function () { |
| 475 | + originalRootPath = env.get('rootPath'); |
| 476 | + env.setProperty('rootPath', tmpDir); |
| 477 | + storageReclamation.setQuotaSizeBytes(QUOTA_100GB); |
| 478 | + storageReclamation.setAvailableSpaceRatioGetter(undefined); // use real default |
| 479 | + }); |
| 480 | + |
| 481 | + afterEach(function () { |
| 482 | + env.setProperty('rootPath', originalRootPath); |
| 483 | + }); |
| 484 | + |
| 485 | + it('uses fresh quota-status file and triggers reclamation when headroom is low', async function () { |
| 486 | + const usedBytes = 65 * 1024 * 1024 * 1024; // 65 GB → 35% remaining → below 40% threshold |
| 487 | + fs.writeFileSync( |
| 488 | + quotaStatusPath, |
| 489 | + JSON.stringify({ usedBytes, quotaBytes: QUOTA_100GB, updatedAt: Date.now() }) |
| 490 | + ); |
| 491 | + |
| 492 | + const handler = sandbox.stub().returns(Promise.resolve()); |
| 493 | + storageReclamation.onStorageReclamation(tmpDir, handler, true); |
| 494 | + await storageReclamation.runReclamationHandlers(); |
| 495 | + |
| 496 | + assert.ok(handler.calledOnce); |
| 497 | + assert.ok(handler.firstCall.args[0] > 1); // priority = 0.4 / 0.35 ≈ 1.14 |
| 498 | + }); |
| 499 | + |
| 500 | + it('uses fresh quota-status file and does not trigger when headroom is sufficient', async function () { |
| 501 | + const usedBytes = 50 * 1024 * 1024 * 1024; // 50% used → 50% remaining → above threshold |
| 502 | + fs.writeFileSync( |
| 503 | + quotaStatusPath, |
| 504 | + JSON.stringify({ usedBytes, quotaBytes: QUOTA_100GB, updatedAt: Date.now() }) |
| 505 | + ); |
| 506 | + |
| 507 | + const handler = sandbox.stub(); |
| 508 | + storageReclamation.onStorageReclamation(tmpDir, handler, true); |
| 509 | + await storageReclamation.runReclamationHandlers(); |
| 510 | + |
| 511 | + assert.ok(handler.notCalled); |
| 512 | + }); |
| 513 | + |
| 514 | + it('clamps ratio to 0 and triggers reclamation when usage exceeds quota', async function () { |
| 515 | + const usedBytes = 110 * 1024 * 1024 * 1024; // 10 GB over quota |
| 516 | + fs.writeFileSync( |
| 517 | + quotaStatusPath, |
| 518 | + JSON.stringify({ usedBytes, quotaBytes: QUOTA_100GB, updatedAt: Date.now() }) |
| 519 | + ); |
| 520 | + |
| 521 | + const handler = sandbox.stub().returns(Promise.resolve()); |
| 522 | + storageReclamation.onStorageReclamation(tmpDir, handler, true); |
| 523 | + await storageReclamation.runReclamationHandlers(); |
| 524 | + |
| 525 | + // Ratio clamped to 0 → priority = Infinity → handler called |
| 526 | + assert.ok(handler.calledOnce); |
| 527 | + }); |
| 528 | + |
| 529 | + it('falls back to du when quota-status file is absent', async function () { |
| 530 | + // No quota-status.json; du reports actual tmpDir usage which is far below 100 GB |
| 531 | + const handler = sandbox.stub(); |
| 532 | + storageReclamation.onStorageReclamation(tmpDir, handler, true); |
| 533 | + await storageReclamation.runReclamationHandlers(); |
| 534 | + |
| 535 | + assert.ok(handler.notCalled); |
| 536 | + }); |
| 537 | + |
| 538 | + it('falls back to du when quota-status file is stale', async function () { |
| 539 | + const staleTimestamp = Date.now() - 10 * 60 * 1000; // 10 minutes old |
| 540 | + fs.writeFileSync( |
| 541 | + quotaStatusPath, |
| 542 | + JSON.stringify({ usedBytes: 65 * 1024 * 1024 * 1024, quotaBytes: QUOTA_100GB, updatedAt: staleTimestamp }) |
| 543 | + ); |
| 544 | + |
| 545 | + // Despite the stale "65 GB" reading, du reports actual tmpDir usage (far below 100 GB) |
| 546 | + const handler = sandbox.stub(); |
| 547 | + storageReclamation.onStorageReclamation(tmpDir, handler, true); |
| 548 | + await storageReclamation.runReclamationHandlers(); |
| 549 | + |
| 550 | + assert.ok(handler.notCalled); |
| 551 | + }); |
419 | 552 | }); |
420 | 553 |
|
421 | 554 | it('quota-aware ratio triggers reclamation when usage exceeds threshold headroom', async function () { |
|
0 commit comments