Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Implement config command #3

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/app/commands/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ describe("makeCreateCommand", () => {
expiresIn: 3600,
secret: "secret",
issuedAt: 1000,
}).mockResolvedValueOnce({
accessToken: "token",
expiresIn: 3600,
secret: "secret",
issuedAt: 1000,
});
vi.mocked(mockAddApp).mockResolvedValueOnce({ liffId: "12345" });

Expand Down
11 changes: 6 additions & 5 deletions src/app/commands/create.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
import { createCommand } from "commander";
import { LiffApiClient } from "../../api/liff.js";
import { resolveChannel } from "../../channel/resolveChannel.js";
import { getLiffBaseUrl } from "../../channel/baseUrl.js";

const createAction = async (options: {
channelId?: string;
name: string;
endpointUrl: string;
viewType: string;
}) => {
const accessToken = (await resolveChannel(options?.channelId))?.accessToken;
if (!accessToken) {
const channelInfo = await resolveChannel(options?.channelId);
if (!channelInfo) {
throw new Error(`Access token not found.
Please provide a valid channel ID or set the current channel first.
`);
}

const liffBaseUrl = await getLiffBaseUrl(options?.channelId);
const client = new LiffApiClient({
token: accessToken,
baseUrl: "https://api.line.me",
token: channelInfo.accessToken,
baseUrl: liffBaseUrl,
});
const { liffId } = await client.addApp({
view: {
Expand Down
3 changes: 3 additions & 0 deletions src/app/commands/delete.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import inquire from "inquirer";
import { LiffApiClient } from "../../api/liff.js";
import { resolveChannel } from "../../channel/resolveChannel.js";
import { makeDeleteCommand } from "./delete.js";
import { getApiBaseUrl } from "../../channel/baseUrl.js";

vi.mock("inquirer");

Expand All @@ -24,6 +25,7 @@ vi.mock("../../api/liff.js", () => {
};
});
vi.mock("../../channel/resolveChannel.js");
vi.mock("../../channel/baseUrl.js");

describe("makeDeleteCommand", () => {
let mockConsoleInfo: MockInstance<
Expand All @@ -44,6 +46,7 @@ describe("makeDeleteCommand", () => {
});

it("should delete a LIFF app successfully", async () => {
vi.mocked(getApiBaseUrl).mockResolvedValueOnce("https://api.line.me");
vi.mocked(resolveChannel).mockResolvedValueOnce({
accessToken: "token",
expiresIn: 3600,
Expand Down
11 changes: 7 additions & 4 deletions src/app/commands/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@ import { Command } from "commander";
import { resolveChannel } from "../../channel/resolveChannel.js";
import { LiffApiClient } from "../../api/liff.js";
import inquirer from "inquirer";
import { getLiffBaseUrl } from "../../channel/baseUrl.js";

const deleteAction = async (options: {
channelId?: string;
liffId: string;
}) => {
const accessToken = (await resolveChannel(options?.channelId))?.accessToken;
if (!accessToken) {
const channelInfo = await resolveChannel(options?.channelId);
if (!channelInfo) {
throw new Error(`Access token not found.
Please provide a valid channel ID or set the current channel first.
`);
}
const liffBaseUrl = await getLiffBaseUrl(options?.channelId);

const { confirmDelete } = await inquirer.prompt<{ confirmDelete: boolean }>([
{
Expand All @@ -25,9 +27,10 @@ const deleteAction = async (options: {
if (!confirmDelete) return;

const client = new LiffApiClient({
token: accessToken,
baseUrl: "https://api.line.me",
token: channelInfo.accessToken,
baseUrl: liffBaseUrl,
});

console.info(`Deleting LIFF app...`);
await client.deleteApp(options.liffId);

Expand Down
5 changes: 5 additions & 0 deletions src/app/commands/list.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ describe("makeListCommand", () => {
expiresIn: 3600,
secret: "secret",
issuedAt: 1000,
}).mockResolvedValueOnce({
accessToken: "valid_token",
expiresIn: 3600,
secret: "secret",
issuedAt: 1000,
});
vi.mocked(mockFetchApps).mockResolvedValueOnce({
apps: [
Expand Down
11 changes: 7 additions & 4 deletions src/app/commands/list.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import { Command } from "commander";
import { LiffApiClient } from "../../api/liff.js";
import { resolveChannel } from "../../channel/resolveChannel.js";
import { getLiffBaseUrl } from "../../channel/baseUrl.js";

const listAction = async (options: { channelId?: string }) => {
const accessToken = (await resolveChannel(options?.channelId))?.accessToken;
if (!accessToken) {
const channelInfo = await resolveChannel(options?.channelId);
if (!channelInfo) {
throw new Error(`Access token not found.
Please provide a valid channel ID or set the current channel first.
`);
}
const liffBaseUrl = await getLiffBaseUrl(options?.channelId);

const client = new LiffApiClient({
token: accessToken,
baseUrl: "https://api.line.me",
token: channelInfo.accessToken,
baseUrl: liffBaseUrl,
});

const { apps } = await client.fetchApps();

console.info("LIFF apps:");
Expand Down
5 changes: 5 additions & 0 deletions src/app/commands/update.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ describe("makeUpdateCommand", () => {
expiresIn: 3600,
secret: "secret",
issuedAt: 1000,
}).mockResolvedValueOnce({
accessToken: "token",
expiresIn: 3600,
secret: "secret",
issuedAt: 1000,
});
vi.mocked(mockUpdateApp).mockResolvedValueOnce();

Expand Down
10 changes: 6 additions & 4 deletions src/app/commands/update.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createCommand } from "commander";
import { LiffApiClient } from "../../api/liff.js";
import { resolveChannel } from "../../channel/resolveChannel.js";
import { getLiffBaseUrl } from "../../channel/baseUrl.js";

const updateAction = async (options: {
liffId: string;
Expand All @@ -9,16 +10,17 @@ const updateAction = async (options: {
endpointUrl?: string;
viewType?: string;
}) => {
const accessToken = (await resolveChannel(options?.channelId))?.accessToken;
if (!accessToken) {
const channelInfo = await resolveChannel(options?.channelId);
if (!channelInfo) {
throw new Error(`Access token not found.
Please provide a valid channel ID or set the current channel first.
`);
}
const liffBaseUrl = await getLiffBaseUrl(options?.channelId);

const client = new LiffApiClient({
token: accessToken,
baseUrl: "https://api.line.me",
token: channelInfo.accessToken,
baseUrl: liffBaseUrl,
});
await client.updateApp(options.liffId, {
view: {
Expand Down
183 changes: 183 additions & 0 deletions src/channel/baseUrl.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
import { getApiBaseUrl, getLiffBaseUrl, setApiBaseUrl, setLiffBaseUrl } from "./baseUrl.js";
import { resolveChannel } from "./resolveChannel.js";
import { getChannel, setChannel } from "./stores/channels.js";

vi.mock("./resolveChannel.js");
vi.mock("./stores/channels.js");

describe("baseUrl", () => {
const mockChannelId = "123";
const mockApiBaseUrl = "https://api.example.com";
const mockLiffBaseUrl = "https://liff.example.com";
const mockChannel = {
secret: "secret",
accessToken: "token",
expiresIn: 3600,
issuedAt: 1000,
baseUrl: {
api: mockApiBaseUrl,
liff: mockLiffBaseUrl,
},
};

beforeEach(() => {
vi.resetAllMocks();
});

afterEach(() => {
vi.restoreAllMocks();
});

describe("getApiBaseUrl", () => {
it("should return the api base URL from channel info", async () => {
vi.mocked(resolveChannel).mockResolvedValueOnce(mockChannel);

const result = await getApiBaseUrl(mockChannelId);

expect(resolveChannel).toHaveBeenCalledWith(mockChannelId);
expect(result).toBe(mockApiBaseUrl);
});

it("should use default API base URL if not set in channel", async () => {
vi.mocked(resolveChannel).mockResolvedValueOnce({
...mockChannel,
baseUrl: {} // No API base URL set
});

const result = await getApiBaseUrl(mockChannelId);

expect(result).toBe("https://api.line.me"); // Default from BASE_URL_CONFIG
});

it("should throw an error if channel is not found", async () => {
vi.mocked(resolveChannel).mockResolvedValueOnce(undefined);

await expect(getApiBaseUrl(mockChannelId)).rejects.toThrow("Channel not found.");
});
});

describe("getLiffBaseUrl", () => {
it("should return the liff base URL from channel info", async () => {
vi.mocked(resolveChannel).mockResolvedValueOnce(mockChannel);

const result = await getLiffBaseUrl(mockChannelId);

expect(resolveChannel).toHaveBeenCalledWith(mockChannelId);
expect(result).toBe(mockLiffBaseUrl);
});

it("should use default LIFF base URL if not set in channel", async () => {
vi.mocked(resolveChannel).mockResolvedValueOnce({
...mockChannel,
baseUrl: {} // No LIFF base URL set
});

const result = await getLiffBaseUrl(mockChannelId);

expect(result).toBe("https://liff.line.me"); // Default from BASE_URL_CONFIG
});

it("should throw an error if channel is not found", async () => {
vi.mocked(resolveChannel).mockResolvedValueOnce(undefined);

await expect(getLiffBaseUrl(mockChannelId)).rejects.toThrow("Channel not found.");
});
});

describe("setApiBaseUrl", () => {
it("should update the API base URL of a channel", () => {
const newApiBaseUrl = "https://new-api.example.com";
vi.mocked(getChannel).mockReturnValueOnce(mockChannel);

setApiBaseUrl(mockChannelId, newApiBaseUrl);

expect(getChannel).toHaveBeenCalledWith(mockChannelId);
expect(setChannel).toHaveBeenCalledWith(mockChannelId, {
...mockChannel,
baseUrl: {
...mockChannel.baseUrl,
api: newApiBaseUrl,
},
});
});

it("should throw an error if channel is not found", () => {
vi.mocked(getChannel).mockReturnValueOnce(undefined);

expect(() => setApiBaseUrl(mockChannelId, "https://api.example.com")).toThrow(
`Channel ${mockChannelId} is not added yet.`
);
expect(setChannel).not.toHaveBeenCalled();
});

it("should handle channel without baseUrl property", () => {
const channelWithoutBaseUrl = {
secret: "secret",
accessToken: "token",
expiresIn: 3600,
issuedAt: 1000,
};
const newApiBaseUrl = "https://new-api.example.com";

vi.mocked(getChannel).mockReturnValueOnce(channelWithoutBaseUrl);

setApiBaseUrl(mockChannelId, newApiBaseUrl);

expect(setChannel).toHaveBeenCalledWith(mockChannelId, {
...channelWithoutBaseUrl,
baseUrl: {
api: newApiBaseUrl,
},
});
});
});

describe("setLiffBaseUrl", () => {
it("should update the LIFF base URL of a channel", () => {
const newLiffBaseUrl = "https://new-liff.example.com";
vi.mocked(getChannel).mockReturnValueOnce(mockChannel);

setLiffBaseUrl(mockChannelId, newLiffBaseUrl);

expect(getChannel).toHaveBeenCalledWith(mockChannelId);
expect(setChannel).toHaveBeenCalledWith(mockChannelId, {
...mockChannel,
baseUrl: {
...mockChannel.baseUrl,
liff: newLiffBaseUrl,
},
});
});

it("should throw an error if channel is not found", () => {
vi.mocked(getChannel).mockReturnValueOnce(undefined);

expect(() => setLiffBaseUrl(mockChannelId, "https://liff.example.com")).toThrow(
`Channel ${mockChannelId} is not added yet.`
);
expect(setChannel).not.toHaveBeenCalled();
});

it("should handle channel without baseUrl property", () => {
const channelWithoutBaseUrl = {
secret: "secret",
accessToken: "token",
expiresIn: 3600,
issuedAt: 1000,
};
const newLiffBaseUrl = "https://new-liff.example.com";

vi.mocked(getChannel).mockReturnValueOnce(channelWithoutBaseUrl);

setLiffBaseUrl(mockChannelId, newLiffBaseUrl);

expect(setChannel).toHaveBeenCalledWith(mockChannelId, {
...channelWithoutBaseUrl,
baseUrl: {
liff: newLiffBaseUrl,
},
});
});
});
});
Loading
Loading