Skip to content

Commit

Permalink
chore: simplify selectHighestCompatibleVersion
Browse files Browse the repository at this point in the history
  • Loading branch information
im-adithya committed Jan 10, 2025
1 parent 9665bc1 commit 13a8a0b
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 114 deletions.
82 changes: 0 additions & 82 deletions src/NWCClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,85 +56,3 @@ describe("NWCClient", () => {
expect(nwcClient.options.lud16).toBe("[email protected]");
});
});

describe("selectHighestCompatibleVersion", () => {
let nwcClient: NWCClient;
let selectVersion: (walletVersions: string[]) => string | null;
const ORIGINAL_SUPPORTED_VERSIONS = NWCClient.SUPPORTED_VERSIONS;

beforeEach(() => {
nwcClient = new NWCClient();
// Access the private method using type assertion
// eslint-disable-next-line @typescript-eslint/no-explicit-any
selectVersion = (nwcClient as any).selectHighestCompatibleVersion;
});

afterEach(() => {
// Restore the original SUPPORTED_VERSIONS
NWCClient.SUPPORTED_VERSIONS = [...ORIGINAL_SUPPORTED_VERSIONS];
});

test("both client and wallet support version 1.0", () => {
NWCClient.SUPPORTED_VERSIONS = ["0.0", "1.0"];
const walletVersions = ["0.0", "1.0"];
const selected = selectVersion(walletVersions);
expect(selected).toBe("1.0");
});

test("client supports version 1.0 but wallet does not", () => {
NWCClient.SUPPORTED_VERSIONS = ["0.0", "1.0"];
const walletVersions = ["0.0"];
const selected = selectVersion(walletVersions);
expect(selected).toBe("0.0");
});

test("wallet supports version 1.0 but client does not", () => {
NWCClient.SUPPORTED_VERSIONS = ["0.0"];
const walletVersions = ["0.0", "1.0"];
const selected = selectVersion(walletVersions);
expect(selected).toBe("0.0");
});

test("wallet and client do not have overlapping versions", () => {
NWCClient.SUPPORTED_VERSIONS = ["0.0"];
const walletVersions = ["1.0"];
const selected = selectVersion(walletVersions);
expect(selected).toBeNull();
});

// Tests for future
test("client supports more versions than wallet", () => {
NWCClient.SUPPORTED_VERSIONS = ["0.0", "1.3"];
const walletVersions = ["1.2"];
const selected = selectVersion(walletVersions);
expect(selected).toBe("1.2");
});

test("wallet supports more versions than client", () => {
NWCClient.SUPPORTED_VERSIONS = ["0.0", "1.4"];
const walletVersions = ["1.6"];
const selected = selectVersion(walletVersions);
expect(selected).toBe("1.4");
});

test("wallet and client have no overlapping major versions", () => {
NWCClient.SUPPORTED_VERSIONS = ["0.0", "1.0"];
const walletVersions = ["2.0"];
const selected = selectVersion(walletVersions);
expect(selected).toBeNull();
});

test("both client and wallet support multiple versions with different majors", () => {
NWCClient.SUPPORTED_VERSIONS = ["0.0", "1.0", "2.4"];
const walletVersions = ["1.0", "2.3"];
const selected = selectVersion(walletVersions);
expect(selected).toBe("2.3");
});

test("wallet has duplicate versions", () => {
NWCClient.SUPPORTED_VERSIONS = ["0.0", "1.2"];
const walletVersions = ["1.1", "1.1"];
const selected = selectVersion(walletVersions);
expect(selected).toBe("1.1");
});
});
37 changes: 5 additions & 32 deletions src/NWCClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1147,38 +1147,11 @@ export class NWCClient {
private selectHighestCompatibleVersion(
walletVersions: string[],
): string | null {
const parseVersions = (versions: string[]) =>
versions.map((v) => v.split(".").map(Number));

const walletParsed = parseVersions(walletVersions);
const clientParsed = parseVersions(NWCClient.SUPPORTED_VERSIONS);

const walletMajors: number[] = walletParsed
.map(([major]) => major)
.filter((value, index, self) => self.indexOf(value) === index);

const clientMajors: number[] = clientParsed
.map(([major]) => major)
.filter((value, index, self) => self.indexOf(value) === index);

const commonMajors = walletMajors
.filter((major) => clientMajors.includes(major))
.sort((a, b) => b - a);

for (const major of commonMajors) {
const walletMinors = walletParsed
.filter(([m]) => m === major)
.map(([, minor]) => minor);
const clientMinors = clientParsed
.filter(([m]) => m === major)
.map(([, minor]) => minor);

const highestMinor = Math.min(
Math.max(...walletMinors),
Math.max(...clientMinors),
);

return `${major}.${highestMinor}`;
if (walletVersions.includes("1.0")) {
return "1.0";
}
if (walletVersions.includes("0.0")) {
return "0.0";
}
return null;
}
Expand Down

0 comments on commit 13a8a0b

Please sign in to comment.