|
| 1 | +use std::io::Write; |
| 2 | +use std::process::{Command, Stdio}; |
| 3 | + |
| 4 | +use base64::{Engine as _, engine::general_purpose::STANDARD}; |
| 5 | +use devup_mcp_figma::{ |
| 6 | + AssetExportOutcome, AssetFormat, AssetRequest, AssetStatus, ReadToolCall, UpstreamResult, |
| 7 | + asset_export_from_result, |
| 8 | +}; |
| 9 | +use serde_json::{Value, json}; |
| 10 | + |
| 11 | +fn execute( |
| 12 | + format: AssetFormat, |
| 13 | + length: usize, |
| 14 | + writer: bool, |
| 15 | + export_fails: bool, |
| 16 | +) -> (AssetRequest, Value) { |
| 17 | + let request = AssetRequest { |
| 18 | + asset_id: "1:2:node".to_owned(), |
| 19 | + node_id: "1:2".to_owned(), |
| 20 | + field: "node".to_owned(), |
| 21 | + image_hash: None, |
| 22 | + format, |
| 23 | + scale: 2, |
| 24 | + }; |
| 25 | + let call = ReadToolCall::asset_export("fixture", Some("v1".to_owned()), request.clone()); |
| 26 | + let code = call.arguments()["code"].as_str().unwrap().to_owned(); |
| 27 | + let input = |
| 28 | + json!({"code": code, "length": length, "writer": writer, "exportFails": export_fails}); |
| 29 | + // The native Plugin API provides base64Encode, but no figma.io. Keep the |
| 30 | + // source bytes deterministic and exercise the actual compiled export script |
| 31 | + // and Rust response decoder, including JSON nested in the bridge envelope. |
| 32 | + let js = r#" |
| 33 | +const input = JSON.parse(require('node:fs').readFileSync(0, 'utf8')); |
| 34 | +const bytes = Uint8Array.from({length: input.length}, (_, i) => 65 + i % 26); |
| 35 | +let exports = 0, writes = 0; |
| 36 | +const figma = { |
| 37 | + fileKey: 'fixture', |
| 38 | + base64Encode: value => Buffer.from(value).toString('base64'), |
| 39 | + getNodeByIdAsync: async () => ({exportAsync: async settings => { |
| 40 | + exports++; |
| 41 | + if (input.exportFails) throw new Error('renderer failed'); |
| 42 | + return settings.format === 'SVG_STRING' ? Buffer.from(bytes).toString() : bytes; |
| 43 | + }}), |
| 44 | +}; |
| 45 | +if (input.writer) figma.io = {write: () => { writes++; }}; |
| 46 | +const AsyncFunction = Object.getPrototypeOf(async function(){}).constructor; |
| 47 | +new AsyncFunction('figma', input.code)(figma).then(data => { |
| 48 | + process.stdout.write(JSON.stringify({exports, writes, data})); |
| 49 | +}).catch(error => { console.error(error); process.exitCode = 1; }); |
| 50 | +"#; |
| 51 | + let mut child = Command::new("node") |
| 52 | + .args(["-e", js]) |
| 53 | + .stdin(Stdio::piped()) |
| 54 | + .stdout(Stdio::piped()) |
| 55 | + .stderr(Stdio::piped()) |
| 56 | + .spawn() |
| 57 | + .expect("Node is required to execute the asset script contract"); |
| 58 | + child |
| 59 | + .stdin |
| 60 | + .take() |
| 61 | + .unwrap() |
| 62 | + .write_all(input.to_string().as_bytes()) |
| 63 | + .unwrap(); |
| 64 | + let output = child.wait_with_output().unwrap(); |
| 65 | + assert!( |
| 66 | + output.status.success(), |
| 67 | + "{}", |
| 68 | + String::from_utf8_lossy(&output.stderr) |
| 69 | + ); |
| 70 | + (request, serde_json::from_slice(&output.stdout).unwrap()) |
| 71 | +} |
| 72 | + |
| 73 | +#[test] |
| 74 | +fn native_plugin_exports_without_a_remote_file_writer() { |
| 75 | + for (format, length) in [ |
| 76 | + (AssetFormat::Png, 7), |
| 77 | + (AssetFormat::Png, 800_000), |
| 78 | + (AssetFormat::Jpg, 257), |
| 79 | + (AssetFormat::Pdf, 258), |
| 80 | + (AssetFormat::Svg, 11), |
| 81 | + (AssetFormat::Svg, 13_000), |
| 82 | + ] { |
| 83 | + let (request, result) = execute(format, length, false, false); |
| 84 | + assert_eq!(result["exports"], 1); |
| 85 | + assert_eq!(result["writes"], 0); |
| 86 | + let response = UpstreamResult { |
| 87 | + raw: json!({"content": [{"type": "text", "text": result["data"].to_string()}]}), |
| 88 | + }; |
| 89 | + let AssetExportOutcome::Entry(asset) = |
| 90 | + asset_export_from_result(&response, "fixture", Some("v1"), &request).unwrap() |
| 91 | + else { |
| 92 | + panic!("a bridge response can carry the bounded bytes without re-exporting fragments") |
| 93 | + }; |
| 94 | + assert_eq!( |
| 95 | + asset.status, |
| 96 | + AssetStatus::Exported, |
| 97 | + "{format:?}/{length}: {result}" |
| 98 | + ); |
| 99 | + let expected: Vec<u8> = (0..length).map(|i| 65 + (i % 26) as u8).collect(); |
| 100 | + assert_eq!( |
| 101 | + STANDARD.decode(asset.data_base64.unwrap()).unwrap(), |
| 102 | + expected |
| 103 | + ); |
| 104 | + assert_eq!(asset.byte_length, Some(length)); |
| 105 | + assert_eq!(asset.mime_type.as_deref(), Some(format.mime_type())); |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +#[test] |
| 110 | +fn remote_asset_delivery_retains_attachment_and_fragment_limits() { |
| 111 | + for (format, length, status, writes) in [ |
| 112 | + (AssetFormat::Png, 7, "exported", 1), |
| 113 | + (AssetFormat::Png, 800_000, "chunked", 0), |
| 114 | + (AssetFormat::Svg, 11, "exported", 1), |
| 115 | + (AssetFormat::Svg, 13_000, "chunked", 0), |
| 116 | + ] { |
| 117 | + let (_, result) = execute(format, length, true, false); |
| 118 | + assert_eq!(result["data"]["status"], status); |
| 119 | + assert_eq!(result["exports"], 1); |
| 120 | + assert_eq!(result["writes"], writes); |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +#[test] |
| 125 | +fn bridge_does_not_hide_export_failure_or_bypass_byte_limit() { |
| 126 | + for (length, fails, error) in [ |
| 127 | + (3, true, "DEVUP_ASSET_EXPORT_FAILED"), |
| 128 | + (0, false, "DEVUP_ASSET_RESPONSE_TOO_LARGE"), |
| 129 | + (8 * 1024 * 1024 + 1, false, "DEVUP_ASSET_RESPONSE_TOO_LARGE"), |
| 130 | + ] { |
| 131 | + let (_, result) = execute(AssetFormat::Png, length, false, fails); |
| 132 | + assert_eq!(result["data"]["status"], "failed"); |
| 133 | + assert_eq!(result["data"]["errorCode"], error); |
| 134 | + assert_eq!(result["writes"], 0); |
| 135 | + } |
| 136 | +} |
0 commit comments