Skip to content

Commit

Permalink
fix tests and checks for channel creation
Browse files Browse the repository at this point in the history
  • Loading branch information
MrBrax committed Apr 26, 2022
1 parent 009e78b commit 2b1dd2d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
18 changes: 13 additions & 5 deletions server/src/Core/TwitchChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ export class TwitchChannel {
const channel_login = channel_data.login;

const channel_config = TwitchChannel.channels_config.find(c => c.login === channel_login);
if (!channel_config) throw new Error(`Could not find channel config for channel login: ${channel_login}`);
if (!channel_config) throw new Error(`Could not find channel config in memory for channel login: ${channel_login}`);

channel.channel_data = channel_data;
channel.config = channel_config;
Expand Down Expand Up @@ -607,13 +607,21 @@ export class TwitchChannel {
const data = await TwitchChannel.getChannelDataByLogin(config.login, true);
if (!data) throw new Error(`Could not get channel data for channel login: ${config.login}`);

TwitchChannel.channels_config.push(config);
TwitchChannel.saveChannelsConfig();

const channel = await TwitchChannel.loadFromLogin(config.login, true);
if (!channel) throw new Error(`Channel ${config.login} could not be loaded`);
if (!channel || !channel.userid) throw new Error(`Channel ${config.login} could not be loaded`);

if (channel.userid) await TwitchChannel.subscribe(channel.userid);
try {
await TwitchChannel.subscribe(channel.userid);
} catch (error) {
Log.logAdvanced(LOGLEVEL.ERROR, "channel", `Failed to subscribe to channel ${channel.login}: ${(error as Error).message}`);
TwitchChannel.channels_config = TwitchChannel.channels_config.filter(ch => ch.login !== config.login); // remove channel from config
TwitchChannel.saveChannelsConfig();
throw error; // rethrow error
}

TwitchChannel.channels_config.push(config);
TwitchChannel.saveChannelsConfig();
TwitchChannel.channels.push(channel);

return channel;
Expand Down
6 changes: 3 additions & 3 deletions server/tests/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@ describe("channels", () => {
live_chat: true,
});

expect(res.status).toBe(200);
expect(res.body.message).toBeUndefined();
expect(res.body.message).toContain("'test' created");
expect(res.body.data).toHaveProperty("display_name");
expect(res.status).toBe(200);

expect(spy1).toHaveBeenCalled();
expect(spy2).toHaveBeenCalled();
Expand Down Expand Up @@ -175,7 +175,7 @@ describe("channels", () => {
it("should remove a channel", async () => {
const res = await request(app).delete("/api/v0/channels/test");
expect(res.status).toBe(200);
expect(res.body.message).toBeUndefined();
expect(res.body.message).toContain("'test' deleted");
expect(res.body.status).toBe("OK");
expect(spy3).toHaveBeenCalled();
});
Expand Down

0 comments on commit 2b1dd2d

Please sign in to comment.