mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
* Convert runMany and runManySync to use injectManyWithMeta Signed-off-by: Sebastian Malton <sebastian@malton.name> * Fixup type errors due to new Runnable requirements Signed-off-by: Sebastian Malton <sebastian@malton.name> * Add documentation for verifyRunnablesAreDAG Signed-off-by: Sebastian Malton <sebastian@malton.name> * Simplify convertToWithIdWith Signed-off-by: Sebastian Malton <sebastian@malton.name> * Move all utility functions to separate package Signed-off-by: Sebastian Malton <sebastian@malton.name> * Move testing utilities to separate package Signed-off-by: Sebastian Malton <sebastian@malton.name> * Move run-many and run-many-sync to separate package Signed-off-by: Sebastian Malton <sebastian@malton.name> * Replace all internal uses of utilities with new packages Signed-off-by: Sebastian Malton <sebastian@malton.name> * Use new @k8slens/run-many package in core Signed-off-by: Sebastian Malton <sebastian@malton.name> * Add dep to open-lens Signed-off-by: Sebastian Malton <sebastian@malton.name> * Fixup type errors Signed-off-by: Sebastian Malton <sebastian@malton.name> * Fixup uses of @k8slens/test-utils Signed-off-by: Sebastian Malton <sebastian@malton.name> * Fixup getGlobalOverride Signed-off-by: Sebastian Malton <sebastian@malton.name> * Move tests to new package too Signed-off-by: Sebastian Malton <sebastian@malton.name> * Fix type errors Signed-off-by: Sebastian Malton <sebastian@malton.name> * Fixup uses of AsyncResult and autoBind Signed-off-by: Sebastian Malton <sebastian@malton.name> * Fixup remaining import issues Signed-off-by: Sebastian Malton <sebastian@malton.name> * Finial fixups to fix build Signed-off-by: Sebastian Malton <sebastian@malton.name> * Fix lint Signed-off-by: Sebastian Malton <sebastian@malton.name> * Revert moving "testUsingFakeTime" to separate package - This fixes tests Signed-off-by: Sebastian Malton <sebastian@malton.name> * Fix integration tests Signed-off-by: Sebastian Malton <sebastian@malton.name> * Fix unit test failing due to spelling fix Signed-off-by: Sebastian Malton <sebastian@malton.name> --------- Signed-off-by: Sebastian Malton <sebastian@malton.name>
129 lines
4.7 KiB
TypeScript
129 lines
4.7 KiB
TypeScript
/**
|
|
* Copyright (c) OpenLens Authors. All rights reserved.
|
|
* Licensed under MIT License. See LICENSE in root directory for more information.
|
|
*/
|
|
|
|
import { bytesToUnits, unitsToBytes } from "./convertMemory";
|
|
|
|
describe("unitsToBytes", () => {
|
|
it("without any units, just parse as float", () => {
|
|
expect(unitsToBytes("1234")).toBe(1234);
|
|
});
|
|
|
|
it("should parse with B suffix", () => {
|
|
expect(unitsToBytes("1234B")).toBe(1234);
|
|
});
|
|
|
|
it("should parse with Ki suffix", () => {
|
|
expect(unitsToBytes("1234Ki")).toBe(1234 * (1024));
|
|
});
|
|
|
|
it("should parse with Ki suffix as the same as KiB", () => {
|
|
expect(unitsToBytes("1234Ki")).toBe(unitsToBytes("1234KiB"));
|
|
});
|
|
|
|
it("should parse with Mi suffix", () => {
|
|
expect(unitsToBytes("1234Mi")).toBe(1234 * (1024 ** 2));
|
|
});
|
|
|
|
it("should parse with Mi suffix as the same as MiB", () => {
|
|
expect(unitsToBytes("1234Mi")).toBe(unitsToBytes("1234MiB"));
|
|
});
|
|
|
|
it("should parse with Gi suffix", () => {
|
|
expect(unitsToBytes("1234Gi")).toBe(1234 * (1024 ** 3));
|
|
});
|
|
|
|
it("should parse with Gi suffix as the same as GiB", () => {
|
|
expect(unitsToBytes("1234Gi")).toBe(unitsToBytes("1234GiB"));
|
|
});
|
|
|
|
it("should parse with Ti suffix", () => {
|
|
expect(unitsToBytes("1234Ti")).toBe(1234 * (1024 ** 4));
|
|
});
|
|
|
|
it("should parse with Ti suffix as the same as TiB", () => {
|
|
expect(unitsToBytes("1234Ti")).toBe(unitsToBytes("1234TiB"));
|
|
});
|
|
|
|
it("should parse with Pi suffix", () => {
|
|
expect(unitsToBytes("1234Pi")).toBe(1234 * (1024 ** 5));
|
|
});
|
|
|
|
it("should parse with Pi suffix as the same as PiB", () => {
|
|
expect(unitsToBytes("1234Pi")).toBe(unitsToBytes("1234PiB"));
|
|
});
|
|
|
|
it("given unrelated data, return NaN", () => {
|
|
expect(unitsToBytes("I am not a number")).toBeNaN();
|
|
});
|
|
|
|
it("given unrelated data, but has number, return that", () => {
|
|
expect(unitsToBytes("I am not a number, but this is 0.1")).toBe(0.1);
|
|
});
|
|
});
|
|
|
|
describe("bytesToUnits", () => {
|
|
it("should return N/A for invalid bytes", () => {
|
|
expect(bytesToUnits(-1)).toBe("N/A");
|
|
expect(bytesToUnits(Infinity)).toBe("N/A");
|
|
expect(bytesToUnits(NaN)).toBe("N/A");
|
|
});
|
|
|
|
it("given a number within the magnitude of 0..124, format with B", () => {
|
|
expect(bytesToUnits(100)).toBe("100.0B");
|
|
});
|
|
|
|
it("given a number within the magnitude of 1024..1024^2, format with KiB", () => {
|
|
expect(bytesToUnits(1024)).toBe("1.0KiB");
|
|
expect(bytesToUnits(2048)).toBe("2.0KiB");
|
|
expect(bytesToUnits(1900)).toBe("1.9KiB");
|
|
expect(bytesToUnits(50*1024 + 1)).toBe("50.0KiB");
|
|
});
|
|
|
|
it("given a number within the magnitude of 1024^2..1024^3, format with MiB", () => {
|
|
expect(bytesToUnits(1024**2)).toBe("1.0MiB");
|
|
expect(bytesToUnits(2048**2)).toBe("4.0MiB");
|
|
expect(bytesToUnits(1900 * 1024)).toBe("1.9MiB");
|
|
expect(bytesToUnits(50*(1024 ** 2) + 1)).toBe("50.0MiB");
|
|
});
|
|
|
|
it("given a number within the magnitude of 1024^3..1024^4, format with GiB", () => {
|
|
expect(bytesToUnits(1024**3)).toBe("1.0GiB");
|
|
expect(bytesToUnits(2048**3)).toBe("8.0GiB");
|
|
expect(bytesToUnits(1900 * 1024 ** 2)).toBe("1.9GiB");
|
|
expect(bytesToUnits(50*(1024 ** 3) + 1)).toBe("50.0GiB");
|
|
});
|
|
|
|
it("given a number within the magnitude of 1024^4..1024^5, format with TiB", () => {
|
|
expect(bytesToUnits(1024**4)).toBe("1.0TiB");
|
|
expect(bytesToUnits(2048**4)).toBe("16.0TiB");
|
|
expect(bytesToUnits(1900 * 1024 ** 3)).toBe("1.9TiB");
|
|
expect(bytesToUnits(50*(1024 ** 4) + 1)).toBe("50.0TiB");
|
|
});
|
|
|
|
it("given a number within the magnitude of 1024^5..1024^6, format with PiB", () => {
|
|
expect(bytesToUnits(1024**5)).toBe("1.0PiB");
|
|
expect(bytesToUnits(2048**5)).toBe("32.0PiB");
|
|
expect(bytesToUnits(1900 * 1024 ** 4)).toBe("1.9PiB");
|
|
expect(bytesToUnits(50*(1024 ** 5) + 1)).toBe("50.0PiB");
|
|
});
|
|
});
|
|
|
|
describe("bytesToUnits -> unitsToBytes", () => {
|
|
it("given an input, round trip to the same value, given enough precision", () => {
|
|
expect(unitsToBytes(bytesToUnits(123))).toBe(123);
|
|
expect(unitsToBytes(bytesToUnits(1024**0 + 1, { precision: 2 }))).toBe(1024**0 + 1);
|
|
expect(unitsToBytes(bytesToUnits(1024**1 + 2, { precision: 3 }))).toBe(1024**1 + 2);
|
|
expect(unitsToBytes(bytesToUnits(1024**2 + 3, { precision: 6 }))).toBe(1024**2 + 3);
|
|
expect(unitsToBytes(bytesToUnits(1024**3 + 4, { precision: 9 }))).toBe(1024**3 + 4);
|
|
expect(unitsToBytes(bytesToUnits(1024**4 + 5, { precision: 12 }))).toBe(1024**4 + 5);
|
|
expect(unitsToBytes(bytesToUnits(1024**5 + 6, { precision: 16 }))).toBe(1024**5 + 6);
|
|
expect(unitsToBytes(bytesToUnits(1024**6 + 7, { precision: 20 }))).toBe(1024**6 + 7);
|
|
});
|
|
|
|
it("given an invalid input, round trips to NaN", () => {
|
|
expect(unitsToBytes(bytesToUnits(-1))).toBeNaN();
|
|
});
|
|
});
|