|
| 1 | +import { expect, test } from "vite-plus/test"; |
| 2 | +import bootstrapCommand from "../src/commands/bootstrap/index.ts"; |
| 3 | + |
| 4 | +const API = { |
| 5 | + loginInfo: "zeldaEasy.cornerstone-portal.cs-console.loginInfo", |
| 6 | + initSpace: "zeldaEasy.bailian-dash-workspace.space.initSpace", |
| 7 | + createUser: "zeldaEasy.bailian-dash-workspace.account.createUser", |
| 8 | + queryBuyResult: "zeldaEasy.bailian-commerce.bill.queryBuyPostpaidResult", |
| 9 | + commodityOrderInfo: "zeldaEasy.bailian-commerce.bill.postpaidCommodityOrderInfo", |
| 10 | + buyCommodity: "zeldaEasy.bailian-commerce.bill.buyPostpaidCommodity", |
| 11 | +} as const; |
| 12 | + |
| 13 | +interface ConsoleCall { |
| 14 | + api: string; |
| 15 | + data: Record<string, unknown>; |
| 16 | +} |
| 17 | + |
| 18 | +function captureStdout(): { read: () => string; restore: () => void } { |
| 19 | + const originalWrite = process.stdout.write.bind(process.stdout); |
| 20 | + let stdout = ""; |
| 21 | + process.stdout.write = ((chunk: string | Uint8Array) => { |
| 22 | + stdout += String(chunk); |
| 23 | + return true; |
| 24 | + }) as typeof process.stdout.write; |
| 25 | + return { |
| 26 | + read: () => stdout, |
| 27 | + restore: () => { |
| 28 | + process.stdout.write = originalWrite; |
| 29 | + }, |
| 30 | + }; |
| 31 | +} |
| 32 | + |
| 33 | +function gatewayResponse(data: unknown): unknown { |
| 34 | + return { |
| 35 | + data: { |
| 36 | + DataV2: { |
| 37 | + data: { |
| 38 | + data, |
| 39 | + }, |
| 40 | + }, |
| 41 | + success: true, |
| 42 | + }, |
| 43 | + }; |
| 44 | +} |
| 45 | + |
| 46 | +function createContext( |
| 47 | + consoleImpl: (api: string, data: Record<string, unknown>) => Promise<unknown>, |
| 48 | +) { |
| 49 | + return { |
| 50 | + settings: { |
| 51 | + dryRun: false, |
| 52 | + output: "json", |
| 53 | + verbose: false, |
| 54 | + }, |
| 55 | + client: { |
| 56 | + console: consoleImpl, |
| 57 | + }, |
| 58 | + }; |
| 59 | +} |
| 60 | + |
| 61 | +test("bootstrap --dry-run lists createUser after initSpace", async () => { |
| 62 | + const stdout = captureStdout(); |
| 63 | + try { |
| 64 | + await bootstrapCommand.run({ |
| 65 | + ...createContext(async () => ({})), |
| 66 | + settings: { dryRun: true, output: "json", verbose: false }, |
| 67 | + } as any); |
| 68 | + } finally { |
| 69 | + stdout.restore(); |
| 70 | + } |
| 71 | + |
| 72 | + const data = JSON.parse(stdout.read()) as { |
| 73 | + apis: Array<{ step: number; api: string }>; |
| 74 | + }; |
| 75 | + expect(data.apis.map((item) => item.api)).toEqual([ |
| 76 | + API.loginInfo, |
| 77 | + API.initSpace, |
| 78 | + API.createUser, |
| 79 | + API.queryBuyResult, |
| 80 | + API.commodityOrderInfo, |
| 81 | + API.buyCommodity, |
| 82 | + ]); |
| 83 | + expect(data.apis.map((item) => item.step)).toEqual([1, 2, 3, 4, 5, 6]); |
| 84 | +}); |
| 85 | + |
| 86 | +test("bootstrap creates user from loginInfo uid when workspace is not initialized", async () => { |
| 87 | + const uid = "AssumedRoleUser300715349082471133"; |
| 88 | + const calls: ConsoleCall[] = []; |
| 89 | + const stdout = captureStdout(); |
| 90 | + const ctx = createContext(async (api, data) => { |
| 91 | + calls.push({ api, data }); |
| 92 | + if (api === API.loginInfo) { |
| 93 | + return gatewayResponse({ spaceInited: false, aliyun: { uid } }); |
| 94 | + } |
| 95 | + if (api === API.queryBuyResult) { |
| 96 | + return gatewayResponse("success"); |
| 97 | + } |
| 98 | + if (api === API.commodityOrderInfo) { |
| 99 | + return gatewayResponse([{ commodityCode: "postpaid", status: 10 }]); |
| 100 | + } |
| 101 | + return gatewayResponse({}); |
| 102 | + }); |
| 103 | + |
| 104 | + try { |
| 105 | + await bootstrapCommand.run(ctx as any); |
| 106 | + } finally { |
| 107 | + stdout.restore(); |
| 108 | + } |
| 109 | + |
| 110 | + expect(calls.map((call) => call.api)).toEqual([ |
| 111 | + API.loginInfo, |
| 112 | + API.initSpace, |
| 113 | + API.createUser, |
| 114 | + API.queryBuyResult, |
| 115 | + API.commodityOrderInfo, |
| 116 | + ]); |
| 117 | + expect(calls.find((call) => call.api === API.createUser)?.data).toEqual({ |
| 118 | + reqDTO: { |
| 119 | + outerKey: uid, |
| 120 | + nickName: uid, |
| 121 | + userName: uid, |
| 122 | + }, |
| 123 | + }); |
| 124 | +}); |
| 125 | + |
| 126 | +test("bootstrap skips initSpace but still creates user when workspace is already initialized", async () => { |
| 127 | + const uid = "AssumedRoleUser300715349082471133"; |
| 128 | + const calls: ConsoleCall[] = []; |
| 129 | + const stdout = captureStdout(); |
| 130 | + const ctx = createContext(async (api, data) => { |
| 131 | + calls.push({ api, data }); |
| 132 | + if (api === API.loginInfo) { |
| 133 | + return gatewayResponse({ |
| 134 | + spaceInited: true, |
| 135 | + aliyun: { uid }, |
| 136 | + }); |
| 137 | + } |
| 138 | + if (api === API.queryBuyResult) { |
| 139 | + return gatewayResponse("success"); |
| 140 | + } |
| 141 | + if (api === API.commodityOrderInfo) { |
| 142 | + return gatewayResponse([{ commodityCode: "postpaid", status: 10 }]); |
| 143 | + } |
| 144 | + return gatewayResponse({}); |
| 145 | + }); |
| 146 | + |
| 147 | + try { |
| 148 | + await bootstrapCommand.run(ctx as any); |
| 149 | + } finally { |
| 150 | + stdout.restore(); |
| 151 | + } |
| 152 | + |
| 153 | + expect(calls.map((call) => call.api)).toEqual([ |
| 154 | + API.loginInfo, |
| 155 | + API.createUser, |
| 156 | + API.queryBuyResult, |
| 157 | + API.commodityOrderInfo, |
| 158 | + ]); |
| 159 | + expect(calls.some((call) => call.api === API.initSpace)).toBe(false); |
| 160 | + expect(calls.find((call) => call.api === API.createUser)?.data).toEqual({ |
| 161 | + reqDTO: { |
| 162 | + outerKey: uid, |
| 163 | + nickName: uid, |
| 164 | + userName: uid, |
| 165 | + }, |
| 166 | + }); |
| 167 | +}); |
0 commit comments