Skip to content

Commit

Permalink
Merge pull request #156 from times-yasunori/improve-from-slack-text
Browse files Browse the repository at this point in the history
POST /awesome/from-slack-text で body がなければ空のテンプレートを返すようにする
  • Loading branch information
tomoya authored Nov 6, 2024
2 parents 9efb143 + 3b53141 commit b9d220e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
34 changes: 21 additions & 13 deletions packages/api/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ describe("Test GET /awesome/random", () => {
});
});

describe("Test GET /awesome/random", () => {
describe("Test GET /awesome/:id", () => {
test("Should return 200 response", async () => {
const res = await app.request("/awesome/1");
expect(res.status).toBe(200);
Expand Down Expand Up @@ -137,11 +137,12 @@ describe("Test POST /awesome/from-slack-text", () => {
yasuhara`,
});
const parsed = await res.text();
const today = new Date().toISOString().split("T").at(0);
const [header, id, ...rest] = parsed.split("\n");
expect(header).toStrictEqual("[[yasunori]]");
expect(id).toMatch("id = "); // いまのテスト環境だとidが変化するため値のテストはできない
expect(rest.join("\n")).toStrictEqual(`title = "えっ、yasunori 知らないの?"
date = "2024-11-02"
date = "${today}"
at = "vim-jp #times-yasunori"
senpan = "tomoya"
content = """
Expand All @@ -154,17 +155,24 @@ meta = """
`);
});

test("Should return 404 error response if entry not found", async () => {
const res = await app.request("/awesome/0");
expect(res.status).toBe(404);
expect(await res.json()).toStrictEqual({ errors: ["not found"] });
});

test("Should return 404 error response if params is not number", async () => {
const res = await app.request("/awesome/id");
expect(res.status).toBe(404);
expect(await res.json()).toMatchObject({
errors: [{ type: "safe_integer" }],
test("Should return blank YA toml response", async () => {
const res = await app.request("/awesome/from-slack-text", {
method: "POST",
});
const parsed = await res.text();
const today = new Date().toISOString().split("T").at(0);
const [header, id, ...rest] = parsed.split("\n");
expect(header).toStrictEqual("[[yasunori]]");
expect(id).toMatch("id = "); // いまのテスト環境だとidが変化するため値のテストはできない
expect(rest.join("\n")).toStrictEqual(`title = ""
date = "${today}"
at = "vim-jp #times-yasunori"
senpan = ""
content = """
"""
meta = """
"""
`);
});
});
4 changes: 2 additions & 2 deletions packages/api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ const route = app
}
const id = latestEntry.id + 1;
const lines = slackText.split("\n");
const [senpan, , title, ...restContents] = lines;
const [senpan, , title = "", ...restContents] = lines;
const date = new Date().toISOString().split("T").at(0);
const content = `${title}\n${restContents.join("\n")}`;
const content = title ? `${title}\n${restContents.join("\n")}` : "";
const tomlString = `[[yasunori]]\nid = ${id}\ntitle = "${title}"\ndate = "${date}"\nat = "vim-jp #times-yasunori"\nsenpan = "${senpan}"\ncontent = """\n${content}\n"""\nmeta = """\n"""\n`;
return c.text(tomlString);
})
Expand Down

0 comments on commit b9d220e

Please sign in to comment.