|
| 1 | +import { Dashboard } from "./dashboards.ts"; |
| 2 | +import { assertEquals, mf } from "./deps.ts"; |
| 3 | +import { Mackerel } from "./mackerel.ts"; |
| 4 | + |
| 5 | +const dummyApiKey = "dummy-apikey"; |
| 6 | +const dummyBaseurl = "https://example.com/"; |
| 7 | + |
| 8 | +Deno.test("createDashboards", async () => { |
| 9 | + const param: Dashboard = { |
| 10 | + title: "test title", |
| 11 | + urlPath: "aaaaaaaa", |
| 12 | + memo: "this is test", |
| 13 | + widgets: [ |
| 14 | + { |
| 15 | + type: "markdown", |
| 16 | + title: "test", |
| 17 | + layout: { |
| 18 | + x: 1, |
| 19 | + y: 1, |
| 20 | + width: 3, |
| 21 | + height: 3, |
| 22 | + }, |
| 23 | + markdown: "### h3", |
| 24 | + }, |
| 25 | + ], |
| 26 | + }; |
| 27 | + mf.install(); |
| 28 | + mf.mock( |
| 29 | + "POST@/api/v0/dashboards", |
| 30 | + (_req, _params) => { |
| 31 | + const resp = { |
| 32 | + id: "testid", |
| 33 | + createdAt: 111111111, |
| 34 | + updatedAt: 111111111, |
| 35 | + ...param, |
| 36 | + }; |
| 37 | + return new Response( |
| 38 | + JSON.stringify( |
| 39 | + resp, |
| 40 | + ), |
| 41 | + { status: 200 }, |
| 42 | + ); |
| 43 | + }, |
| 44 | + ); |
| 45 | + const client = new Mackerel.Client(dummyApiKey, dummyBaseurl); |
| 46 | + const resp = await client.createDashboards(param); |
| 47 | + assertEquals(resp.id, "testid"); |
| 48 | + assertEquals(resp.widgets[0].title, "test"); |
| 49 | + mf.uninstall(); |
| 50 | +}); |
| 51 | + |
| 52 | +Deno.test("getDashboards", async () => { |
| 53 | + const dashboardID = "testid"; |
| 54 | + mf.install(); |
| 55 | + mf.mock(`GET@/api/v0/dashboards/${dashboardID}`, (_req, _params) => { |
| 56 | + return new Response( |
| 57 | + JSON.stringify({ |
| 58 | + id: dashboardID, |
| 59 | + createdAt: 1111111111, |
| 60 | + updatedAt: 2222222222, |
| 61 | + title: "Test Dashbord", |
| 62 | + urlPath: "aaaaaaaaaaa", |
| 63 | + memo: "this is test dashbord", |
| 64 | + widgets: [ |
| 65 | + { |
| 66 | + type: "markdown", |
| 67 | + title: "Test Markdown", |
| 68 | + markdown: "# Markdown\n\nthis is markdown", |
| 69 | + layout: { x: 0, y: 0, width: 6, height: 6 }, |
| 70 | + }, |
| 71 | + { |
| 72 | + type: "graph", |
| 73 | + title: "test title", |
| 74 | + graph: { |
| 75 | + type: "role", |
| 76 | + roleFullname: "hatena-mac:hatena-mac", |
| 77 | + name: "cpu.{user,system}", |
| 78 | + isStacked: true, |
| 79 | + }, |
| 80 | + layout: { x: 6, y: 0, width: 6, height: 6 }, |
| 81 | + }, |
| 82 | + { |
| 83 | + type: "alertStatus", |
| 84 | + title: "test title", |
| 85 | + roleFullname: "test:test", |
| 86 | + layout: { x: 12, y: 0, width: 6, height: 6 }, |
| 87 | + }, |
| 88 | + ], |
| 89 | + }), |
| 90 | + { status: 200 }, |
| 91 | + ); |
| 92 | + }); |
| 93 | + const client = new Mackerel.Client(dummyApiKey, dummyBaseurl); |
| 94 | + const resp = await client.getDashboards(dashboardID); |
| 95 | + assertEquals(resp.id, dashboardID); |
| 96 | + assertEquals(resp.widgets[0].type, "markdown"); |
| 97 | +}); |
| 98 | + |
| 99 | +Deno.test("updateDashboards", async () => { |
| 100 | + const dashboardID = "testid"; |
| 101 | + const param: Dashboard = { |
| 102 | + title: "test title", |
| 103 | + urlPath: "aaaaaaaa", |
| 104 | + memo: "this is test", |
| 105 | + widgets: [ |
| 106 | + { |
| 107 | + type: "markdown", |
| 108 | + title: "test", |
| 109 | + layout: { |
| 110 | + x: 1, |
| 111 | + y: 1, |
| 112 | + width: 3, |
| 113 | + height: 3, |
| 114 | + }, |
| 115 | + markdown: "#### h4", |
| 116 | + }, |
| 117 | + ], |
| 118 | + }; |
| 119 | + mf.install(); |
| 120 | + mf.mock( |
| 121 | + `PUT@/api/v0/dashboards/${dashboardID}`, |
| 122 | + (_req, _params) => { |
| 123 | + const resp = { |
| 124 | + id: dashboardID, |
| 125 | + createdAt: 111111111, |
| 126 | + updatedAt: 222222222, |
| 127 | + ...param, |
| 128 | + }; |
| 129 | + return new Response( |
| 130 | + JSON.stringify( |
| 131 | + resp, |
| 132 | + ), |
| 133 | + { status: 200 }, |
| 134 | + ); |
| 135 | + }, |
| 136 | + ); |
| 137 | + const client = new Mackerel.Client(dummyApiKey, dummyBaseurl); |
| 138 | + const resp = await client.updateDashboards(dashboardID, param); |
| 139 | + assertEquals(resp.id, dashboardID); |
| 140 | + assertEquals(resp.widgets[0].title, "test"); |
| 141 | + mf.uninstall(); |
| 142 | +}); |
| 143 | + |
| 144 | +Deno.test("deleteDashboards", async () => { |
| 145 | + const dashboardID = "testid"; |
| 146 | + mf.install(); |
| 147 | + mf.mock(`DELETE@/api/v0/dashboards/${dashboardID}`, (_req, _params) => { |
| 148 | + return new Response( |
| 149 | + JSON.stringify({ |
| 150 | + id: dashboardID, |
| 151 | + createdAt: 1111111111, |
| 152 | + updatedAt: 2222222222, |
| 153 | + title: "Test Dashbord", |
| 154 | + urlPath: "aaaaaaaa", |
| 155 | + memo: "this is test", |
| 156 | + }), |
| 157 | + { status: 200 }, |
| 158 | + ); |
| 159 | + }); |
| 160 | + const client = new Mackerel.Client(dummyApiKey, dummyBaseurl); |
| 161 | + const resp = await client.deleteDashboards(dashboardID); |
| 162 | + assertEquals(resp.id, dashboardID); |
| 163 | + assertEquals(resp.memo, "this is test"); |
| 164 | + mf.uninstall(); |
| 165 | +}); |
| 166 | + |
| 167 | +Deno.test("listDashboards", async () => { |
| 168 | + mf.install(); |
| 169 | + mf.mock("GET@/api/v0/dashboards", (_req, _params) => { |
| 170 | + return new Response( |
| 171 | + JSON.stringify({ |
| 172 | + dashboards: [ |
| 173 | + { |
| 174 | + id: "test id", |
| 175 | + createdAt: 1111111111, |
| 176 | + updatedAt: 2222222222, |
| 177 | + title: "Test Dashbord", |
| 178 | + urlPath: "aaaaaaaa", |
| 179 | + memo: "this is test", |
| 180 | + }, |
| 181 | + ], |
| 182 | + }), |
| 183 | + { status: 200 }, |
| 184 | + ); |
| 185 | + }); |
| 186 | + const client = new Mackerel.Client(dummyApiKey, dummyBaseurl); |
| 187 | + const resp = await client.listDashboards(); |
| 188 | + assertEquals(resp[0].title, "Test Dashbord"); |
| 189 | + assertEquals(resp[0].memo, "this is test"); |
| 190 | + mf.uninstall(); |
| 191 | +}); |
0 commit comments