File size: 8,666 Bytes
afa0cbf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
use crate::export::GenerateTsOptions;
use crate::export::generate_internal_json_schema;
use crate::export::generate_json_with_experimental;
use crate::export::generate_ts_with_options;
use crate::precomputed_exports::decode_precomputed_exports;
use crate::schema_fixtures::SchemaFixtureOptions;
use crate::schema_fixtures::collect_export_files_recursive;
use crate::schema_fixtures::generate_typescript_schema_fixture_subtree_for_tests;
use crate::schema_fixtures::read_schema_fixture_subtree;
use crate::schema_fixtures::write_schema_fixtures_with_options;
use anyhow::Context;
use anyhow::Result;
use pretty_assertions::assert_eq;
use similar::TextDiff;
use std::collections::BTreeMap;
use std::path::Path;
use std::path::PathBuf;

#[test]
fn typescript_schema_fixtures_match_generated() -> Result<()> {
    let schema_root = schema_root()?;
    let fixture_tree = read_tree(&schema_root, "typescript")?;
    let generated_tree = generate_typescript_schema_fixture_subtree_for_tests()
        .context("generate in-memory typescript schema fixtures")?;

    assert_schema_trees_match("typescript", &fixture_tree, &generated_tree)?;
    let config_requirements = generated_tree
        .get(Path::new("v2/ConfigRequirements.ts"))
        .context("generated ConfigRequirements.ts should exist")?;
    anyhow::ensure!(
        !String::from_utf8_lossy(config_requirements).contains("../PathUri")
            || generated_tree.contains_key(Path::new("PathUri.ts")),
        "stable ConfigRequirements.ts imports PathUri but PathUri.ts was not generated"
    );

    Ok(())
}

#[test]
fn json_schema_fixtures_match_generated() -> Result<()> {
    assert_schema_fixtures_match_generated("json", |output_dir| {
        generate_json_with_experimental(output_dir, /*experimental_api*/ false)
    })
}

#[test]
fn stable_precomputed_exports_match_schema_fixtures() -> Result<()> {
    let schema_root = schema_root()?;
    let exports = decode_precomputed_exports(/*experimental_api*/ false)?;

    assert_eq!(
        exports.typescript,
        collect_export_files_recursive(&schema_root.join("typescript"))?
    );
    assert_eq!(
        exports.json_schema,
        collect_export_files_recursive(&schema_root.join("json"))?
    );

    let internal_dir = tempfile::tempdir().context("create internal schema temp dir")?;
    generate_internal_json_schema(internal_dir.path())?;
    assert_json_export_trees_match(
        &exports.internal_json_schema,
        &collect_export_files_recursive(internal_dir.path())?,
    )?;
    Ok(())
}

#[test]
fn experimental_precomputed_exports_match_generated() -> Result<()> {
    let output_dir = tempfile::tempdir().context("create experimental schema temp dir")?;
    let typescript_dir = output_dir.path().join("typescript");
    let json_dir = output_dir.path().join("json");
    generate_ts_with_options(
        &typescript_dir,
        /*prettier*/ None,
        GenerateTsOptions {
            experimental_api: true,
            ..GenerateTsOptions::default()
        },
    )?;
    generate_json_with_experimental(&json_dir, /*experimental_api*/ true)?;

    let exports = decode_precomputed_exports(/*experimental_api*/ true)?;
    assert_eq!(
        exports.typescript,
        collect_export_files_recursive(&typescript_dir)?
    );
    assert_json_export_trees_match(
        &exports.json_schema,
        &collect_export_files_recursive(&json_dir)?,
    )?;
    assert_eq!(exports.internal_json_schema, BTreeMap::new());
    Ok(())
}

#[test]
#[ignore = "invoked by `just write-app-server-schema`"]
fn write_schema_fixtures_from_env() -> Result<()> {
    let schema_root = std::env::var_os("CODEX_APP_SERVER_SCHEMA_ROOT")
        .map(PathBuf::from)
        .context("CODEX_APP_SERVER_SCHEMA_ROOT must be set")?;
    let prettier = std::env::var_os("CODEX_APP_SERVER_SCHEMA_PRETTIER").map(PathBuf::from);
    let experimental = std::env::var("CODEX_APP_SERVER_SCHEMA_EXPERIMENTAL")
        .context("CODEX_APP_SERVER_SCHEMA_EXPERIMENTAL must be set")?;
    let experimental_api = match experimental.as_str() {
        "0" => false,
        "1" => true,
        value => {
            anyhow::bail!("CODEX_APP_SERVER_SCHEMA_EXPERIMENTAL must be 0 or 1, got {value:?}")
        }
    };

    write_schema_fixtures_with_options(
        &schema_root,
        prettier.as_deref(),
        SchemaFixtureOptions { experimental_api },
    )
}

