|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +import { AzureKeyCredential } from "@azure/core-auth"; |
| 5 | +import { RemoteRenderingClient } from "../src/index.js"; |
| 6 | +import { |
| 7 | + ClientSecretCredential, |
| 8 | + DefaultAzureCredential, |
| 9 | + DeviceCodeCredential, |
| 10 | +} from "@azure/identity"; |
| 11 | +import { setLogLevel } from "@azure/logger"; |
| 12 | +import { describe, it } from "vitest"; |
| 13 | +import { randomUUID } from "node:crypto"; |
| 14 | + |
| 15 | +describe("snippets", () => { |
| 16 | + it("ReadmeSampleCreateClient_KeyCredential", async () => { |
| 17 | + const accountDomain = "<account domain>"; |
| 18 | + const accountId = "<account ID>"; |
| 19 | + const accountKey = "<account key>"; |
| 20 | + const serviceEndpoint = "<serviceEndpoint>"; |
| 21 | + // @ts-preserve-whitespace |
| 22 | + const credential = new AzureKeyCredential(accountKey); |
| 23 | + const client = new RemoteRenderingClient(serviceEndpoint, accountId, accountDomain, credential); |
| 24 | + }); |
| 25 | + |
| 26 | + it("ReadmeSampleCreateClient_DeviceCodeCredential", async () => { |
| 27 | + const accountDomain = "<account domain>"; |
| 28 | + const accountId = "<account ID>"; |
| 29 | + const serviceEndpoint = "<serviceEndpoint>"; |
| 30 | + const tenantId = "<tenant ID>"; |
| 31 | + const clientId = "<client ID>"; |
| 32 | + // @ts-preserve-whitespace |
| 33 | + const userPromptCallback = (deviceCodeInfo) => { |
| 34 | + console.debug(deviceCodeInfo.message); |
| 35 | + console.log(deviceCodeInfo.message); |
| 36 | + }; |
| 37 | + // @ts-preserve-whitespace |
| 38 | + const credential = new DeviceCodeCredential({ |
| 39 | + tenantId, |
| 40 | + clientId, |
| 41 | + userPromptCallback, |
| 42 | + authorityHost: `https://login.microsoftonline.com/${tenantId}`, |
| 43 | + }); |
| 44 | + // @ts-preserve-whitespace |
| 45 | + const client = new RemoteRenderingClient(serviceEndpoint, accountId, accountDomain, credential); |
| 46 | + }); |
| 47 | + |
| 48 | + it("ReadmeSampleCreateClient_ClientSecretCredential", async () => { |
| 49 | + const accountDomain = "<account domain>"; |
| 50 | + const accountId = "<account ID>"; |
| 51 | + const serviceEndpoint = "<serviceEndpoint>"; |
| 52 | + const tenantId = "<tenant ID>"; |
| 53 | + const clientId = "<client ID>"; |
| 54 | + const clientSecret = "<client secret>"; |
| 55 | + // @ts-preserve-whitespace |
| 56 | + const credential = new ClientSecretCredential(tenantId, clientId, clientSecret, { |
| 57 | + authorityHost: `https://login.microsoftonline.com/${tenantId}`, |
| 58 | + }); |
| 59 | + // @ts-preserve-whitespace |
| 60 | + const client = new RemoteRenderingClient(serviceEndpoint, accountId, accountDomain, credential); |
| 61 | + }); |
| 62 | + |
| 63 | + it("ReadmeSampleCreateClient_DefaultAzureCredential", async () => { |
| 64 | + const accountDomain = "<account domain>"; |
| 65 | + const accountId = "<account ID>"; |
| 66 | + const serviceEndpoint = "<serviceEndpoint>"; |
| 67 | + // @ts-preserve-whitespace |
| 68 | + const credential = new DefaultAzureCredential(); |
| 69 | + const client = new RemoteRenderingClient(serviceEndpoint, accountId, accountDomain, credential); |
| 70 | + }); |
| 71 | + |
| 72 | + it("ReadmeSampleCreateClient_AccessToken", async () => { |
| 73 | + const accountId = "<account ID>"; |
| 74 | + const serviceEndpoint = "<serviceEndpoint>"; |
| 75 | + // @ts-preserve-whitespace |
| 76 | + // getMixedRealityAccessTokenFromWebService is a hypothetical method that retrieves |
| 77 | + // a Mixed Reality access token from a web service. The web service would use the |
| 78 | + // MixedRealityStsClient and credentials to obtain an access token to be returned |
| 79 | + // to the client. |
| 80 | + async function getMixedRealityAccessTokenFromWebService() { |
| 81 | + return { |
| 82 | + token: "<access token>", |
| 83 | + expiresOnTimestamp: Date.now() + 24 * 60 * 60 * 1000, |
| 84 | + }; |
| 85 | + } |
| 86 | + const accessToken = await getMixedRealityAccessTokenFromWebService(); |
| 87 | + // @ts-preserve-whitespace |
| 88 | + const client = new RemoteRenderingClient(serviceEndpoint, accountId, accessToken); |
| 89 | + }); |
| 90 | + |
| 91 | + it("ReadmeSampleConvertASimpleAsset", async () => { |
| 92 | + const accountDomain = "<account domain>"; |
| 93 | + const accountId = "<account ID>"; |
| 94 | + const serviceEndpoint = "<serviceEndpoint>"; |
| 95 | + const storageAccountName = "<storageAccountName>"; |
| 96 | + const blobContainerName = "<blobStorageName>"; |
| 97 | + const storageContainerUrl = `https://${storageAccountName}.blob.core.windows.net/${blobContainerName}`; |
| 98 | + // @ts-preserve-whitespace |
| 99 | + const credential = new DefaultAzureCredential(); |
| 100 | + const client = new RemoteRenderingClient(serviceEndpoint, accountId, accountDomain, credential); |
| 101 | + // @ts-preserve-whitespace |
| 102 | + const inputSettings = { |
| 103 | + storageContainerUrl, |
| 104 | + relativeInputAssetPath: "box.fbx", |
| 105 | + }; |
| 106 | + const outputSettings = { |
| 107 | + storageContainerUrl, |
| 108 | + }; |
| 109 | + const conversionSettings = { inputSettings, outputSettings }; |
| 110 | + // @ts-preserve-whitespace |
| 111 | + // A randomly generated UUID is a good choice for a conversionId. |
| 112 | + const conversionId = randomUUID(); |
| 113 | + // @ts-preserve-whitespace |
| 114 | + const conversionPoller = await client.beginConversion(conversionId, conversionSettings); |
| 115 | + // @ts-preserve-whitespace |
| 116 | + const conversion = await conversionPoller.pollUntilDone(); |
| 117 | + // @ts-preserve-whitespace |
| 118 | + if (conversion.status === "Succeeded") { |
| 119 | + console.log(`Conversion succeeded: Output written to ${conversion.output?.outputAssetUrl}`); |
| 120 | + } else if (conversion.status === "Failed") { |
| 121 | + console.log(`Conversion failed: ${conversion.error.code} ${conversion.error.message}`); |
| 122 | + } |
| 123 | + }); |
| 124 | + |
| 125 | + it("ReadmeSampleConvertAMoreComplexAsset", async () => { |
| 126 | + const accountDomain = "<account domain>"; |
| 127 | + const accountId = "<account ID>"; |
| 128 | + const serviceEndpoint = "<serviceEndpoint>"; |
| 129 | + const storageAccountName = "<storageAccountName>"; |
| 130 | + const blobContainerName = "<blobStorageName>"; |
| 131 | + const storageAccountName2 = "<storageAccountName2>"; |
| 132 | + const blobContainerName2 = "<blobStorageName2>"; |
| 133 | + const inputStorageUrl = `https://${storageAccountName}.blob.core.windows.net/${blobContainerName}`; |
| 134 | + const outputStorageUrl = `https://${storageAccountName2}.blob.core.windows.net/${blobContainerName2}`; |
| 135 | + // @ts-preserve-whitespace |
| 136 | + const credential = new DefaultAzureCredential(); |
| 137 | + const client = new RemoteRenderingClient(serviceEndpoint, accountId, accountDomain, credential); |
| 138 | + // @ts-preserve-whitespace |
| 139 | + const inputSettings = { |
| 140 | + storageContainerUrl: inputStorageUrl, |
| 141 | + blobPrefix: "Bicycle", |
| 142 | + relativeInputAssetPath: "bicycle.gltf", |
| 143 | + }; |
| 144 | + const outputSettings = { |
| 145 | + storageContainerUrl: outputStorageUrl, |
| 146 | + blobPrefix: "ConvertedBicycle", |
| 147 | + }; |
| 148 | + const conversionSettings = { inputSettings, outputSettings }; |
| 149 | + // @ts-preserve-whitespace |
| 150 | + const conversionId = randomUUID(); |
| 151 | + // @ts-preserve-whitespace |
| 152 | + const conversionPoller = await client.beginConversion(conversionId, conversionSettings); |
| 153 | + // @ts-preserve-whitespace |
| 154 | + const conversion = await conversionPoller.pollUntilDone(); |
| 155 | + // @ts-preserve-whitespace |
| 156 | + if (conversion.status === "Succeeded") { |
| 157 | + console.log(`Conversion succeeded: Output written to ${conversion.output?.outputAssetUrl}`); |
| 158 | + } else if (conversion.status === "Failed") { |
| 159 | + console.log(`Conversion failed: ${conversion.error.code} ${conversion.error.message}`); |
| 160 | + } |
| 161 | + }); |
| 162 | + |
| 163 | + it("ReadmeSampleListConversions", async () => { |
| 164 | + const accountDomain = "<account domain>"; |
| 165 | + const accountId = "<account ID>"; |
| 166 | + const serviceEndpoint = "<serviceEndpoint>"; |
| 167 | + // @ts-preserve-whitespace |
| 168 | + const credential = new DefaultAzureCredential(); |
| 169 | + const client = new RemoteRenderingClient(serviceEndpoint, accountId, accountDomain, credential); |
| 170 | + // @ts-preserve-whitespace |
| 171 | + for await (const conversion of client.listConversions()) { |
| 172 | + if (conversion.status === "Succeeded") { |
| 173 | + console.log( |
| 174 | + `Conversion ${conversion.conversionId} succeeded: Output written to ${conversion.output?.outputAssetUrl}`, |
| 175 | + ); |
| 176 | + } else if (conversion.status === "Failed") { |
| 177 | + console.log( |
| 178 | + `Conversion ${conversion.conversionId} failed: ${conversion.error.code} ${conversion.error.message}`, |
| 179 | + ); |
| 180 | + } |
| 181 | + } |
| 182 | + }); |
| 183 | + |
| 184 | + it("ReadmeSampleCreateASession", async () => { |
| 185 | + const accountDomain = "<account domain>"; |
| 186 | + const accountId = "<account ID>"; |
| 187 | + const serviceEndpoint = "<serviceEndpoint>"; |
| 188 | + // @ts-preserve-whitespace |
| 189 | + const credential = new DefaultAzureCredential(); |
| 190 | + const client = new RemoteRenderingClient(serviceEndpoint, accountId, accountDomain, credential); |
| 191 | + // @ts-preserve-whitespace |
| 192 | + const sessionSettings = { |
| 193 | + maxLeaseTimeInMinutes: 4, |
| 194 | + size: "Standard", |
| 195 | + }; |
| 196 | + // @ts-preserve-whitespace |
| 197 | + // A randomly generated UUID is a good choice for a conversionId. |
| 198 | + const sessionId = randomUUID(); |
| 199 | + // @ts-preserve-whitespace |
| 200 | + const sessionPoller = await client.beginSession(sessionId, sessionSettings); |
| 201 | + }); |
| 202 | + |
| 203 | + it("ReadmeSampleExtendLease", async () => { |
| 204 | + const accountDomain = "<account domain>"; |
| 205 | + const accountId = "<account ID>"; |
| 206 | + const serviceEndpoint = "<serviceEndpoint>"; |
| 207 | + // @ts-preserve-whitespace |
| 208 | + const credential = new DefaultAzureCredential(); |
| 209 | + const client = new RemoteRenderingClient(serviceEndpoint, accountId, accountDomain, credential); |
| 210 | + // @ts-preserve-whitespace |
| 211 | + const sessionId = "<session ID from previous step>"; |
| 212 | + const currentSession = await client.getSession(sessionId); |
| 213 | + if (currentSession.status === "Ready") { |
| 214 | + if ( |
| 215 | + currentSession.maxLeaseTimeInMinutes - |
| 216 | + (Date.now() - +currentSession.properties.createdOn) / 60000 < |
| 217 | + 2 |
| 218 | + ) { |
| 219 | + const newLeaseTime = currentSession.maxLeaseTimeInMinutes + 15; |
| 220 | + // @ts-preserve-whitespace |
| 221 | + await client.updateSession(sessionId, { maxLeaseTimeInMinutes: newLeaseTime }); |
| 222 | + } |
| 223 | + } |
| 224 | + }); |
| 225 | + |
| 226 | + it("ReadmeSampleListSessions", async () => { |
| 227 | + const accountDomain = "<account domain>"; |
| 228 | + const accountId = "<account ID>"; |
| 229 | + const serviceEndpoint = "<serviceEndpoint>"; |
| 230 | + // @ts-preserve-whitespace |
| 231 | + const credential = new DefaultAzureCredential(); |
| 232 | + const client = new RemoteRenderingClient(serviceEndpoint, accountId, accountDomain, credential); |
| 233 | + // @ts-preserve-whitespace |
| 234 | + for await (const session of client.listSessions()) { |
| 235 | + console.log(`Session ${session.sessionId} is ${session.status}`); |
| 236 | + } |
| 237 | + }); |
| 238 | + |
| 239 | + it("ReadmeSampleStopASession", async () => { |
| 240 | + const accountDomain = "<account domain>"; |
| 241 | + const accountId = "<account ID>"; |
| 242 | + const serviceEndpoint = "<serviceEndpoint>"; |
| 243 | + // @ts-preserve-whitespace |
| 244 | + const credential = new DefaultAzureCredential(); |
| 245 | + const client = new RemoteRenderingClient(serviceEndpoint, accountId, accountDomain, credential); |
| 246 | + // @ts-preserve-whitespace |
| 247 | + const sessionId = "<session ID from previous step>"; |
| 248 | + await client.endSession(sessionId); |
| 249 | + }); |
| 250 | + |
| 251 | + it("SetLogLevel", async () => { |
| 252 | + setLogLevel("info"); |
| 253 | + }); |
| 254 | +}); |
0 commit comments