fn assert_schema_fixtures_match_generated(
    label: &'static str,
    generate: impl FnOnce(&Path) -> Result<()>,
) -> Result<()> {
    let schema_root = schema_root()?;
    let fixture_tree = read_tree(&schema_root, label)?;

    let temp_dir = tempfile::tempdir().context("create temp dir")?;
    let generated_root = temp_dir.path().join(label);
    generate(&generated_root).with_context(|| {
        format!(
            "generate {label} schema fixtures into {}",
            generated_root.display()
        )
    })?;

    let generated_tree = read_tree(temp_dir.path(), label)?;
    assert_schema_trees_match(label, &fixture_tree, &generated_tree)
}

fn assert_json_export_trees_match(
    expected: &BTreeMap<String, String>,
    actual: &BTreeMap<String, String>,
) -> Result<()> {
    assert_eq!(
        expected.keys().collect::<Vec<_>>(),
        actual.keys().collect::<Vec<_>>()
    );
    for (path, expected) in expected {
        let expected: serde_json::Value = serde_json::from_str(expected)
            .with_context(|| format!("parse precomputed JSON export {path}"))?;
        let actual: serde_json::Value = serde_json::from_str(&actual[path])
            .with_context(|| format!("parse freshly generated JSON export {path}"))?;
        assert_eq!(expected, actual, "JSON export differs: {path}");
    }
    Ok(())
}

fn assert_schema_trees_match(
    label: &str,
    fixture_tree: &BTreeMap<PathBuf, Vec<u8>>,
    generated_tree: &BTreeMap<PathBuf, Vec<u8>>,
) -> Result<()> {
    let fixture_paths = fixture_tree
        .keys()
        .map(|p| p.display().to_string())
        .collect::<Vec<_>>();
    let generated_paths = generated_tree
        .keys()
        .map(|p| p.display().to_string())
        .collect::<Vec<_>>();

    if fixture_paths != generated_paths {
        let expected = fixture_paths.join("\n");
        let actual = generated_paths.join("\n");
        let diff = TextDiff::from_lines(&expected, &actual)
            .unified_diff()
            .header("fixture", "generated")
            .to_string();

        panic!(
            "Vendored {label} app-server schema fixture file set doesn't match freshly generated output. \
Run `just write-app-server-schema` to overwrite with your changes.\n\n{diff}"
        );
    }

    for (path, expected) in fixture_tree {
        let actual = generated_tree
            .get(path)
            .ok_or_else(|| anyhow::anyhow!("missing generated file: {}", path.display()))?;

        if expected == actual {
            continue;
        }

        let expected_str = String::from_utf8_lossy(expected);
        let actual_str = String::from_utf8_lossy(actual);
        let diff = TextDiff::from_lines(&expected_str, &actual_str)
            .unified_diff()
            .header("fixture", "generated")
            .to_string();
        panic!(
            "Vendored {label} app-server schema fixture {} differs from generated output. \
Run `just write-app-server-schema` to overwrite with your changes.\n\n{diff}",
            path.display()
        );
    }

    Ok(())
}

fn schema_root() -> Result<PathBuf> {
    let typescript_index = codex_utils_cargo_bin::find_resource!("schema/typescript/index.ts")
        .context("resolve TypeScript schema index.ts")?;
    let schema_root = typescript_index
        .parent()
        .and_then(|p| p.parent())
        .context("derive schema root from schema/typescript/index.ts")?
        .to_path_buf();

    let json_bundle =
        codex_utils_cargo_bin::find_resource!("schema/json/codex_app_server_protocol.schemas.json")
            .context("resolve JSON schema bundle")?;
    let json_root = json_bundle
        .parent()
        .and_then(|p| p.parent())
        .context("derive schema root from schema/json/codex_app_server_protocol.schemas.json")?;
    anyhow::ensure!(
        schema_root == json_root,
        "schema roots disagree: typescript={} json={}",
        schema_root.display(),
        json_root.display()
    );

    Ok(schema_root)
}

fn read_tree(root: &Path, label: &str) -> Result<BTreeMap<PathBuf, Vec<u8>>> {
    read_schema_fixture_subtree(root, label).with_context(|| {
        format!(
            "read {label} schema fixture subtree from {}",
            root.display()
        )
    })
}